Results 1 to 10 of 10

Thread: [RESOLVED] Returning a Char from a Function

  1. #1

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444

    [RESOLVED] Returning a Char from a Function

    I'm trying to return a single char from a function, but I'm not doing it right. Can someone tell me how? Here's what I have:

    PHP Code:
    #include <iostream.h>
    #include <string>

    char show_choice(bool clear_screen true);

    int main()
    {
        
    char    user_choice[1] = "";
        do    
    //Loop while the users doesn't want to quit
        
    {
            
    //Get the user's choice
              
    user_choice[1] = show_choice();
        } while (
    strcmp(user_choice"Q") != && strcmp(user_choice"q") != 0);
        return 
    0;
    }

    char show_choice(bool clear_screen /*= true*/)
    {
        
    //if (clear_screen == true) ;
        
        
    char user_choice[1] = "";
        do
        {
            
    cout << "U = Enter web page URI\n" <<
                    
    "O = Options\n" <<
                    
    "H = Help\n" <<
                    
    "Q = Quit\n";
            
    cout << "Please enter your choice: ";
            
    cin >> user_choice;
        } while (
    strcmp(user_choice"U") != && strcmp(user_choice"u") != &&
                 
    strcmp(user_choice"O") != && strcmp(user_choice"o") != &&
                 
    strcmp(user_choice"H") != && strcmp(user_choice"H") != &&
                 
    strcmp(user_choice"Q") != && strcmp(user_choice"q") != 0);
        return 
    user_choice[1];

    (they need some real C++ highlighting, so it says C++ code: )
    Last edited by Rick Bull; Sep 13th, 2002 at 12:11 PM.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    char    user_choice[1] = "";
    Try just char user_choice = '\0' and compare only the first character (using == rather than strcmp).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Code:
    char user_choice;
        do
        {
            cout << "U = Enter web page URI\n" <<
                    "O = Options\n" <<
                    "H = Help\n" <<
                    "Q = Quit\n";
            cout << "Please enter your choice: ";
            cin >> user_choice;
        } while ((user_choice != 'U') && (user_choice != 'u') && (user_choice != 'O') && (user_choice != 'o') &&
    (user_choice != 'H') && (user_choice != 'h') &&
    (user_choice != 'Q') && (user_choice != 'q'));
        return user_choice;
    Should work.

    Z.

  4. #4

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Thanks for the help guys. It's working now! So what's the difference between using single and double quotes?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Double quotes creates a const char*, i.e. a pointer to a pre-allocated section of memory. Single quotes surround *one* character.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I get ya (I think ). Thanks.

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    'a' == 'a' != "a" == |'a'|'\0'|

    If that makes sense =).

    Z.

  8. #8

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Umm, almost! So "a" is just 'a' with a null char at the end?

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes, but stored elsewhere.

    Ok, you have your function:
    Code:
    void function() {
        char a = 'b';
        const char* b = "hello world!";
        char c[] = "another string";
    }
    There, on the function's stack, there is the character 'b', then a pointer, then enough space to fit in "another string". This is why I made the distinction with const.

    The "hello world!" is stored somewhere completely different, all that is kept with the function is a pointer that you cannot change.

    You can, of course, modify c as long as you don't change the length
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    OK I get it now, thanks.

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