Thread: My c++ program does not work

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    My c++ program does not work

    I have done this code under here.
    What is does is:
    you write a sentence and it will count words, shortest word etc.

    when i compile it goes fine, but when i run it it doesnt write aout any anserws.

    help me!



    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
    
      int counter1 = 0;
      int counter2 = 0;
      int counter3 = 0;
      int length, largest, smallest, average=0;
      string word;
      char firstchar, lastchar, space;
    
      cout << "Enter your lines:" << endl;
    
    
      while (cin >> word) {
        counter1++;
        length = word.length(); 
        firstchar = word[0];
        if (firstchar >= 'A' && firstchar <= 'Z') 
          counter2++;
        if (counter1 == 1)  
          largest = smallest = length;
        else if (length > largest) 
          {
          largest = length;
          }
        else  if (length < smallest)
          {
          smallest = length;
          }
        lastchar = word[word.length()-1];
        if (lastchar == '!' || lastchar == '?' || lastchar == '.')
          counter3++;  
        average += length; 
      }   
       
      cout << "There were " << counter1 << " words entered" << endl;
      cout << "There were " << counter2 << " capital letters." << endl;   
      cout << "The longest string had " << largest << " letter(s)." << endl;
      cout << "The shortest string had " << smallest << " letter(s)." << endl; 
      cout << "There were " << counter3 << " sentences entered." << endl;
      cout << "The average word length is " << (average / counter1) << " characters." << endl;
        
    return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Your loop:
    Code:
    while (cin >> word){
    will never end by portable means. I have Windows and I pressed Ctrl-Z and [Enter] and it showed the results.

    EDIT: Remember to issue a "cin.clear()" before the below.

    Don't forget to put something that prevents the console from closing at the end, something like cin.get() or cin.ignore() or both.

    EDIT^2: "average / counter1" will trigger a division-by-zero error if the user doesn't pass any words.
    Last edited by GReaper; 09-26-2011 at 09:33 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    does your program ever terminate? I suspect that it does not. even after you press enter at the end of your sentence, the while loop continues, unless you send it an EOF (ctrl-Z on windows/ctrl-D on linux).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why can't I get this program to work?
    By nadeni0119 in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2003, 04:59 PM
  2. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 11:04 PM
  3. How does this program work??
    By Paninaro in forum C Programming
    Replies: 4
    Last Post: 06-20-2002, 06:50 AM
  4. Can't get program to work
    By theirishrover12 in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 11:10 AM