#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
usingnamespace std;
int main()
{
char c, response;
int Uc = 0, Dc=0, Vc=0, Wc=0;
string vowels("aeiouAEIOU");
cout << "I can count the number of upper case, lower case, vowels and words" << endl;
cout << "Enter a sentence: ";
cin.get(c);
while(c != '\n')
{
{
if(isupper(c))
Uc++;
if(isdigit(c))
Dc++;
if(vowels.find(c) != string::npos)
Vc++;
if (isspace(c))
Wc++;
cin.get(c);
}
}
cout << setfill ('.');
cout << " Number of uppercase letters: " << Uc << endl;
cout << " Number of digits: " << Dc << endl;
cout << " Number of vowels: " << Vc << endl;
cout << " Number of words: " << Wc << endl;
cout << "CONTINUE(y/n)? ";
cin >> response;
if (response == 'Y' || response=='y')
{
cout << setfill ('.');
cout << " Number of uppercase letters: " << Uc << endl;
cout << " Number of digits: " << Dc << endl;
cout << " Number of vowels: " << Vc << endl;
cout << " Number of words: " << Wc << endl;
}
elseif (response == 'N'|| response=='n')
{
cout << endl;
}
system("pause");
return 0;
}
This is my output I'm getting:
1 2 3 4 5 6 7 8 9 10 11 12
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Happy 2015 Valentine
Number of uppercase letters: 2
Number of digits: 4
Number of vowels: 5
Number of words: 2
CONTINUE(y/n)? y
Number of uppercase letters: 2
Number of digits: 4
Number of vowels: 5
Number of words: 2
Press any key to continue . . .
This is how it should look like.
1 2 3 4 5 6 7 8 9 10 11 12 13
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Happy 2015 Valentine
Number of uppercase letters: 2
Number of digits: 4
Number of vowels: 5
Number of words: 2
CONTINUE(y/n)? y
Enter a sentence: Prepare Yourself for Exam No 1
Number of uppercase letters: 4
Number of digits: 1
Number of vowels: 8
Number of words: 5
Press any key to continue . . .
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Toaday Is Thu 45 gRa
Number of uppercase letters: 4
Number of digits: 2
Number of vowels: 6
Number of words: 4
CONTINUE(y/n)? y
Enter a sentence: Number of uppercase letters: 4
Number of digits: 2
Number of vowels: 6
Number of words: 4
CONTINUE(y/n)?
When you get the single character asking for Y or N, cin accepts the character input but leaves a newline in the input buffer. When you start the loop, clean out the input buffer. Also, reset your counters.
I did it that way and it sorta worked but for some reason the vowels don't add up correctly, don't get why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
I can count the number of upper case, lower case, vowels and words
Enter a sentence: Happy 2015 Valentine
Number of uppercase letters: 2
Number of digits: 4
Number of vowels: 5
Number of words: 2
CONTINUE(y/n)? y
Enter a sentence: Prepare Yourself for Exam No 1
Number of uppercase letters: 4
Number of digits: 1
Number of vowels: 10
Number of words: 5
CONTINUE(y/n)? N
Press any key to continue . . .