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 ;
}
|