0
Infinity Loop
I want take int from user, but if user accidentally enter string values, that going to infinitely. How can i stop or fix that? https://code.sololearn.com/c24bKie7NTT4/?ref=app
18 Answers
+ 8
MMK ,
For example if i give 5...
Since do while Loop....it will print the statement in cout("Enter your number":)
And then it checks the condition in while loop since satisfied..bcoz (5!=10)..it will again goes up and print the statement in cout...
Until you give 10...
If you give 10 then condition fails and only one time the statement in cout prints....and the loop breaks...
+ 7
MMK ,
It goes infinitely bcoz other than 10 whatever you give the loop runs again and again since the condition is while (number!=10)...
If you give 10 the condition fails and it breaks the loop....
And remove string dk;---->there is no use of it in this code...
Don't post your code in description tag...save your code in code playground and post the link of the code here....
what exactly your problem statement..??
+ 3
MMK Interesting. Now it's in Code Playground, I ran it.
It enters an infinite loop if I enter anything except 10. Even if I enter nothing.
But, if I remove the cout inside the loop, the code works as expected. There is some leftover from cout in the buffer, and it keeps providing content to cin.
Suggestion: either clear the buffer or change the I/O commands.
+ 3
Dragon RB when I/O accepts a string when int is expected, it set the bit of the current state to iostate::failbit.
There are other bits including iostate::goodbit, iostate::eofbit and iostate::badbit
What I am trying to tell him is to check if the bit is not good, then he should reset it otherwise cin will stop working therefore leading to the problem he's encountering .. this Is an example
https://code.sololearn.com/cEv3fvWaxqi8/?ref=app
+ 2
MMK Because the "number" is an int, not a string!
The code ask the user to input an INTEGER, but if STRING is found in "number", this may lead to unhandled/runtime error(will be introduced in upcoming lessons, I guess), and it will run the loop infinite times!!
If you want the code to not run the loop inf time in case user input a string, you can simply change "int" to string, and place the number 10 in double quotes.
You can convert it to int again if you want to use it in some situation..(Idk how to convert string to int in C++)
==================================
Please note that C++ doesn't handle all possible Exceptions and Errors(C++ doesn't provide a descriptive Exception names or Exception description, as if you divide any numbers with zero, it will generate random negative number, instead of throwing exception. A try-(throw)-catch block would be helpful at this time, but it may not work when checking if input variable does not contain a correctsponding value.
==================================
+ 2
MMK if user enters a string, note that the iostream:iostate will be set to iostate::failbit which can further be checked by an if condition inside the do statement
if (cin.fail()) {
// Reset the bit to goodbit
// Now loop runs normally
}
+ 1
Ok listen, I want to is user should be put 10, if user put integer there is no problem, but if user accidentally put character then there is a problem it is only allow integer, if user accidentally put string value loop gone infinitely... I want to if user accidentally enter string than loop will show aging, best try your Self put string than see you... What happened..
+ 1
MMK your code requires to run a while loop interactively and I don't have my PC with me at the moment... Maybe I'll check it and try to fix it after some times
0
How?
0
MMK
Why does it run infinite times even though you use do-while loop?
As I said before, C++ doesn't handle all possible exceptions and error, and the integer "number" is executed before the loop.. But if string is found in "number".. this may lead to unhandled/runtime error or Exception like I said before..
a do-while loop is used execute atleast one time even though if the condition is False, but I believe it doesn't handle exceptions.
Since the error occurs, we won't be able to input next number, then the program continues to ask user to input number..
C++ goes very difficult to understand sometimes, as it confuses me a lot when I was pretty new.
0
Runtime Terror I think that explanation may confuse the learner, because you haven't explain what the `cin.fail()` does and what is iostream:iostate, iostate::failbit(I don't understand as well).
But I hope it helps:)
0
Runtime i don’t understand what are you talking about, but thanks for try...
0
Other think you can do is take my code fix it than share that link
0
Runtime Terror
I think I got it(a little). Thanks man
But I'm still getting confused about bits in C++. //does it work like bits from bits array in C#? :))//
0
Ok i don’t have any PC also, everything i doing in my phone
0
Take the input via a string and use getline(). Then use strtoul() to convert to a number. If successful proceed and if not ask again.
0
I don’t know about
strtoul()