0
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.

opricnik
  • 23
  • 5
  • To clarify , when i debug my program ,on second call of function it wants you get input but not when getline() called, just before it's called/should be called. Then after i assign input it continues to stream but it doesnt assign my cin input to string. – opricnik Sep 27 '17 at 12:12
  • 2
    Well you apparently have a leftover newline in the input buffer, and calling `ignore` should remove it. If it doesn't seem to work then perhaps you have *multiple* newlines in the input buffer? Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) for us to see, and to *show* us the input you give the program? – Some programmer dude Sep 27 '17 at 12:12
  • @DragonRock It gets segmentation fault. – opricnik Sep 27 '17 at 12:13
  • My game gets input from terminal. It's either can get something like "LOAD FILE.TXT" or save version to do those things . Or input like "A","B". Because i have to think user can enter something with whitespace in it , i had to use getline. I don't know any other option where you can get full line . – opricnik Sep 27 '17 at 12:15
  • Wow , i found this line of code in very old site , it worked. While i was going to destroy my question i thought it may be useful. if ( cin.peek() == '\n' ) cin.ignore(); just before getline(cin,string); – opricnik Sep 27 '17 at 12:29
  • 1
    Possible dupe of https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – Mgetz Sep 27 '17 at 12:29
  • You haven’t fixed the problem; you’ve papered it over. Mixing stream extractors (`>>` operators) and `getline` is tricky, because the stream extractors copy up to whitespace, but don’t remove it. Life is much easier if you don’t mix them. Either use extractors throughout or use `getline` throughout. If you do need to use both, be sure you know how to deal with the transitions. – Pete Becker Sep 27 '17 at 12:51
  • What do you meant by "papered it over" . It works like expected to work now, what seems to be the problem – opricnik Sep 27 '17 at 13:24
  • Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Andrew Henle Sep 27 '17 at 18:23

1 Answers1

1

Place your cin.ignore() below getline()

getline(cin,cheyeni);
cin.ignore();
Ghulam Moinul Quadir
  • 1,448
  • 1
  • 9
  • 16