looking for a function

This is a discussion on looking for a function within the C++ Programming forums, part of the General Programming Boards category; I am supposed to use a 'library function' to check if a letter is a vowel. My guess is that ...

  1. #1
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    looking for a function

    I am supposed to use a 'library function' to check if a letter is a vowel. My guess is that the function is in the cctype header - but I don't know the function - or if one even exists. Could someone point me to this function, and an example of it's use please?
    Oh my goodness.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    int isalpha( int ch );

    That's what I know of; you have to manually check for vowels.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Why did you ask this when writing your own "isvowel" is a 5 minute job (if that)?

    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    I figured I would have to - but the exercise tells me to do this:
    Code:
     "Design a program that asks the usr to input a letter. Use a library 
    function to determine and output whether or not the letter is a
     vowel..."
    I know isalpha checks for a letter - but the directions in the exercise do seem a tad confusing - so what I'll do, is check for a letter with isalpha() and then write some code to check for a vowel with an if statement.

  5. #5
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by ahluka
    Why did you ask this when writing your own "isvowel" is a 5 minute job (if that)?

    I'm just trying to do what the book is asking... I haven't hit the chapter on functions yet.
    Oh my goodness.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    If a lecturer or equivalent gave that to you as an assignment ask them what the hell the std lib function is because I'd really like to know

    EDIT: Sorry didn't see above post (same time ).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Writing such a function is not as easy as it may first seems: vowels and consonants are not written with the same letters in all writing systems, and sometimes whether a letter is a vowel or consonant depends on the context.

    Take for example the letter Y. In English sometimes it's a vowel (lady), sometimes a consonant (year), and sometimes a diphtong (my). In other languagues it's always a consonant (Spanish), and in yet others always a vowel (Finnish).

  8. #8
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Well, I'm having trouble compiling - something is wrong with a static cast that tried to put together... also - could you guys help me double check my program's logic? I'm not sure the program is going to act like it is supposed to. So here's the order of operation that I want: Get user input, check for a letter, tell if said letter is uppercase or lowercase, check for a vowel, and then output the letter as lowercase at the end.

    Using a ton of if statements always seems to confuse me!
    EDIT: along with confusing me - I can never get clean formatting when I use them.

    Here's the code - with errors after:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cctype>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        char letter;
        
        cout <<"Please enter a letter on your keyboard: ";
        cin >> letter;
        cout << endl;
        
        if(std::isalpha(letter)) {
                if(std::isupper(letter)) {
                                                cout <<" Letter is uppercase\n";
                                                }
                            if(std::islower(letter)) {
                                                cout <<" Letter is lowercase.\n";
                                                }            
              
              
              static_cast<char>(std::toupper(letter)) <<endl;
              if(letter == 'A' || 'E' || 'I' || 'O' || 'U' || 'Y') {
                        cout <<"Your letter is a vowel.\n";
                        }
                        
              else{
                   cout <<"Your letter is not a vowel.\n";
                   }
              
              static_cast<char>(std::tolower(letter)); <<endl;
              cout <<"The letter was: " << letter;
                             
                             }                                               
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Errors are as follows:
    Code:
     
     C:\Dev-Cpp\letters.cpp In function `int main()': 
    26 C:\Dev-Cpp\letters.cpp invalid operands of types `char' and `<unknown type>' to binary `operator<<' 
    35 C:\Dev-Cpp\letters.cpp expected primary-expression before '<<' token 
     C:\Dev-Cpp\Makefile.win [Build Error]  [letters.o] Error 1
    Last edited by mabufo; 02-11-2006 at 10:16 AM.
    Oh my goodness.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    static_cast<char>(std::toupper(letter)) <<endl;
    static_cast<char>(std::tolower(letter)); <<endl;
    Spot the ;
    You also probably meant
    cout << static_cast<char>(std::toupper(letter)) <<endl;

    > if(letter == 'A' || 'E' || 'I' || 'O' || 'U' || 'Y')
    This is not what you want (it's always true)
    Try
    if(letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U' || letter == 'Y')

  10. #10
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by Salem
    static_cast<char>(std::toupper(letter)) <<endl;
    static_cast<char>(std::tolower(letter)); <<endl;
    Spot the ;
    You also probably meant
    cout << static_cast<char>(std::toupper(letter)) <<endl;

    > if(letter == 'A' || 'E' || 'I' || 'O' || 'U' || 'Y')
    This is not what you want (it's always true)
    Try
    if(letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U' || letter == 'Y')
    Hey I apprechiate the fixes! thank you! What I want to do though, is actually change the value of the letter to it's uppcase equivalent - that's what I wanted to do with the static cast to type char - but I guess I misunderstood how that worked with toupper()/tolower()....

    Could you show me how to change, say letter a to A and then store it? Because in my program, before it checks if the letter is a vowel - it skips the static cast and goes straight into the conditional if statement. when it should change the letter to uppercase and then go through the condtional statement.
    Oh my goodness.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    How about use an assignment statement?

  12. #12
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by Salem
    How about use an assignment statement?
    I'm not sure what you mean by that...
    Oh my goodness.

  13. #13
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by mabufo
    I'm not sure what you mean by that...
    = ...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  14. #14
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by ahluka
    = ...
    Well yes of course that - but how do I combine that with my static cast?
    Oh my goodness.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Oh well,
    letter = static_cast<char>(std::toupper(letter));

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21