473,382 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Strange infinite looping


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;
}

Jul 23 '05 #1
6 1814
ch************@gmail.com wrote:

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.

Jul 23 '05 #2
<ch************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

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

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #3
Well, I should have paid more attention.

I had replied to ch************@gmail.com , saying:
... infinite loop ...
#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;

...

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+....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

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #4
Rolf Magnus wrote:
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?

Jul 23 '05 #5
Panjandrum wrote:
Rolf Magnus wrote:
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?


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

Jul 23 '05 #6
Rolf Magnus wrote:
Panjandrum wrote:
Rolf Magnus wrote:
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?


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


Reading this as beginner I'd immediately switch to C#.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stuart D. Gathman | last post by:
I have a multi-threaded background program in Python (http://www.bmsi.com/python/milter.html). Rarely, several threads will get themselves into an infinite loop. The system continues to run - but...
6
by: RdR | last post by:
Hi, Has anyone encountered infinite looping in Q Replication? This happens when I have a source DB2 table A going to a target DB2 table B, it also happens that the samne target table B is...
7
by: Rob | last post by:
I'm stuck in a program I'm making. I'm supposed to have a menu that asks the user to choose an option between 1 and 3. If the user chooses a valid option, i.e. types a number from 1 to 3, then...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
2
by: Paul Kozik | last post by:
I'm working with a small server program I'm writing for a small video game. The main class constructor starts a thread that handles socket connections, which itself starts new threads for each user...
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
3
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.