Dec 4, 2013 at 1:31pm UTC
Write a value-returning function, isVowel( ), that returns the value true if a given character
is a vowel and otherwise returns false. Using function isVowel( ), write a program that
prompts the user to input a sequence of characters and outputs the number of vowels. ?
#include <iostream>
using namespace std;
bool isvowel()
{
char x ;
{
if ((x=='a' )||(x == 'u') || (x=='e') || (x=='o')|| (x== 'i') )
return true ;
else
return false ;
}
}
int main()
{
int NumOfVowels=0 ;
char x ;
bool flag;
cout << "Enter a sequance ?" ;
cin >> x ;
for ( flag==false ; flag==false ; flag++ )
{
if ((x=='a' )||(x == 'u')||(x=='e')||(x=='o')||(x== 'i') )
{
isvowel() ;
NumOfVowels++;
}
}
cout << NumOfVowels ;
return 0 ;
}
please help me when i enter a sequence of characters it reads the first character only and give me one
i dont know where is the problem !!!
i use code blocks
thank you
Dec 4, 2013 at 3:08pm UTC
Hi Teemo,
First, initialize your variables because you are using them before give them a value and revise the parameters of your isvowel() function.
You should use string instead of char and use a for-loop to go through the string to count how many vowels the user entered.
Dec 4, 2013 at 3:30pm UTC
thank you mr-krugle ,
but when i use string it gives me an error in
if ((x=='a' )||(x == 'u') || (x=='e') || (x=='o')|| (x== 'i') )
Dec 4, 2013 at 3:39pm UTC
i already did this function
the problem is how to check each character
because i think it checks the first character only and give me 1 when the first character is vowel
Dec 4, 2013 at 3:59pm UTC
this is the code now
#include <iostream>
using namespace std;
bool isvowel(char x) ;
int main()
{
int NumOfVowels=0 ;
char x ;
bool flag;
cout << "Enter a sequance ?" ;
cin >> x ;
for ( flag=false ; flag==false ; flag++ )
{
if ((x=='a' )||(x == 'u')||(x=='e')||(x=='o')||(x== 'i') )
{
isvowel(x) ;
NumOfVowels++;
}
}
cout << NumOfVowels ;
return 0 ;
}
bool isvowel(char x)
{
if ((x=='a' )||(x == 'u')||(x=='e')||(x=='o')||(x== 'i') )
return true ;
else
return false ;
}
Dec 4, 2013 at 4:04pm UTC
You do not need this check:
if ((x=='a' )||(x == 'u' )||(x=='e' )||(x=='o' )||(x== 'i' ) )
twice in your code. Remove it from main() and keep it in your isvowel method.
edit: i've just properly read your question.
1. read in a STRING not a char. this is your character sequence.
2. iterate over this string and call isvowel() on each character.
You have asked the user for a sequence but are storing only a character.
Last edited on Dec 4, 2013 at 4:06pm UTC
Dec 4, 2013 at 4:29pm UTC
thank you mutexe
#include <iostream>
#include <string>
using namespace std;
bool isvowel(string x ) ;
int main()
{
int NumOfVowels=0 ;
string x ;
bool flag;
cout << "Enter a sequance ?" ;
getline ( cin , x ) ;
for ( flag==false ; flag==false ; flag++ )
{
isvowel(x);
NumOfVowels++;
}
cout << NumOfVowels ;
return 0 ;
}
bool isvowel(string x )
{
if ((x =='a' )||(x == 'u')||(x=='e')||(x=='o')||(x== 'i') )
return true ;
else
return false ;
}
i searched for iteration of strings and i found that , we didnt study that method yet , so i cant use it now for the homework
and i still have an error in
if ((x =='a' )||(x == 'u')||(x=='e')||(x=='o')||(x== 'i') )
helppp me guys
Dec 4, 2013 at 4:43pm UTC
Nearly there mate :)
Your isVowel method should still take a char.
give me a sec i'll knock something up.
Dec 4, 2013 at 5:18pm UTC
thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaank yooooou finally it works
Dec 4, 2013 at 6:28pm UTC
No worries dude. You were nearly there anyway.