Thread: ...still need some help!

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    Question ...still need some help!

    OK, I think that I am getting closer to completing this simple program which identifies whether or not a letter entered by the user is a vowel or not by using a function. The program successfully returns whether or not it is a vowel however it prints the lines in a disorderly fashion and it also shows some smiley face after the line "the letter you have entered is: " If you guys could help a NOOB clean this up it would be much appreciated. I am amazed how confused this stuff is making me
    Code:
    #include <iostream>
    using namespace std;
    
    char isVowel(char ch);
    
    int main()
    {
    	char letter;
    
    	cout << "Please enter a letter from the alphabet: ";
    	cin >> letter;
    	cout << "the letter you have entered is: "<<isVowel(letter)<<""<<endl;
    	
    	return 0;
    }
    char isVowel(char ch)
    {
    	if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch== 'U'||
    		ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch== 'u')
    		cout << "a Vowel" <<endl;
    	else  
    		cout << "not a vowel" <<endl;
    	return 1;    
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you should use the toupper() or tolower() function.. located in the <cctype> library.. to limit your user entries to either upper or lower case.. instead of listing all vowels in both upper and lower case...


    Code:
    if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch== 'U'||
    		ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch== 'u')
    
                                   cout << "a Vowel" <<endl;


    could be:
    Code:
    toupper(ch);     //turn 'ch' to upper case
    
    if (ch ==  'A' || 'E' || 'I' || 'O' || 'U')
    		
    		cout << "a Vowel" <<endl;

    save yourself a little bit of code
    Last edited by The Brain; 10-12-2004 at 12:30 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int v( int c )
    {
        int d = toupper( c );
    #define C case
        switch( d ){C 'A':C 'E':C 'I':C 'O':C 'U':return 1;}return 0;
    }


    [edit]
    Code:
    int v( int c )
    {
            int d=toupper(c);
            return (d=='A')|(d=='E')|(d=='I')|(d=='O')|(d=='U');
    }

    Parenthesis to stave off warnings...
    [/edit]
    Quzah.
    Last edited by quzah; 10-12-2004 at 12:47 AM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    I've got a lot of research and learning to do, the answer you gave me quzah I understand absolutely none of...except maybe return 0...lol

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quzah was just messing with you its called a macro to replace something in your code with what you defined eg in quzah's case he defined C as case so every where there is a C in your code the compiler replaces it with case
    Woop?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by The Brain
    could be:
    Code:
    toupper(ch);     //turn 'ch' to upper case
    
    if (ch ==  'A' || 'E' || 'I' || 'O' || 'U')
    		
    		cout << "a Vowel" <<endl;
    save yourself a little bit of code
    Actually, no, it couldn't. Try it.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    oi...



    Code:
    if (toupper(ch) ==  'A' || 'E' || 'I' || 'O' || 'U')
    		
    		cout << "a Vowel" <<endl;

    i guess toupper() or tolower() do not modify the variable.. it only returns the upper/lower case version of it's argument.

    learn something new everyday.
    Last edited by The Brain; 10-12-2004 at 01:26 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're still doing it wrong. Look at everything to the right of the first ||. That's all wrong. Put that as-is in a function, and pass it 'D' and watch it say a Vowel.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Quote Originally Posted by quzah
    Code:
            return (d=='A')|(d=='E')|(d=='I')|(d=='O')|(d=='U');
    
    Quzah.


    I think you mean this...

    Code:
    if (toupper(ch) ==  'A' | 'E' | 'I' | 'O' | 'U')


    I am not familiar with the | operator.. but why use this as opposed to the binary || OR operator..

    I think it would work with using the || operators.. i know i have used similar logic before in my programs and it works fine.



    [edit]: I think | appears to be an "exclusive OR" operator
    Last edited by The Brain; 10-12-2004 at 01:39 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Run this:
    Code:
    #include <iostream>
    int main()
    {
        int d = 'D';
        if( d == 'E' || 'F' )
            std::cout << "See now? << std::endl;
        return 0;#include <iostream>
    int main()
    {
            int d = 'D';
            if( d == 'E' || 'F' )
                    std::cout << "See now?" << std::endl;
            return 0;
    }
    Go run mine and you'll see that mine works as intended while yours doesn't. You probably won't understand why, but hopefully the above will make it 'click'. If not, I'll tell you next post.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    #include <iostream>
    
    int main()
    {
            int d = 'D';
            if( d == 'E' || d == 'F' )
                    std::cout << "See now?" << std::endl;
            return 0;
    }


    man... how did I miss this..>?!?!?



    must be getting late zzZZzZ



    How about this..??
    Code:
    if (toupper(ch) == ('A' || 'E' || 'I' || 'O' || 'U'))
    		
    		cout << "a Vowel" <<endl;



    Thanks for making me use my brain btw
    Last edited by The Brain; 10-12-2004 at 01:51 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about
    Code:
    char isVowel(char ch) {
      return strchr( "AEIOU", toupper(ch) ) != NULL;
    }
    Spooky - A deja-vue moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Geez, somebody just tell him already
    Code:
    if (toupper(ch) == ('A' || 'E' || 'I' || 'O' || 'U'))
    You can't use || like that. Salem's answer works. Yours could go like this:
    Code:
    char upCh = toupper(ch);
    if (upCh == 'A' || upCh == 'E' || upCh == 'I' || upCh == 'O' || upCh == 'U'))

    For strgglingstuden:

    The problem is that you are returning 1 in a function that returns a char. Then you are printing out the char with the ASCII value of 1, which I guess is a smiley face. Since your function already outputs whether the character is a vowel or not, you don't need to return anything, and you certainly don't need to cout the return value of the function.

    If you want, you could remove the cout statements from your function and return a bool instead of a char. Then, if the letter is a vowel (your if statement), return true, and otherwise return false. Then in your main function have an if (isVowel(letter)) that outputs the correct statement at the end of your sentence. This is the preferred method of using function. The isVowel function would have one purpose, to return true or false depending on whether the character is a vowel or not.

Popular pages Recent additions subscribe to a feed