Thread: Switch??

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question Switch??

    Hi,

    I was wondering why this part of a switch function does not pause to get user input.


    Code:
    switch(input)
    		{
    		case 1:
    			cout << endl << "CREATE YOUR CHARACTER" << endl;
    			cout << "------------------------------" << endl << endl;
    			cout << "Enter your player's name: ";
    			getline(cin, mName);
    			cout << endl << endl;
    			break;
    Can someone please provide some enlightenment. Thanks very much. I appreciate it.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It has nothing to do with switch, and all to do with "how input works". I'm pretty sure that somewhere shortly before the switch(input) line, you have something lile
    Code:
    cin >> input;
    This, like scanf() in C, will leave a newline in the input stream. You need to read that newline out of the input stream before you do getline() [since getline will read everything up to the next newline, and if there is already a newline in the input stream, it will say "Ah, ok, you didn't want anything else in the strign, then..."].

    Code:
    cin.ignore(1000, '\n');
    will do that.

    Edit: Note, if someone fell asleep on the spacebar key and there are 1000+ spaces after the 1, then it will still fail. But I think that is unlikely enough to not worry about today - someone will surely show us how you make it "the largest ever number".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM