Strange infinite looping

C

chandra.somesh

I am having trouble understanding why the code given belows enters an
infinite loop when a char is entered instead of an int.
i.e.on subsequent looping ,control doesn't wait for user input and just
keeps printing "Hello" infinitely.

#include<iostream>

using namespace std;

int main()
{
int ch;


while(1)
{
cout<<"Hello";

cin>>ch;

cout<<"You entered "<<ch<<endl;

if(ch==1)
break;

}

return 0;
}
 
R

Rolf Magnus

I am having trouble understanding why the code given belows enters an
infinite loop when a char is entered instead of an int.
i.e.on subsequent looping ,control doesn't wait for user input and just
keeps printing "Hello" infinitely.

See question 15.2 "Why does my program go into an infinite loop when someone
enters an invalid input character?" in the FAQ to this newsgroup.
 
R

Robbie Hatley

I am having trouble understanding why the code given belows enters an
infinite loop when a char is entered instead of an int.
i.e.on subsequent looping ,control doesn't wait for user input and just
keeps printing "Hello" infinitely.

#include<iostream>

using namespace std;

int main()
{
int ch;


while(1)
{
cout<<"Hello";

cin>>ch;

cout<<"You entered "<<ch<<endl;

if(ch==1)
break;

}

return 0;
}

The main problem is your test:

if(ch==1) break;

That will only be true if the character the user enters has
ascii code 1, which is the "SOH" control character. That's
not on any keyboard. There's no such key. Hence, infinite
loop. (Well, the user might be able to break from the loop
by pressing ALT-0-0-0-1, but not many users would have the
gumption to do that.)

I think what you really want is:

if ('1' == ch) break;

NOTE THE SINGLE QUOTES AROUND THE '1'. That indicates
"the character '1' " rather than "the character whose ascii
code is 1".


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
R

Robbie Hatley

Well, I should have paid more attention.

I had replied to (e-mail address removed) , saying:
The main problem is your test:

if(ch==1) break;

...

I think what you really want is:

if ('1' == ch) break;

Well, no, not unless you make the code read one character
at a time. I saw "ch", thought it was a character, and
replied. I should have scrolled up, saw that ch was an
int.

You main problem is actually that your cin stream is
getting screwed up. Ach, whole can of worms.
I should never have stuck my foot in my mouth.
Read the FAQ, as Rolf Magnus suggests in his reply.
It explains this issue quite nicely. The relevant link is:

http://new-brunswick.net/workshop/c++/faq/input-output.html#faq-15.2


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
P

Panjandrum

Rolf said:
See question 15.2 "Why does my program go into an infinite loop when someone
enters an invalid input character?" in the FAQ to this newsgroup.

An who explains to the poor newbie what 'while (std::cin >> i)'
_really_ does?
 
R

Rolf Magnus

Panjandrum said:
An who explains to the poor newbie what 'while (std::cin >> i)'
_really_ does?

Question 15.4 "How does that funky while (std::cin >> foo) syntax work?".
 

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

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,181
Latest member
RexGreenwa
Top