Hello. I need some help with my program wich shoulc calculate letters in a sentence and then print out the longest, shortest, how many words, and what's the avrage value.
Thanks for some help. :)

#include <iostream>
#include <string>

using namespace std;

int main()
{
       int word;
       int n;
       string sentence;
       string min;
       string max;
       word=0;
       n=0;

       while( cin >> sentence)
              {
                  word++;
                  n += sentence.size();
                  
                if(sentence.size() >= max.size())
                  {
                      max = sentence;
                  }
                  else if (sentence.size() < min.size())
              {
                 min = sentence;
             }
       }
       if (word == 0)
       {
            cout << "You need to write someting" << endl;
       }
       else
       {
             cout << "The text got " << word << " words." << 
             "The shortest word was \"" << min << "\" = " << min.size() <<  
             "The longest word was \"" << max << "\" = " << max.size() << 
             "The average value is " << n / word <<  endl;
       }
       return 0;
}

Lines 16 - 29 - The indentation here is really bad, so the code is hard to follow.

How do you get out of the while loop?

Lines 16 - 29 - The indentation here is really bad, so the code is hard to follow.

How do you get out of the while loop?

Sorry about that. I dont get out from the loop. ^^
Im stuck and need some help fix this.

Have a sentinel value that the person enters when they're done with the input. For example:

string sentence;

do
{
  cout << "Enter a sentence : Enter 'done' when finished : ";
  cin >> sentence;
   // do whatever you want in here
}
while (sentence.compare ("done") != 0);

// place more code here
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.