std::cin

J

junw2000

Below is simple code:

#include <iostream>

int main(){

int i;
while(1){
while(!(std::cin >> i)){
std::cout<<"Invalid input i: "<<i<<'\n';
}
std::cout<<"input i: "<<i<<'\n';
}
}

When I input a non-integer, like 'r', the code will loop infinitely.
What is the problem? How to revise it?

Thanks.
 
J

junw2000

Phlip said:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2
"Why does my program go into an infinite loop when someone enters an invalid
input character?"
Thanks. I think I followed the faq which suggests while(std::cin >> i).
The code below still cause infinite loop, if I input a char, e.g, 'a'.

#include <iostream>

int main(){

int i;
while(1){
while(std::cin >> i){
std::cout<<"Invalid input i: "<<i<<'\n';
}
std::cout<<"input i: "<<i<<'\n';
}
}
 
A

Andreas Bernauer

Thanks. I think I followed the faq which suggests while(std::cin >> i).
The code below still cause infinite loop, if I input a char, e.g, 'a'.

Maybe because you use an infinite loop? (search for `while(1)').
#include <iostream>

int main(){

int i;
while(1){
while(std::cin >> i){
std::cout<<"Invalid input i: "<<i<<'\n';
}
std::cout<<"input i: "<<i<<'\n';
}
}


Andreas.
 
J

junw2000

Andreas said:
Maybe because you use an infinite loop? (search for `while(1)').
Yes. while(1) is a infinite loop. But if LINE1 is correct, LINE1 below
should stop the loop.
Furthermore, nothing is printed from LINE2.

int i;
while(1){
while(std::cin >> i){ //LINE1
std::cout<<"Invalid input i: "<<i<<'\n';
}
std::cout<<"input i: "<<i<<'\n'; //LINE2
}
 
L

lmh86

Thanks. I think I followed the faq which suggests while(std::cin >> i).
The code below still cause infinite loop, if I input a char, e.g, 'a'.

#include <iostream>

int main(){

int i;
while(1){
while(std::cin >> i){
std::cout<<"Invalid input i: "<<i<<'\n';
}
std::cout<<"input i: "<<i<<'\n';
}
}

std::cin is attempting to input a letter into an int variable. It can't
do this, so it (std::cin) enters a "failed state". This means all
attempt to invoke std::cin from then on will throw an exception. while()
catches this exception and interprets it as FALSE, therefore breaking
the loop.
In your situation, once a non-int is inputted, the inner while() will
always skip. The outer while() loops infinitely, with i spitting out its
last valid value.
 
P

Peter Jansson

lmh86 said:
std::cin is attempting to input a letter into an int variable. It can't
do this, so it (std::cin) enters a "failed state". This means all
attempt to invoke std::cin from then on will throw an exception. while()
catches this exception and interprets it as FALSE, therefore breaking
the loop.
In your situation, once a non-int is inputted, the inner while() will
always skip. The outer while() loops infinitely, with i spitting out its
last valid value.

To clarify...

An exception may be thrown when std::cin enters a "failed state" if the
user have coded that it should be so.

while() will not catch, only a catch clause can catch an exception. The
inner while is exited when "std::cin>>i" fails.


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
R

Rolf Magnus

lmh86 said:
std::cin is attempting to input a letter into an int variable. It can't
do this, so it (std::cin) enters a "failed state". This means all
attempt to invoke std::cin from then on will throw an exception.

Not by default.
while() catches this exception and interprets it as FALSE, therefore
breaking the loop.

This hasn't anything to do with exceptions. What actually happens is that
the operator<< returns a reference to the stream, which can be converted
into a pointer that is a null pointer if the stream is in fail state or
non-null otherwise. There are (obscure) reasons why it's a pointer instead
of just a bool. Anyway, this value is used to terminate the while loop if
the stream is in fail state.
In your situation, once a non-int is inputted, the inner while() will
always skip. The outer while() loops infinitely, with i spitting out its
last valid value.

Yup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top