954,148 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

infinite loop...

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. :sad:
how to prevent it from looping when character was entered?

lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

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

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.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
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. :sad:

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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!

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

>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:

int number;

while ( cin>> number )
  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 streamor 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:

#include <iostream>

using namespace std;

bool get_number ( int& number )
{
  while ( !( cin>> number ) ) {
    if ( cin.eof() )
      return false;
    else {
      char ch;

      cin.clear();

      cout<<"Invalid input, please try again: ";
      while ( cin.get ( ch ) && ch != '\n' )
        ;
    }
  }

  return true;
}

int main()
{
  int number;

  while ( get_number ( number ) )
    cout<< number <<endl;
}

>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

if ( Z >= '0' && Z <= '9' )

Since the standard requires digits to have adjacent values regardless of the character set. Or better yet, include and say

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? :D

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I know this thread is really old, but I just can't help commenting on the infinite loop optimization joke :) That was funny!

mmiikkee12
Posting Whiz in Training
274 posts since Oct 2004
Reputation Points: 17
Solved Threads: 5
 

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. :sad: how to prevent it from looping when character was entered?

Here is some solutionZ = static_cast(Z);
static cast changes parametres (Z) type into type declared inside
I dont know if this is best sollution, place it before cin>>
:)

Hymppis
Newbie Poster
1 post since Feb 2011
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You