CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Jan 2013
    Posts
    71

    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)
     {
    
    
     }
     
     }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

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

  3. #3
    Join Date
    Jan 2013
    Posts
    71

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: isvowel function

    Err... I implied that your isVowel function is incomplete: you forgot to return a value from it.
    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

  5. #5
    Join Date
    Jan 2013
    Posts
    71

    Re: isvowel function

    lol, thank you. I see what youre saying.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: isvowel function

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

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

  8. #8
    Join Date
    Jan 2013
    Posts
    71

    Re: isvowel function

    I don't know how to do the return statement, im lost. Everytime I think im getting better, im not

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: isvowel function

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

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: isvowel function

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

  12. #12
    Join Date
    Jan 2013
    Posts
    71

    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;
     }
     }

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  14. #14
    Join Date
    Aug 2009
    Posts
    440

    Re: isvowel function

    I'd work on fixing your indenting. it will make reading your code much easier.

  15. #15
    Join Date
    Jan 2013
    Posts
    71

    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.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured