sql
html
iphone
css
xml
python
database
linux
android
regex
mysql
visual-studio
eclipse
perl
algorithm
oracle
cocoa
delphi
api
dom
#include <iostream> using namespace std; int main() { char ch; int vowel_count = 0; int space_count = 0; int other_count = 0; cout << "Enter a string ends with #: " << endl; while(1) { cin.get(ch); if(ch == '#') { break; } if(ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u') { ++vowel_count; } else if(ch == ' ') { ++space_count; } else { ++other_count; } } cout << "Vowels: " << vowel_count << endl; cout << "White spaces: " << space_count << endl; cout << "Other: " << other_count << endl; return 0; }
No arrays
This line
if (character == 'A' || 'E' || 'I' || 'O' || 'U');
Is not doing what you think. It will always return true.
you need
if (character == 'A' || character == 'E' || character == 'I' || character == 'O' || character =='U')
and remove the semicolon as well at the end of that line
You can check for whitespace the exact same way. Common whitespace characters are space (' '), and horizontal tab ('\t'). Less-common are newline ('\n'), carriage return ('\r'), form feed ('\f') and vertical tab ('\v').
' '
'\t'
'\n'
'\r'
'\f'
'\v'
You can also use isspace from ctype.h.
isspace
ctype.h
Here:
while (cin >> character && character != '#')
You are skipping all white space. To prevent the operator >> from skiiping white space you need to explicitly specify this with the noskipws modifier.
while(std::cin >> std::noskipws >> character && character != '#')
Alternatively the same affect can be achieved with get
while(std::cin.get(character) && character != '#')
Next you are reading more characters outside the loop condition.
cin.get(character);
You already have a value in the variable 'character'. So remove both of these. The next iteration of the loop (in the while condition) will get the next character (as it is executed before the loop is entered).
Then fix you test as Tim pointed out. You can then add another test for white space with:
if (std::isspace(character)) // Note #include <cctype> { /* STUFF */ }