English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

For some reason it takes the string but does not show any result for the string can anybody help?
#include
#include
#include
#include


using namespace std;
bool IsVowel(char c);


int main(int argc, char *argv[])
{
int VowelCount = 0;
cout << "Enter a string of text: " << endl << flush;
char c;

while (cin.get(c)) {
if (IsVowel(c))
VowelCount++;
}


cout << endl << endl;
cout << "There were " << VowelCount << " vowels" << endl;
return 0;
}



bool IsVowel(char c)
{
c = toupper(c);


switch (c)
{

case 'A': case 'E': case 'I':
case 'O': case 'U': case 'Y':
return true;
}

}

2007-03-18 14:00:34 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

Of course is in C++

2007-03-18 14:10:00 · update #1

2 answers

use std::string str;
cin >> str;
Now search for vowels in the string

2007-03-19 00:37:27 · answer #1 · answered by Anonymous · 0 1

Your function IsVowel always returns true, so it should return by default false unless one of the vowels appear;
I also modified your code where you read the input;
Instead of attempting to read charcter by character, which is also implemented wrong in your code unless you use the non-standard conio "getch()".
So instead I replaced that part with a simple code which reads a string and then iterates through the string to count the vowels.

#include
#include
#include
#include

using namespace std;
bool IsVowel(char c);

int main(int argc, char *argv[])
{
int VowelCount = 0;
cout << "Enter a string of text: ";
string str;
getline(cin,str);
for (unsigned int i = 0; i < str.size(); i++){
if (IsVowel(str.at(i)))
VowelCount++;
}
cout << endl << endl;
cout << "There were " << VowelCount << " vowels" << endl;
system("pause");
return 0;
}

bool IsVowel(char c)
{
c = toupper(c);
switch (c)
{
case 'A': case 'E': case 'I':
case 'O': case 'U': case 'Y':
return true;
}
return false;
}

2007-03-18 21:14:54 · answer #2 · answered by Coosa 2 · 1 0

fedest.com, questions and answers