473,890 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::cin

Below is simple code:

#include <iostream>

int main(){

int i;
while(1){
while(!(std::ci n >i)){
std::cout<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut 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 15728
junw2000 wrote:
while(!(std::ci n >i)){
std::cout<<"Inv alid 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(ci n, str).)

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

Phlip wrote:
junw2000 wrote:
while(!(std::ci n >i)){
std::cout<<"Inv alid 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<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut 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<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut 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<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut i: "<<i<<'\n'; //LINE2
}

Sep 2 '06 #5
junw2000 wrote:
while(1){
while(std::cin >i){ //LINE1
std::cout<<"Inv alid 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::ci n >i)){
std::cout<<"Inv alid 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<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut 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::ci n >i)){
std::cout<<"Inv alid 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<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut 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::ci n >i)){
std::cout<<"Inv alid 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<<"Inv alid input i: "<<i<<'\n';
}
std::cout<<"inp ut 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2017
by: William Payne | last post by:
Hello, when I was writing a user-driven test program for a data structure I wrote, I encountered an annoying problem. The test program is laid out as a menu with several numbered options. The user selects one option by typing in its corresponding number (int). Depending on the choice he made, he may be asked to provide another integer (or no input at all). So all the user does is entering integers. I don't want the user to be able to cause...
5
12248
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue reading it reads what's left over in the cin, and well that's logical.
3
1934
by: yw | last post by:
Hi, When I use std::cin and std::cout to input some data, how can I ingore the cin by using the Enter key? For example: string sname; cout<<"Please input your name:"<<endl; cin>>sname; If I don't wan't to input anything, just press enter key to ingnore the cin
3
7117
by: puzzlecracker | last post by:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); I have seen it in faq - what does it do, exactly?
3
5334
by: moleskyca1 | last post by:
In C++ FAQ (15.5), I see this code: int i = 0; while (std::cin >x) { // RIGHT! (reliable) ++i; // Work with x ... } but I don't know how can while loop end? ostream &operator >>(...) return ostream &. how can that ever end the while loop? Is it because there is hidden operator to cast to int? I cannot figure out, please help?
8
3476
by: Johannes Meng | last post by:
Good day, I'm experimenting with unbuffered input at the moment. To get input I basically use cin.get(). My problem are control sequences preceeded by an ESC character (i.e. up, down, f-keys et cetera). They basically look like this: <ESC><EXCODE><CODE>, where EXCODE is a number describing the type of key pressed and CODE is the actual key. I want to be able to decide whether ESC or such a control key was pressed. Now if I do it like...
3
14697
by: Ralf Goertz | last post by:
Hi, consider the following program #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv){
12
3323
by: pekka | last post by:
I'm trying to measure user input time with my Timer class object. It isn't as easy as I expected. When using std::cin between timer start and stop, I get zero elapsed time. For some unknown reason, the clock seems to stop ticking during execution of std::cin. Here's my code: #include <ctime> #include <iostream> #include <string>
3
2902
by: Alex Snast | last post by:
hello guys I need to modify the std::cin delim char from the default ' ' and '\n' characters to ',' i know that i can edit the delim in the getline command however i'd like to know if there's something build in with me having to overload the std::cin operator thanks, Alex Snast.
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10829
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.