| | |
infinite loop...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 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.
C++ Syntax (Toggle Plain Text)
z=(int)(z);
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.
•
•
•
•
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.
•
•
•
•
Originally Posted by lara_
how to prevent it from looping when character was entered?
"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
>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:
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:
>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
Since the standard requires digits to have adjacent values regardless of the character set. Or better yet, include <cctype> and say
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?
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:
C++ Syntax (Toggle Plain Text)
int number; while ( cin>> number ) cout<< number <<endl;
C++ Syntax (Toggle Plain Text)
#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; }
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
C++ Syntax (Toggle Plain Text)
if ( Z >= '0' && Z <= '9' )
C++ Syntax (Toggle Plain Text)
if ( std::isdigit ( Z ) )
>That way the infinite loop will run 2-4 times faster!
Kind of like trying to optimize your idle system process, no?
![]() |
Similar Threads
- Excel 2007 - infinite code loop (Windows Software)
- XP Startup Problem: Infinite Loop (Windows NT / 2000 / XP)
- infinite loop (C)
- infinite loop (C++)
Other Threads in the C++ Forum
- Previous Thread: programming in C using Visual C++.net
- Next Thread: C++ efficiency program
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets








