-
March 30th, 2013, 02:26 PM
#1
isvowel function
Im trying to create a code that simply counts the number of vowels imputed by the user. Here's where I am.
Code:
#include <iostream>
using namespace std;
bool isVowel(char ch);
int main()
{
int count=0;
char character;
int vowelcount=0;
cout << "Enter a sentence"<< endl;
cout << endl;
while(character !='!')
{
cin >> character;
if(isVowel(character)==true)
{
vowelcount++;
cout << "Vowels are " << vowelcount << endl;
}
if(vowelcount !=0)
{
cout << endl;
}
}
cout << endl;
}
bool isVowel(char ch)
{
if ('A' == ch || 'a' == ch ||
'E' == ch || 'e' == ch ||
'I' == ch || 'i' == ch ||
'O' == ch || 'o' == ch ||
'U' == ch || 'u' == ch)
{
}
}
-
March 30th, 2013, 02:32 PM
#2
Re: isvowel function
You seem to be close. What's stopping you from completing the isVowel function?
Also, you need to indent your code properly.
-
March 30th, 2013, 02:35 PM
#3
Re: isvowel function
I'm not sure. It isn't counting them properly. If I put in a 3 letter word, it displays:
Vowels 1
Vowels 2
Vowels 3
I forgot to indent as I was working, my bad
-
March 30th, 2013, 02:38 PM
#4
Re: isvowel function
Err... I implied that your isVowel function is incomplete: you forgot to return a value from it.
-
March 30th, 2013, 02:42 PM
#5
Re: isvowel function
lol, thank you. I see what youre saying.
-
March 30th, 2013, 02:42 PM
#6
Re: isvowel function
As it stands, your program shouldn't compile as isVowel(..) is supposed to return a type bool but the function doesn't have a return statement! Also, your main function should return a value as well (usually 0).
I forgot to indent as I was working,
Indenting of code as you enter/change it should become second nature. It makes reading and understanding it so much easier - especially when it comes to trying to work out why a program doesn't function as expected.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 30th, 2013, 02:55 PM
#7
Re: isvowel function
Originally Posted by 2kaud
As it stands, your program shouldn't compile as isVowel(..) is supposed to return a type bool but the function doesn't have a return statement!
It only results in undefined behaviour, hence a compiler might still compile the program. However, it probably would result in a warning, so... psfign: consider increasing the warning level of your compiler.
Originally Posted by 2kaud
Also, your main function should return a value as well (usually 0).
That's optional since a missing return statement for the global main function would be as if return 0; was present at the end.
-
March 30th, 2013, 02:59 PM
#8
Re: isvowel function
I don't know how to do the return statement, im lost. Everytime I think im getting better, im not
-
March 30th, 2013, 04:25 PM
#9
Re: isvowel function
Originally Posted by psfign
I don't know how to do the return statement, im lost. Everytime I think im getting better, im not
In this case, as it's a bool function, all you need to do is return the result of the if statement.
Code:
bool isVowel(char ch)
{
return 'A' == ch || 'a' == ch ||
'E' == ch || 'e' == ch ||
'I' == ch || 'i' == ch ||
'O' == ch || 'o' == ch ||
'U' == ch || 'u' == ch;
}
-
March 30th, 2013, 03:13 PM
#10
Re: isvowel function
Originally Posted by psfign
I don't know how to do the return statement
Don't panic; review your learning material on functions. You will surely encounter a few examples of return statements there.
-
March 30th, 2013, 03:15 PM
#11
Re: isvowel function
I don't know how to do the return statement
To return a value from a function, the return statement is used. The format is
return val;
where val is the data you want to return and has the same type as that defined with the function definition. In your case isVowel(.) needs to return a value of type bool which is true if ch is a vowel and false otherwise.
This may help
http://www.learncpp.com/cpp-tutorial...-at-functions/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 30th, 2013, 06:11 PM
#12
Re: isvowel function
ive got that part essentially, but it created an unexpected error.
23 undefined reference to `isVowel(char)'
Code:
#include <iostream>
using namespace std;
bool isVowel(char ch);
char ch=' ';
int main()
{
int count=0;
char character;
int vowelcount=0;
cout << "Enter a sentence"<< endl;
cout << endl;
while(character !='!')
{
cin >> character;
if(isVowel(character)==true)
{
vowelcount++;
cout << "Vowels are " << vowelcount << endl;
return 0;
}
if(vowelcount !=0)
{
cout << endl;
return 0;
}
}
bool isVowel(char ch);
{
if ('A' == ch || 'a' == ch ||
'E' == ch || 'e' == ch ||
'I' == ch || 'i' == ch ||
'O' == ch || 'o' == ch ||
'U' == ch || 'u' == ch)
cout << endl;
}
}
-
March 30th, 2013, 07:41 PM
#13
Re: isvowel function
You need to figure out meaningful indentation.
Code:
bool isVowel(char ch);
{
if ('A' == ch || 'a' == ch ||
'E' == ch || 'e' == ch ||
'I' == ch || 'i' == ch ||
'O' == ch || 'o' == ch ||
'U' == ch || 'u' == ch)
cout << endl;
}
Lose the ;
Your function still doesn't return a value. I showed you what to do in a previous post. It certainly shouldn't cout anything.
Last edited by GCDEF; March 30th, 2013 at 07:43 PM.
-
March 30th, 2013, 07:45 PM
#14
Re: isvowel function
I'd work on fixing your indenting. it will make reading your code much easier.
-
March 30th, 2013, 09:10 PM
#15
Re: isvowel function
Ok, I tried to indent better. I also worked on the return statement, but it's not counting properly. Here's what I have so far. I cant see why it always only counts 1 vowel.
Code:
#include <iostream>
using namespace std;
bool isVowel(char ch);
char ch = ' ';
int main()
{
int count=0;
char character;
int vowelcount = 0;
cout << "Enter a sentence"<< endl;
cin >> character;cin.get(character);
cout << endl;
while(character !='!')
{
cin >> character;
if(isVowel(character) == true)
{
vowelcount++;
cout << "Vowels are " << vowelcount << endl;
return 0;
}
if(vowelcount !=0)
{
cout << endl;
return 0;
}
}
}
bool isVowel(char ch)
{
return 'A' == ch || 'a' == ch ||
'E' == ch || 'e' == ch ||
'I' == ch || 'i' == ch ||
'O' == ch || 'o' == ch ||
'U' == ch || 'u' == ch;
return true;
}
Last edited by psfign; March 30th, 2013 at 09:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|