+ 1

A little help?

So I coded a feature which says to the user if he wants to exit the program by typing zero(0).It works perfect on the test subject but when I put the same line of code to a pre-existing app that I had previously made it doesn't work, in fact it skips completely the function of asking the user to put the number 0 and the program terminates by it self. The code: bool quit=0; while(!quit) { cout<<"Press the '0' key to terminate the programm"<<endl; int a; cin >> a; if(a <=0) { return 0; } }

2nd Jun 2019, 9:16 PM
DRIMITEROS
DRIMITEROS - avatar
16 Antworten
+ 3
~ swim ~ as usual you are very competent! I tried and the results are: If I input a char or a string it assigns 0 and exit. With enter or space or nothing it prints 8 infinite times. I don't know if it is garbage or what at this point. [EDIT if I initialize a to 7 for example and I enter nothing it prints 7 infinite times ] With noskipws instead it assigns 0 So it seems like it sets failbit and assign 0 if I enter wrong inputs, it skips if I don't provide input, e.g. space,enter I have also noticed that if I enter one valid input for example 2 it assigns 2 and print it infinitely Anyway it doesn't wait.. it proceed and loops infinitely P.s. I have added a cout<< at the end of while block
3rd Jun 2019, 7:25 AM
AZTECCO
AZTECCO - avatar
+ 2
bool quit=0 while(!quit<1) The ! means not. So this means: While quit is not lower than 1. But it is lower than 1 to begin with, so the code is not executed.
2nd Jun 2019, 9:40 PM
HonFu
HonFu - avatar
+ 2
~ swim ~ yes you are right ! It was undefined behaviour , but from C++11 it is set to 0 in case it fails to convert input. Anyway I think leaving uninitialized variables is a bad habit, potentially leading to UB.
3rd Jun 2019, 5:34 AM
AZTECCO
AZTECCO - avatar
+ 2
~ swim ~ it doesn't wait, it fail to extract the input and set a to 0
3rd Jun 2019, 5:53 AM
AZTECCO
AZTECCO - avatar
+ 2
~ swim ~ yes , it should wait for input but it's not, at least here on code playground. That's why I was a bit confused. Maybe due to the system forward input , seems like cin gets fed up on every iteration of the while loop. For the white space / enter I got the same result(discarded) but on my tests an 8 or previous value is assigned even if I clear and flush , I don't think it's garbage, probably something else. Anyway there's no reason to struggle with all of this, since this kind of programs are managed in a different way and streams aren't intended for key events.
4th Jun 2019, 12:49 AM
AZTECCO
AZTECCO - avatar
+ 1
Try it like this, you can freely choose to use `break` to exit the loop, or `return` statement to exit a function. I used `break` here because I didn't see the code placed in a function. int a; while(true) { cout << "Press the '0' key to terminate the programm" << endl; cin >> a; if(a <= 0) { break; } } Hth, cmiiw
2nd Jun 2019, 9:41 PM
Ipang
+ 1
The code was ment to start with: bool quit=0; while(!quit) And not: bool quit=0; while(!quit<1) Sorry for that I got distracted and typed wrong 😊 but the problem remains...
2nd Jun 2019, 9:48 PM
DRIMITEROS
DRIMITEROS - avatar
0
1 Undefined Behaviour : a is local uninitialized, some compiler may default to 0. 2 A key should be of char type, or enumerated. 3 declare a every iteration is not good from my point for ( char a = 0 ; !quit ; ){ cin >> a; If ( '0' == a ) return 0; // better quit = 1 }
2nd Jun 2019, 11:45 PM
AZTECCO
AZTECCO - avatar
0
~ swim ~ what happens if I press enter? DIMITRIS_PLAYS you should edit your question and correct to while (!quit) {... As you really intended.
3rd Jun 2019, 5:02 AM
AZTECCO
AZTECCO - avatar
0
And a? What value has been assigned? Does it wait till an integer is provided or does it continue through the program?
3rd Jun 2019, 5:11 AM
AZTECCO
AZTECCO - avatar