0

I'm currently trying to create a loop that should only stop if the user enters "quit", "Quit" or something similar. I tried a few variations already but I need it to execute the rest of the loop and not stop to wait for an Input.

Running = true;
while(Running)
  {
    char c;
    std::cout << "test ";
    c = std::cin.peek(); 
    if(c=='Q'|| c=='q')
    {
    Running = false;
    }
  }

Any help would be greatly appreciated.

eitzo
  • 93
  • 2
  • 8
  • You probably meant to write `c = std::cin.peek();` Also you should tell us what else is wrong with that code. – πάντα ῥεῖ Mar 18 '15 at 14:12
  • Yeah I did. It prints "test" once. If I press enter I can't stop the loop. If I enter "quit" after the first "test" it work. I want it to print "test" endlessly until I enter "quit". Sorry if I wasn't clear enough. – eitzo Mar 18 '15 at 14:16
  • Maybe this is worth reading: http://stackoverflow.com/questions/3317740/checking-data-availability-before-calling-stdgetline – Support Ukraine Mar 18 '15 at 14:19

3 Answers3

1

Aside from peek() returning int_type, not char...

...it reads the next character from the stream without actually extracting it.

Since your cin does, at this point, not contain anything (unless you piped something to your program, or already typed something), the call will block until there is a character to be peek()ed.

Since, using <iostream>, your program does not actually receive input until you press Enter, that is what your program does: Waiting until you entered something, and pressed Enter.

Only then does the program check whether the first character you entered is "q" or "Q". The line you entered, including that first character, will remain in the stream -- so the next time you peek(), it will still be the very same character.

Solution? There is no non-blocking query of cin in the C++ standard library. You either have to work with threads, or refer to some third-party input handling (like the getch() function offered by e.g. Microsoft Windows or the NCurses library).

shauryachats
  • 9,975
  • 4
  • 35
  • 48
DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    @volerag: Damn it, they've *changed* the label of the bloody key since the C64? Tells you how long it's been since I stopped looking. :-D – DevSolar Mar 18 '15 at 15:06
0

Do it simply this way.

std::string input;
cin>>input;
while(input!="quit" && input!="Quit")
{
// do something;
// get input.
}
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Not sure what you mean by

I need it to execute the rest of the loop and not stop to wait for an Input.

But if you replace c=std::cin.peek() by std::cin >> c, infinite loop problem will be solved.

Running = true;
while (Running)
{
    char c; 
    std::cin >> c;
    std::cout << "test";

    if (c == 'Q' || c == 'q')
    {
        Running = false;
    }
}
sam
  • 2,033
  • 2
  • 10
  • 13
  • I want the loop to continue if there is no input, but stop if there is "quit". I guess I'm looking for some kind of interrupt thing ? – eitzo Mar 18 '15 at 14:56
  • Define 'quit' because I am still not sure. Maybe loop continues till count is 10,100...? – sam Mar 18 '15 at 15:00