C++ Looping Program, counting digits and words

This program is suppose to count the number of digits, words ( i.e Abc, fish...) upper case letters, and longest word length inputted.
I am having trouble properly writing the code to identify word count and number of letters inputted. When I run it only the number of digits seen seems to work and I do not know how to make the program count the longest word inputted.Am I on the right track? Any help is greatly appreciated!

My code:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;

bool die(const string & msg);

int main(){
char s = '0';
char sentinel = '.';
int countWords = 0;
int countDigits = 0;
int countLetters = 0;
int countUppers = 0;
cout << "Enter your string: " << endl;
while (cin >> s){
if (isdigit(s)){
countDigits++;
}
else if (isupper(s)){
countUppers++;
}
else if (isspace(s)){
countWords++;
}

cout << "# of Digits: " << countDigits << endl;
cout << "# of Uppers: " << countUppers << endl;
cout << "# of Words: " << countWords << endl;
cout << "Longest word length" << countLetters << endl;
}

system("PAUSE");
}
/*getline(cin, s);
const int size = s.length();
cout << "The total number of characters entered is: " << size << endl;*/


bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
First post code in code tags (the "<>" on the toolbar)
Second, your code is going to print count each time it reads a character
Third, avoid using platform specific extensions such as system

Modified you code to calculate and print the statistics properly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;

bool die(const string & msg);

int main(){
        std::string s;
        char sentinel = '.';
        int countWords = 0;
        int countDigits = 0;
        int countLetters = 0;
        int countUppers = 0;

        cout << "Enter your string: " << endl;
        cin >> s;

        for (auto c: s){
                if (isdigit(c)){
                        countDigits++;
                }
                else if (isupper(c)){
                        countUppers++;
                }
                else {
                        ++countLetters;
                }

        }

        cout << "# of Digits: " << countDigits << endl;
        cout << "# of Uppers: " << countUppers << endl;
        cout << "# of Letters: " << countLetters << endl;
/*      cout << "# of Words: " << countWords << endl;
        cout << "Longest word length" << countLetters << endl;*/
}

bool die(const string & msg){
        cout << "Fatal error: " << msg << endl;
        exit(EXIT_FAILURE);
}


Now you have to modify to get all entered words from cin and count the words

If you do not have C++11 capable compiler then upgrade your Visual Studio to latest Express edition.
Topic archived. No new replies allowed.