Please can someone Help me with my first function program

I need to Create a function inVowel to count the number of vowels the user entered.Can someone help me find what is wrong that keep This program displaying "Please enter a character multiple times when I run the program??

Please enter a character: going to school
Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: Please enter a character: #
Of the 14 characters you entered 5 were vowels.
logout
Saving session...

[Process completed]
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
  #include <iostream>
#include <string>


using namespace std;



void isVowel();

int main ()
{
    char userChar;
    int vowelCount = 0;
    int charCount = 0;
    
    
    cout << "This program will check the letters you input to check for vowels." << endl;
    cout << "Enter # after you Last Word " << endl;

    
    do {
        cout << "Please Enter a Text and Enter '#' to count the vowels "<< endl;
        
        cin >> userChar;
        
        charCount++;
        
        if (isVowel (userChar))
            
        vowelCount++;

            cout << "Of the " << charCount << " characters you entered " << vowelCount << " were vowels." << endl;
    return 0;


}

void isVowel(char userChar);
do {
    if ((x =='a' )||(x == 'u')||(x=='e')||(x=='o')||(x== 'i')||(x =='A' )||(x == 'U')||(x=='E')||(x=='O')||(x== 'I') )
    
            return true ;
        else
            return false ;
    


}
Line 25: How much text do you think the user can enter? Hint: userChar is a single character.

Line 9: Your function prototype says isVowel() does not accept any arguments. Your implementation does not agree.

Line 9,39,43,45: isVowel is declared to return void (nothing). You can't return a bool value.

Line 39: The ; makes this a function prototype, not a function definition.

Line 29: You can't test a void function in an if statement.

Line 22,40: Where is the end of your do/while loop? Regarding line 40, why are even attempting to loop here?

Line 41: x is undefined.



This should never have compiled successfully for you.
Thanks for the input!
I've made some changes :

#include <iostream>
#include <string>


using namespace std;

bool isvowel(char x );


int main()
{
char isVowel;

int count = 0;

cout << "Enter a Letter to count the Vowels : "<< endl;

cin.get (isVowel);

cout <<"Vowels: " << endl;



while ( isVowel != '\n')
{
cin.get (isVowel);
switch(toupper(isVowel))
{
case 'A': cout << isVowel <<" , ";
count++;
break;
case 'E': cout << isVowel <<" , ";
count++;
break;
case 'I': cout << isVowel <<" , ";
count++;
break;
case 'O': cout << isVowel <<" , ";
count++;
break;
case 'U': cout << isVowel <<" , ";
count++;
break;
case 'a': cout << isVowel <<" , ";
count++;
break;
case 'e': cout << isVowel <<" , ";
count++;
break;
case 'i': cout << isVowel <<" , ";
count++;
break;
case 'o': cout << isVowel <<" , ";
count++;
break;
case 'u': cout << isVowel <<" , ";
count++;
break;
}
}

cout << "\n\nThere were a Total of " <<count << " vowels in your sentence. \n\n ";


return 0;
}

bool isvowel(char x )
{
if ((x =='a' )||(x == 'e')||(x=='i')||(x=='o')||(x== 'u'||x =='A' )||(x == 'E')||(x=='I')||(x=='O')||(x== 'U') )

return true ;
else
return false ;

}
Line 18: What's the point of this cin.get()? It's going to get overlaid by line 26. Note that if you remove line 18, isVowel is going to be uninitialized at line 24.

Line 44-56: What is the point of the lower case cases in you switch statement. You do a toupper(), so the lower case values can never occur.

Line 26: You're inputting one character at a time not a sentence.

Line 68: You never call your isvowel() function.

Line 27-59: Your switch statement can be replaced by the following:
1
2
3
4
5
 
  if (isvowel(toupper(isvowel)))
  {  cout << isVowel << ",";
      count++;
  }


Your logic should really be to read in an entire sentence into a std::string at line 18 using getline(), then at line 24 iterate through the string one character at a time.

I'm not thrilled with isvowel as a function name and isVowel as a variable name. This is confusing to the reader.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

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
#include <iostream>
#include <string>
using namespace std;

bool isvowel (char x);

int main()
{   string sentence;
    char c;
    int count = 0;

    cout << "Enter a sentence to count the Vowels : "<< endl;
    getline (cin, sentence); 
    cout << "Vowels: " << endl;
    for (size_t i=0; i<sentence.size(); i++)
    {   c = toupper(sentence[i]);
        if (isvowel(c)) 
        {   cout << c <<" , ";
            count++;
        }
    }
    cout << "\n\nThere were a Total of " <<count << " vowels in your sentence. \n\n ";
    return 0;
}
//  Assumes character has already been upshifted
bool isvowel (char x)
{   if ((x =='A')||(x == 'E')||(x=='I')||(x=='O')||(x== 'U'))
        return true ;
    else
        return false ;
}
Last edited on
thank you!
Topic archived. No new replies allowed.