opricnik
opricnik

Reputation: 23

C++ getline gets skipped after first call

char input_Handler(int& error,int user,const char satir[]){
    string cheyeni;
    int error_string = 0;
    char che;
    char test;

    cout << "Please enter your move with keyboard letters such as 'A' or 'a'\n";
    //cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    //cin.ignore();
    //
    cin.ignore();
    getline(cin,cheyeni);
........rest is not here

I have this function which handles input for my game. This function gets called multiple times in game. In first call it handles input correctly. But when called again it behaves stupidly.From my search on web people always say getline doesn't assign newline when i press Enter, so when called again it doesnt get input from me but rather gets '\n' to my string(cheyeni) automatically.

I cant seem to fix this, i tried multiple things cin.sync(),cin.ignore() . While you may think i did use it wrongly i tried a lot of ways before getline after getline etc.. But still i could/probably am wrong on something. I cant seem to quite see it where's the part i'm missing.

User expected to input some string or letter.

Upvotes: 0

Views: 266

Answers (1)

Ghulam Moinul Quadir
Ghulam Moinul Quadir

Reputation: 1648

Place your cin.ignore() below getline()

getline(cin,cheyeni);
cin.ignore();

Upvotes: 1

Related Questions