if, else if question

This is a discussion on if, else if question within the C++ Programming forums, part of the General Programming Boards category; Can someone please explain why this program does not work? Also, for some reason, I cannot get my cin.get() to ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    177

    if, else if question

    Can someone please explain why this program does not work? Also, for some reason, I cannot get my cin.get() to pause and await a key press from the user. I know both items are rather simple but I have drawn a blank. Thank you all!
    Code:
    #include <iostream>
    #include <cstdlib>
    
    int choice;
    
    int main()
    {
        using namespace std;
    
        cout << "Do you want to hear a joke?" << endl;
        cout << "Enter y for yes or n for no:" << endl;
        cin >> choice;
    
            if (choice == 'y' || choice == 'Y')
            {
                cout << "\n" << endl;
                cout << "What do you call a guy with" << endl;
                cout << "no arms and no legs laying on the floor?" << endl;
                cout << "\n" << endl;
                cin.get();
                cout << "Matt!" << endl;
            }
            else if (choice == 'n' || choice == 'N')
            {
                cout << "Program will end." << endl;
                return 1;
            }
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    'y' is a char, not an int.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Quote Originally Posted by tabstop View Post
    'y' is a char, not an int.
    Thanks tabstop! Really appreciate it, but can you tell me why my cin.get() doe not work? What I am wanting to do is make the program stop and await the user to press any key to allow the program to continue and show the result.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    cin.get works exactly as it's supposed to. The problem is that what it is supposed to do is not "make the program stop and wait for the user to press any key". In fact, there's not really any standard C++ function that does that, since C++ doesn't really expect there to be a keyboard on the other end.

    get will wait for a line of input to be entered. Since there already is a line of input there, from you asking the question earlier (but only reading in a single character instead of the whole thing) there's no reason to wait.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    177
    Quote Originally Posted by tabstop View Post
    cin.get works exactly as it's supposed to. The problem is that what it is supposed to do is not "make the program stop and wait for the user to press any key". In fact, there's not really any standard C++ function that does that, since C++ doesn't really expect there to be a keyboard on the other end.

    get will wait for a line of input to be entered. Since there already is a line of input there, from you asking the question earlier (but only reading in a single character instead of the whole thing) there's no reason to wait.
    Thanks! I suppose the only way to actually do that is use a system pause but then again that would not be portable. I appreciate the insight.

  6. #6
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    A simple solution might be:

    Code:
    std::cin.ignore();
    std::string guess;
    std::getline(std::cin, guess);
    goto( comeFrom() );

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Quote Originally Posted by StainedBlue View Post
    A simple solution might be:

    Code:
    std::cin.ignore();
    std::string guess;
    std::getline(std::cin, guess);
    That's really a press any key followed by enter command, and because his program already has "using namespace std;", all those std::'s are just going to confuse him.

    I think I remember seeing a press any key to continue code in a program from a book I read earlier this year, but I can't find it in there and can't remember it at all :\

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

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