if i want to take the input of a user and check to see an int, how do i do that?
if i want to take the input of a user and check to see an int, how do i do that?
I Love Jesus
yea i have a similar problem that when you input a char(when you need an Int) or a bunch of chars it loops and loops and loops... i need a way to fix that...
someone said something like
but i dunno i havent tried it...Code:while(cin.fail()) { cout<<"Enter an integer not a char: "; cin>>num; }
Paro
those aren't working. the second one doesn't let me input a new value for my variable
I Love Jesus
howdy,
this function was given to me by another friend on this board. i dont remember who at this moment.
it works just fine for what i need.
all of the credit goes to someone else on this one.Code:int IsIntt(char *input) //checks for input int { int bad = 0; int i; for(i = 0; i<strlen(input); i++) { if(!isdigit(input[i]) && input[i] != '.') return 0; if(input[i] == '.') bad++; } if(bad) return 0; return 1; }
M.R.
whoa! thats some high-tech shiz, and i dont understand half of it...isn't there a more nooblar way of doing it?
gosh, im starting to find out how nooblar i really am
Paro
howdy,
i,m going out of town right now, i will explain it to you tonight.
notice how the input is read into the function.
on hint:
if(input[i] == '.')
bad++;
looks for decimal point and increments bad so 5.4 will return bad.
M.R.
Fiddle around with this and see what is good and what is bad.
Generally anything that can't be truncated into an integer will cause the if to fail. So an alphabetic character or special symbol will fail but a floating point will be truncated and lose everything past the decimal.Code:#include <iostream> using namespace std; int main ( void ) { int i; if ( cin>>i ) cout<< i <<" good"<<endl; else cout<<"bad"<<endl; return EXIT_SUCCESS; }
This is the easiest way, but the problem of not reading floating point numbers might not work for what you need it to do. But I don't see how it would unless the user entered 1.6 and wanted it to be rounded to 2.
-Prelude
My best code is written with the delete key.
No offense to the fine example, here is a full example of how to test for an integer entry with c++.
Code:#include<iostream.h> #include<string.h> #include<stdlib.h> #include<ctype.h> int isinteger( char* sznumber ); void main( void ) { char buffer[100] = {0}; int num = 0; while( num != -1 ) { cout << "\nEnter a number(-1 to quit): " << flush; cin.getline(buffer, 80); if( isinteger( buffer ) == 1) { // convert to integer after verification num = atoi( buffer ); cout << "You entered: " << num << endl; } else { cout << buffer << " is not a valid integer." << endl; } } } int isinteger( char* sznumber ) { int iLength = strlen(sznumber); for( int i = 0; i < iLength; i++) { //accept '-' only as first char if( sznumber[i] == '-' && i == 0 ) continue; if( !isdigit( sznumber[i] ) ) return 0; } return 1; }
zMan
..or the concise version, using functionality already built into the istream classes -
Code:int i; cin>>i; while(cin.fail()) { cin.clear(); cin.ignore(); cout << "not integer\n"; cin>>i; }
the function automatically loops through your entry say for instance you enter w123 which is not an integer it will fail at first and when it encounters 123 it will pass. Also you must remember the cin is tied to cout and the behavior is screwy when trying to flush the input. I prefer getting all input as a line evaluating it and if it is screwed up you must re-enter it.
zMan
>the function automatically loops through your entry say for instance you enter w123 which is not an integer it will fail at first and when it encounters 123 it will pass. Also you must remember the cin is tied to cout and the behavior is screwy when trying to flush the input.
Ok, then you'd clear the buffer -
What would happen in your case if you were to enter different data types on the same line?Code:int i; cin>>i; while(cin.fail()) { cin.clear(); cin.ignore(80,'\n'); cout << "not integer\n"; cin>>i; }