Count Vowel C++



// ===============
bool isVowel( char );
// ============
int main( ) {

int vowel=0,i;
char text;

cout << "Enter a sentences:";

for (i = 0; i <= 10; i++) {
cin.get(text);
if ( text == 'a' ||text == 'e' || text== 'i' ||text == 'o' || text == 'u' || text == 'A' || text == 'E' || text == 'I' || text == 'O' || text == 'U')

vowel++;

cout << vowel << endl;
}
return 0;
} // Function Main ()

// ======================
bool isVowel( char ch) {


if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

return true;
else
return false;
}



This is my result:

Enter a sentences:ieoaso
1
2
3
4
4
5
5


It's right that there is 5 vowel, but how can I output only one number of the result instead of 1 2 3 44 55 ? for example: your sentence has 5 vowel?

Please help me!!
Move
cout << vowel << endl;
to OUTSIDE the loop, so it only happens once.
aww! Thank you so much :):)
because i < 10 so if I enter 4 letters I have to space 6 times to get the output. how can I get the output immediately after I enter the letters?
This is C++; use a proper C++ string.

1
2
3
4
5
6
7
string text;
cin >> text;
for (i = 0; i < text.size(); i++) {
if ( text[i] == 'a' ||text[i] == 'e' || text[i]== 'i' ||text[i] == 'o' || text[i] == 'u' || text[i] == 'A' || text[i] == 'E' || text[i] == 'I' || text[i] == 'O' || text[i] == 'U')
vowel++;
}
cout << vowel << endl;


Thank you so much :):)
hey Ray, which college are you taking this class for?
int main( ) {

int vowelCount = 0 ;
char text;
int n;

Banner ();


cout << "How many letters would you like to enter? ";
cin >> n;
cout << endl;

while (vowelCount <= n ){

cout << "Please letters here: ";
cin >> text;



if (isVowel(text))


vowelCount++;


--n;
cout << endl;

}

cout << "You have " << vowelCount << " vowels here" << endl;

return 0;


This is my first result:

*******************************************

Welcome to my Programming Assignment Six
In this program, I will count your vowel
in your sequence of letters

********************************************
How many letters would you like to enter? 3

Please letters here: a

Please letters here: s

Please letters here: d

You have 1 vowels here
Press any key to continue . . .


This is the second one:

*******************************************

Welcome to my Programming Assignment Six
In this program, I will count your vowel
in your sequence of letters

********************************************
How many letters would you like to enter? 5

Please letters here: u

Please letters here: i

Please letters here: o

You have 3 vowels here
Press any key to continue . . .



The first one seems right. In the second one, it supposes to ask the user enter the letters 5 times, but here it only asks 3 times then execute the loop. Does anyone know why? Please help me, thank you :):)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "stdafx.h"
#include <iostream>


    using namespace std;

//  ================

//  ======================
    bool isVowel(char);
    void Banner ();
//  ======================
//  =============
    int main( ) {
       
        int vowelCount;
        char text;
		int n;
	
        Banner ();
        

        cout << "How many letters would you like to enter? ";
        cin >> n;
		cout << endl;
		
		vowelCount = 0;
		while (vowelCount <= n ){

			cout << "Please enter your letters here: ";
		    cin >> text;
	
            
		
			if (isVowel(text))   
				
				
				vowelCount++;
			    

				--n;
				cout << endl;
		
		}
        
		cout << "You have " << vowelCount << " vowels here" << endl;
         
        return 0;
    } // Function Main ()
//  =====================

//  ========================
    bool isVowel( char ch) {
      
      
      if (ch == 'a' || 
          ch == 'e' || 
          ch == 'i' || 
          ch == 'o' || 
          ch == 'u' || 
          ch == 'A' || 
          ch == 'E' || 
          ch == 'I' || 
          ch == 'O' || 
          ch == 'U')

          return true;
      else 
          return false;
 }

//  ======================

//  ================
    void Banner () {
    cout << "*******************************************" << endl;
    cout << "\nWelcome to my Programming Assignment Six";
    cout << "\nIn this program, I will count your vowel";
    cout << "\nin your sequence of letters" << endl << endl;
    cout << "********************************************";
    cout << endl;


}
I only saw 1 problem, and it was in your while loop.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "stdafx.h"
#include <iostream>
using namespace std;

//  ======================
    bool isVowel(char);
    void Banner ();
//  ======================
//  =============
    int main( ) {
       
        int vowelCount;
        char text;
		int n;
	
        Banner ();

        cout << "How many letters would you like to enter? ";
        cin >> n;
		cout << endl;
		
		vowelCount = 0;
		while (n > 0){   // This loop only needs to use N; 
                                         // N is the # of times you want to loop,
                                         // So when N reaches 0, you are done.

			cout << "Please enter your letter here: ";
		    cin >> text;
		
			if (isVowel(text))   
				vowelCount++;
             --n;
             cout << endl;
		
		}
        
		cout << "You have " << vowelCount << " vowels here" << endl;
         
        return 0;
    } // Function Main ()
//  =====================

//  ========================
    bool isVowel( char ch) {
      
      
      if (ch == 'a' || 
          ch == 'e' || 
          ch == 'i' || 
          ch == 'o' || 
          ch == 'u' || 
          ch == 'A' || 
          ch == 'E' || 
          ch == 'I' || 
          ch == 'O' || 
          ch == 'U')

          return true;
      else 
          return false;
 }

//  ======================

//  ================
    void Banner () {
    cout << "*******************************************" << endl;
    cout << "\nWelcome to my Programming Assignment Six";
    cout << "\nIn this program, I will count your vowel";
    cout << "\nin your sequence of letters" << endl << endl;
    cout << "********************************************";
    cout << endl;


}
I wonder how many days vowels shall be counted?!
I also recommend cleaning your isVowel function a little. You've got a lot of unnecessary spaces.

1
2
3
4
5
6
7
8
9
    bool isVowel( char ch) {
      
      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || 
          ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

          return true;
      else 
          return false;
 }


This is the same as your code, but with less spaces.
Why do you ask that, Vlad?
For your flowchart:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "stdafx.h"
#include <iostream>
using namespace std;

//  ======================
    bool isVowel(char);
    void Banner ();
//  ======================
//  =============
    int main( ) {
       
        int vowelCount;
        char text;
		int n;
	
        Banner ();  // Process (Rectangle) 

        cout << "How many letters would you like to enter? ";
        cin >> n;    // Input (Parallelogram) 
		cout << endl;
		
		vowelCount = 0;  // Process (Rectangle) 

		while (n > 0){      // Decision (Diamond) 

			cout << "Please enter your letter here: ";
		    cin >> text;    // Input (Parallelogram) 
		
			if (isVowel(text))   // Decision (Diamond)
				vowelCount++;  // Process (Rectangle)
             --n; // Process (Rectangle)
             cout << endl;
		
		}
        
		cout << "You have " << vowelCount << " vowels here" << endl;
              // Output (Parallelogram) 
        return 0;
    } // Function Main ()
//  =====================

//  ========================
    bool isVowel( char ch) {
      
      
      if (ch == 'a' ||  // Decision (Diamond) 
          ch == 'e' || 
          ch == 'i' || 
          ch == 'o' || 
          ch == 'u' || 
          ch == 'A' || 
          ch == 'E' || 
          ch == 'I' || 
          ch == 'O' || 
          ch == 'U')

          return true;  // Process (Rectangle) 
      else 
          return false; // Process (Rectangle) 
 }

//  ======================

//  ================
    void Banner () {
    cout << "*******************************************" << endl;
    cout << "\nWelcome to my Programming Assignment Six";
    cout << "\nIn this program, I will count your vowel";
    cout << "\nin your sequence of letters" << endl << endl;
    cout << "********************************************";
    cout << endl;   // Output (Parallelogram) 
Last edited on
Topic archived. No new replies allowed.