Using members of <iostream>, finding digits and vowels?

I'm supposed to output the number of uppercase letters, digits, and vowels. It's not running correctly and how do I get the vowels of the sentence?

So far I have this:
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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	int Uc = 0, Dc = 0, Vc=0;
	int i=0;
	char c; 
	
	cout << "Enter a sentence: ";
	cin >> c; 
	cout << endl; 

      while (c != 0)
     {
        if (c >= 'A' && c <= 'Z') {
         Uc++;
          i++;
		}           
        else if(c >= '0' && c <= '9') {     
            Dc++;
             i++;
		}
	  }

	  cout << setfill ('.');
	  cout << "Number of uppercase letters" << right << setw(10) << Uc << endl; 
	  cout << "Number of digits" << right << setw(10) << Dc << endl;  
	  cout << "Number of vowels" << right << setw(10) << Vc << endl;  

	//end program
	system("pause");
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
{
	char c = getchar();
	string vowels("aeiouAEIOU");
	if (vowels.find(c) != string::npos)
	{
		cout << c << " is a vowel" << endl;
	}
	else
	{
		cout << c << " is not a vowel" << endl;
	}
}
I'm still getting errors from other stuff

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
45
46
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	int Uc = 0, Dc = 0, Vc=0;
	int i=0;
	char c = getchar();
	string vowels("aeiouAEIOU");
	
	cout << "Enter a sentence: ";
	cin.get(c);  

      while (c != '\n')
     {
        if (c >= 'A' && c <= 'Z') {
         Uc++;
          i++;
		}           
        else if(c >= '0' && c <= '9') {     
            Dc++;
             i++;
		}

		 if (vowels.find(c) != string::npos)
	{
		cout << c << " is a vowel" << endl;
	}
	else
	{
		cout << c << " is not a vowel" << endl;
	}
	  }

	  cout << setfill ('.');
	  cout << "Number of uppercase letters" << right << setw(10) << Uc << endl; 
	  cout << "Number of digits" << right << setw(10) << Dc << endl;  
	  cout << "Number of vowels" << right << setw(10) << Vc << endl;  

	//end program
	system("pause");
	return 0;
}
I suppose you start by getting a sentence. You only extract a single character from your input stream. (Well, that's true of the original code. In the latest code you extract two characters and discard the first.)
Last edited on
How would I fix something like this:
It is without the is a vowel or is not a vowel, still doesn't work but how do I fix it?

This is what it outputs:
1
2
Today Is Thr Jan, 2015
Enter a sentence:


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 <iomanip> 
#include <string>

using namespace std;

int main()
{
	int Uc = 0, Dc = 0, Vc=0;
	int i=0;
	char c = getchar();
	string vowels("aeiouAEIOU");
	
	cout << "Enter a sentence: ";
	cin.get(c);  

      while (c != '\n')
     {
        if (c >= 'A' && c <= 'Z') {
         Uc++;
          i++;
		}           
        else if(c >= '0' && c <= '9') {     
            Dc++;
             i++;
		}

		else if(vowels.find(c) != string::npos)
		{
		Vc++; 
		i++; 
	    }
	  }

	  cout << setfill ('.');
	  cout << "Number of uppercase letters" << right << setw(10) << Uc << endl; 
	  cout << "Number of digits" << right << setw(10) << Dc << endl;  
	  cout << "Number of vowels" << right << setw(10) << Vc << endl;  

	//end program
	system("pause");
	return 0;
}
Last edited on
Dkob1 wrote:
How would I fix something like this:
It is without the is a vowel or is not a vowel, still doesn't work but how do I fix it?
cire wrote:
I suppose you start by getting a sentence.

You can do that a char at a time if you desire, but to do that you still need to extract more than one or two char's total.

You might start by forgetting about vowels and consonants and simply outputting the chars as you would process them. Once you've got that down, build on it.
I did it a little cleaner but why is there an error for else if part?

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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;


int main()
{
	char c;
	int Uc = 0, Dc=0;

	cout << "Enter a sentence: ";
	cin.get(c);
	while(c != '\n')
	{
		if(isupper(c))  
			Uc++;  
		cin.get(c);
               {
		else if(isdigit(c)) 
			Dc++; // 
		cin.get(c);
                }
	}
	 cout << setfill ('.');
	 cout << "Number of uppercase letters" << right << setw(10) << Uc << endl; 
	 cout << "Number of digits" << right << setw(10) << Dc << endl; 

	 system("pause");
	return 0;
}
Last edited on
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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	char c;
	int Uc = 0, Dc=0, Vc=0;
	string vowels("aeiouAEIOU");

	cout << "Enter a sentence: ";
	cin.get(c);
	while(c != '\n')
	{
		if(isupper(c))  
			Uc++;  
		cin.get(c);
		{
		if(isdigit(c)) 
			Dc++; // 
		cin.get(c);
		}
		{
		if(vowels.find(c) != string::npos)
			Vc++;
		cin.get(c);
		}
	}
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters" << right << setw(10) << Uc << endl; 
	 cout << "  Number of digits" << right << setw(20) << Dc << endl; 
	 cout << "  Number of vowels" << right << setw(20) << Vc << endl;

	 system("pause");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//This is what I have

Enter a sentence: Today Is Thr Jan, 2015


  Number of uppercase letters.........3
  Number of digits...................1
  Number of vowels...................1
Press any key to continue . . .

//The output should look like this:

Enter a sentence: Today Is Thr Jan, 2015
  Number of uppercase letters.........4
  Number of digits....................4
  Number of vowels....................4
Press any key to continue . . .
I did this but it's still not giving me the right amount of uppercase letters or number of digits correctly ??? Any solutions???

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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	char c;
	int Uc = 0, Dc=0, Vc=0;
	string vowels("aeiouAEIOU");

	cout << "Enter a sentence: ";
	cin.get(c);
	while(c != '\n')
	{
		{
		if(isupper(c))  
			Uc++;  
		if(isdigit(c)) 
			Dc++; 
		cin.get(c);
		}
		{
		if(vowels.find(c) != string::npos)
			Vc++;
		cin.get(c);
		}
	}
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters" << right << setw(10) << Uc << endl; 
	 cout << "  Number of digits" << right << setw(20) << Dc << endl; 
	 cout << "  Number of vowels" << right << setw(20) << Vc << endl;

	 system("pause");
	return 0;
}
Why are you extracting a character twice within the loop? You test one character to see if it's uppercase or a digit, then you check another to see if it's a vowel.
so I should separate while loops?
cire wrote:
Why are you extracting a character twice within the loop?
I tried using else if and it gave me an error.
I don't get what you mean "extracting a character twice within the loop", I'm a beginner sorry.
I tried this way and it gave me 4, 0, 0.

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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	char c;
	int Uc = 0, Dc=0, Vc=0;
	string vowels("aeiouAEIOU");

	cout << "Enter a sentence: ";
	cin.get(c);
	while(c != '\n')
	{
		{ if(isupper(c))  
			Uc++;       
		  cin.get(c);   }
	}
	while(c != '\n') 
	{
		{ if(isdigit(c)) 
		    Dc++;   
		    cin.get(c);   }
	}
	while(c != '\n') 
	{
		{ if(vowels.find(c) != string::npos)
			Vc++;
		    cin.get(c);   }
	}
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters" << right << setw(10) << Uc << endl; 
	 cout << "  Number of digits" << right << setw(21) << Dc << endl; 
	 cout << "  Number of vowels" << right << setw(21) << Vc << endl;

	 system("pause");
	return 0;
}
Not understanding, please help???

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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	char c;
	int Uc = 0, Dc=0, Vc=0;
	string vowels("aeiouAEIOU");

	cout << "Enter a sentence: ";
	cin.get(c);
	while(c != '\n')
	{
		{
		if(isupper(c))  
			Uc++;  
		else if(isdigit(c)) 
			Dc++; 
		cin.get(c);
		}
		{
		if(vowels.find(c) != string::npos)
			Vc++;
		cin.get(c);
		}
	}
	 cout << setfill ('.');
	 cout << "  Number of uppercase letters" << right << setw(10) << Uc << endl; 
	 cout << "  Number of digits" << right << setw(22) << Dc << endl; 
	 cout << "  Number of vowels" << right << setw(22) << Vc << endl;

	 system("pause");
	return 0;
}
Last edited on
You mean like this for this part?????????? :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    while(c != '\n')
    {
        {
        if(isupper(c))  
            Uc++;  
        if(isdigit(c)) 
            Dc++; 
        cin.get(c);
        }
        {
        if(vowels.find(c) != string::npos)
            Vc++;
        cin.get(c);
        }
    }
When you do
cin.get()
, you're only reading a single character. You could try reading the entire sentence first, but if yu really want to do it one character at a time then do like so:

1
2
3
get a character
process it
repeat until no more characters


Compare with this :
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
45
46
#include <iostream>
#include <cctype>

using namespace std;

bool isvowel(int c)
{
	static const string vowels("aeiouAEIOU");
	if (vowels.find(c) != string::npos)
	{
		return true;
	}
	
	return false;
}

int main()
{
	int vowels = 0;
	int capitals = 0;
	int digits = 0;
	int c = -1;
	while (c != '\n')
	{
		c = cin.get();
		
		if (isalpha(c))
		{
			if (isupper(c))
			{
				++capitals;
			}
			
			if (isvowel(c))
			{
				++vowels;
			}
		}
		else if (isdigit(c))
		{
			++digits;
		}
	}
	
	cout << "\nThere are:\n" << vowels << " vowels.\n" << capitals << " capital letters.\n" << digits << " digits." << endl;
}
somewhat understand it I guess.
Topic archived. No new replies allowed.