Drextor
Drextor

Reputation: 433

C++ - Method exit

I'm new to c++, and I am trying to write a program which goes from main-method to a case-choice and than to a second method with a if-choice. But the second cin gets skipped and the program exits in the method Lobby after printing out the text.

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Char();
void Lobby();
void Status(); 
void Test(); 

int i;
char c;
char pcharchoice;
int money;  
char pname; 

int main() {
    cout << "$$ Test $$\n";
    Char();
}

void Char(){
    cout << "Select :\n";
    cout << "1 : Test1  \n";
    cout << "2 : Test2  \n";
    cin >> pcharchoice;                
    cout << "\n";

    switch (pcharchoice) {
        case '1':
            money = 1000;
            break;
        case '2':
            money = 2500;
            break;
        default:
            exit(0);
        break;
    }

    cout << "Select a Name:\n";
    cin >> pname;

    Lobby();
}


void Lobby(){

    i = 0; 

    cout << "What would do ? \n";
    cout << "1 : Status \n";
    cout << "2 : Test \n";
    cout << "3 : Leave \n";

    cin >> i;                
    cout << "\n";

    if( i == 1 ) {
        Status();
    } else if( i == 2 ){
        Test();
    } else if ( i == 3 ){
        cout << "Quit\n";   
    }       
}

void Status(){
    cout << "** Status **\n";
    //TODO
}

void Test(){
    cout << "** Test **\n";
    //TODO  
}

Upvotes: 1

Views: 81

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409442

When reading a character with std::cin and the >> operator, you read one character. The problem is that you enter two characters of input: The actual character, and the newline you use to terminate the current input.

That newline is still in the input buffer, and next character you read will be that newline. This is because reading single characters reads the exact characters that are in the input buffer, it doesn't skip white-space.

There are two simple way of solving this, and two more complicated ways. The first simple way is to not read characters, but strings instead, because then the input operator >> will skip leading white-space, like for example newlines.

The second simple solution is to use the std::skipws I/O manipulator, to tell the input stream to skip all white-space:

cin >> skipws >> pcharchoice;

If you just want characters and don't expect any problems with the user entering to much input, this is the solution I recommend.

The first little more complicated method is to use the std::istream::ignore function to skip past the newline:

cin >> pcharchoice;
cin.ignore((numeric_limits<streamsize>::max(), '\n');

This solution has one big advantage in that it skips not only white-space until it passes the end of the line, it also skips any extra input the user might have given on the same line.

The second little more complicated method is to read the whole line into a string, and then get the character from the line (either by using index 0 of the string, or by using std::istringstream and "normal" formatted input operators).

Upvotes: 3

Related Questions