isvowel function - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: isvowel function

  1. #16
    Join Date
    Apr 1999
    Posts
    27,423

    Re: isvowel function

    Quote Originally Posted by psfign View Post
    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.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,423

    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.

  3. #18
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: isvowel function

    Quote Originally Posted by psfign View Post
    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.

  4. #19
    Join Date
    Jan 2013
    Posts
    71

    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.

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: isvowel function

    Quote 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).

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center