-
March 30th, 2013, 10:07 PM
#16
Re: isvowel function
Originally Posted by psfign
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.
Are you debugging your code, or just placing statements hoping the program will work?
If a program doesn't behave correctly, then it's time to learn how to debug your program. Debugging a program is part and parcel of learning how to write a program.
No one writes perfect programs the first time. Your compiler more than likely has a debugger, where you can step through the program one line at a time, watching variables and seeing how the program flows. If you did that, not only would you know what's wrong, you will learn how your program is doing what it's doing, i.e. program flow.
At the very least, you should have output the value of variables or some other statements telling you what your program is doing at certain times.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 30th, 2013 at 10:09 PM.
-
March 30th, 2013, 10:13 PM
#17
Re: isvowel function
For example:
Code:
if(isVowel(character) == true)
{
vowelcount++;
cout << "Vowels are " << vowelcount << endl;
return 0;
}
if(vowelcount !=0)
{
cout << endl;
return 0;
}
If you used the debugger, you would see that when the return is executed, the program ends. Then you would learn right away that the return statement did something you didn't expect, and that is to end the program. You then take out your C++ book and go to the chapter concerning "return" and the main() program. Then you see that a return from main() ends the program. All learned by starting to use the debugger.
So how do you fix it? You get rid of the return statement and place it in a more logical place in your program, e.g. where you really and truly want the program to end.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 30th, 2013 at 10:17 PM.
-
March 31st, 2013, 06:38 AM
#18
Re: isvowel function
Originally Posted by psfign
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:
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;
}
Now you're just guessing. As Paul said, read up on what return does. I showed you exactly how to write that function.
Last edited by GCDEF; March 31st, 2013 at 06:42 AM.
-
March 31st, 2013, 11:25 AM
#19
Re: isvowel function
I got it.
Code:
//Author: Kenneth Watkins
#include <stdlib.h>
#include <iostream>
#include <string>
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.get(character);
cout << endl;
while(character != '\n')
{
if(isVowel(character))
{
vowelcount++;
cin.get(character);
}
else
{
cin.get(character);
}
}
cout << "Vowels are " << vowelcount << endl;
}
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;
}
Last edited by psfign; March 31st, 2013 at 11:27 AM.
-
March 31st, 2013, 12:43 PM
#20
Re: isvowel function
Great! You can simplify the code somewhat though. You have cin.get(character) twice within the if statement when you could just do it once. As this statement is executed irrespective of the result of the if statement, you can remove the else part and put the cin.get(..) after the if. This would give
Code:
if (isVowel(character))
{
vowelcount++;
}
cin.get(character);
You are also doing cin.get(..) twice, once before the loop and once in the loop. If a do loop is used rather than a while then cin.get(..) is only needed once as in
Code:
cout << "Enter a sentence" << endl;
do {
cin.get(character);
if (isVowel(character)) {
vowelcount++;
}
} while (character != '\n');
cout << endl << "Vowels are " << vowelcount << endl
You can even remove the if statement altogther as isVowel returns a bool which can added to an int (true equates to 1 and false to 0) giving the final version as
Code:
#include <iostream>
using namespace std;
bool isVowel(char ch);
int main()
{
char character;
int vowelcount = 0;
cout << "Enter a sentence"<< endl;
do {
vowelcount += isVowel(character = cin.get());
} while (character != '\n');
cout << endl << "Vowels are " << vowelcount << endl;
}
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;
}
Have a good look at the do loop and try to understand how it works.
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 31st, 2013, 12:55 PM
#21
Re: isvowel function
Originally Posted by 2kaud
If a do loop is used rather than a while then cin.get(..) is only needed once
I think a while loop is fine. It is a matter of conditions, e.g.,
Code:
while (cin.get(character) && character != '\n')
{
if (isVowel(character))
{
++vowelcount;
}
}
Note that if you use cin.get() instead (i.e., no argument), then this is the version of get that returns an int, not a char, since it could return EOF (or some equivalent).
Originally Posted by 2kaud
You can even remove the if statement altogther as isVowel returns a bool which can added to an int (true equates to 1 and false to 0)
You certainly could, but personally this implicit conversion makes me queasy. I would rather be explicit.
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
|