468,462 Members | 1,858 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 developers. It's quick & easy.

std::cin

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.

Sep 1 '06 #1
8 15397
junw2000 wrote:
while(!(std::cin >i)){
std::cout<<"Invalid 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?
http://www.parashift.com/c++-faq-lit....html#faq-15.2
"Why does my program go into an infinite loop when someone enters an invalid
input character?"

Always read the FAQ before posting!

(Then use std::getline(cin, str).)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 1 '06 #2

Phlip wrote:
junw2000 wrote:
while(!(std::cin >i)){
std::cout<<"Invalid 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?

http://www.parashift.com/c++-faq-lit....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';
}
}

Sep 1 '06 #3
On Fri, 01 Sep 2006 23:10:48 +0200, <ju******@gmail.comwrote:
>http://www.parashift.com/c++-faq-lit....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'.
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.
Sep 1 '06 #4

Andreas Bernauer wrote:
On Fri, 01 Sep 2006 23:10:48 +0200, <ju******@gmail.comwrote:
http://www.parashift.com/c++-faq-lit....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'.

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
}

Sep 2 '06 #5
junw2000 wrote:
while(1){
while(std::cin >i){ //LINE1
std::cout<<"Invalid input i: "<<i<<'\n';
}
The code in the FAQ had a break statement.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Sep 2 '06 #6
ju******@gmail.com wrote:
Phlip wrote:
>junw2000 wrote:
>> while(!(std::cin >i)){
std::cout<<"Invalid 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?
http://www.parashift.com/c++-faq-lit....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';
}
}
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.
Sep 2 '06 #7
lmh86 wrote:
ju******@gmail.com wrote:
>Phlip wrote:
>>junw2000 wrote:

while(!(std::cin >i)){
std::cout<<"Invalid 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?

http://www.parashift.com/c++-faq-lit....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';
}
}

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/
Sep 2 '06 #8
lmh86 wrote:
ju******@gmail.com wrote:
>Phlip wrote:
>>junw2000 wrote:

while(!(std::cin >i)){
std::cout<<"Invalid 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?
http://www.parashift.com/c++-faq-lit....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';
}
}

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.

Sep 2 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by William Payne | last post: by
5 posts views Thread by Chris Mantoulidis | last post: by
3 posts views Thread by yw | last post: by
3 posts views Thread by moleskyca1 | last post: by
8 posts views Thread by Johannes Meng | last post: by
3 posts views Thread by Ralf Goertz | last post: by
12 posts views Thread by pekka | last post: by
3 posts views Thread by Alex Snast | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.