infinite loop...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2004
Posts: 17
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

infinite loop...

 
0
  #1
Sep 27th, 2004
i dunno whether this question has been ask before but i've try to search but didn't found. so i ask it here...

very simple... if i declare

int Z;
cin >> Z;


if I enter integer, it works fine but i enter character it's looping.
how to prevent it from looping when character was entered?
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 125
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: infinite loop...

 
0
  #2
Sep 27th, 2004
It looks as if you are automatically assuming that Z is an integer, and then doing some math calculations with it. When it's not an integer, but it's treated as one, you could end up in some sorta loop further down in your program. It looks as if your best option here is to determine whether Z is an integer, and if it isn't, prompt the user again to re-enter a number. You might also try doing
  1. z=(int)(z);
to force z to be treated as an integer.

Regardless ... there might be a built-in function (I'm not exactly sure) that determines whether a value isInt() but if not, you will need to write one yourself. You would look at the ASCII equivalent of the value and see if it is an integer range or a character range. I'm not exactly positive how this would be done or if there is a simpler way (basically because I don't have an ASCII chart handy right now.) Perhaps someone else can offer more assistance than I.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: infinite loop...

 
0
  #3
Sep 28th, 2004
Originally Posted by lara_
i dunno whether this question has been ask before but i've try to search but didn't found. so i ask it here...

very simple... if i declare

int Z;
cin >> Z;


if I enter integer, it works fine but i enter character it's looping.
If you enter characters that are not part of an int, then the input will fail. But the offending character(s) will be left in the input stream.
Originally Posted by lara_
how to prevent it from looping when character was entered?
Read user input as a string. Then attempt to convert the test to an integer. If it fails, try again.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: infinite loop...

 
0
  #4
Sep 28th, 2004
As a side note, if you think your program has an infinite loop, use the compiler's optimizer switches. That way the infinite loop will run 2-4 times faster!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 717
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: infinite loop...

 
1
  #5
Sep 29th, 2004
>It looks as if you are automatically assuming that Z is an integer
That's a reasonable assumption seeing as how Z was declared as int.

>When it's not an integer, but it's treated as one, you could end up in some sorta loop further down in your program.
The problem is with cin. The >> operator of cin will figure out what type the object is and convert the data from the standard input stream to that type. If there's no conversion then cin will leave the unconverted data in the stream and enter a failure state. This is a common problem with loops like this:
  1. int number;
  2.  
  3. while ( cin>> number )
  4. cout<< number <<endl;
If a letter is entered, this will be an infinite loop because cin will continue to fail on the invalid data in the stream. The solution is to remove the offending input and clear the stream or read all data as a string and parse it for error handling. The latter is easy and the former can be done (rather naively) like this:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool get_number ( int& number )
  6. {
  7. while ( !( cin>> number ) ) {
  8. if ( cin.eof() )
  9. return false;
  10. else {
  11. char ch;
  12.  
  13. cin.clear();
  14.  
  15. cout<<"Invalid input, please try again: ";
  16. while ( cin.get ( ch ) && ch != '\n' )
  17. ;
  18. }
  19. }
  20.  
  21. return true;
  22. }
  23.  
  24. int main()
  25. {
  26. int number;
  27.  
  28. while ( get_number ( number ) )
  29. cout<< number <<endl;
  30. }
>to force z to be treated as an integer.
It's too late at that point. The issue isn't with Z being treated as an integer, but with cin's >> operator returning a failure status.

>there might be a built-in function (I'm not exactly sure) that determines whether a value isInt()
Provided the value is a string, you can try to convert it to an integer with atoi (yuck) or strtol (better). If the conversion succeeds then it's an integer, otherwise not. But this is a moot point if you're trying to read and convert input with the same function call, such as scanf or cin's >> operator.

>You would look at the ASCII equivalent of the value and see if it is an integer range or a character range.
Or just use
  1. if ( Z >= '0' && Z <= '9' )
Since the standard requires digits to have adjacent values regardless of the character set. Or better yet, include <cctype> and say
  1. if ( std::isdigit ( Z ) )
But that only handles one character. For numbers longer than a single digit you need a loop. Once again, the point is moot because cin will try to convert the input to an integer before you can get your hands on it. By then the damage has presumably already been done.

>That way the infinite loop will run 2-4 times faster!
Kind of like trying to optimize your idle system process, no?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 274
Reputation: mmiikkee12 is an unknown quantity at this point 
Solved Threads: 5
mmiikkee12's Avatar
mmiikkee12 mmiikkee12 is offline Offline
Posting Whiz in Training

Re: infinite loop...

 
0
  #6
Jul 2nd, 2005
I know this thread is really old, but I just can't help commenting on the infinite loop optimization joke That was funny!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC