472,358 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 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 1736
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.