-
Check??Newbie..HELP!!
Hi,
Its a newbie Q!!please help........
I want to check if the integers entered are only numbers ,not strings/characters.
I know isdigit function but i suppose its in character-handled library,but I am using class string.So is there any function for checking that input values are only integers not character/strings/symbols etc;
thanks
-
-
thanks...but
Hi,
thanks for replying, but i assume, u gave me a soln in c and not vc++,
if,iam not wrong??I am unsure though.........:(
thankx
-
yes it is C but it works just as well in C++. the header file is conio.h
or at least it works on MY VC++, 4.0 that is
-
you could do it manually... personally, i dont use the string data tye cuz it takes away all your power (muwhahaha). anywayz, use a char array and check the contents:
Code:
int i;
char s[100];
cin.getline( s, '\n' );
for( i = -1; i != strlen( s ) + 1; i++ ){
//strlen checks the length of the string
if( s[i] != '1' && s[i] != '2' && s[i] != '3' ){
//your string has a symbol or whatever
//this just check 1, 2, and 3, but you get the point (i hope)
.
.
.
break; //breaks out of the for loop
}
}
remember to include string.h
im probably gonna receive some criticism for this post because some ppl might think the code "inefficient", but computers now are like 2ghz. welcome to reality folks!
also, scanf is stdio.h not conio.h
-
Yeah.. instead of using the string class, you post O(n^2) code for something that could trivially be O(N) :).
-
????
Hi,
I dont understand what SilentStrike is tryin to say????
Btw,thanks flikm and denethor2000
But as this an assignment, its expected to use a string and not char array.......also only c++ will be allowed for obvious reasons...
So can anyone please tell me how to check if the value entered is an integer and not strings/char/symbols???
thanks..........
-
Please help!
32 reads!!!still waiting!!!!!
-
>>32 reads!!!still waiting!!!!!
...meanwhile do a search on the board for integer input check... it might help
-
If you absoloutely have to have a string you can always typecast a character array into a string.
-
if I haven't misunderstood your request, here is a nifty little while loop that clears and ignores the information that was in the buffer and does exactly what you want...the only thing it will accept is numbers ;)
Code:
int num;
cout <<"pls enter num";
cin >> num;
while (cin.fail())
{
cin.clear();
cin.ignore (BUFFER, '\n');
cout << "\n pls re-enter number";
cin >> num;
}
NB: dont forget to create a BUFFER constant, that is :
const int BUFFER=128; before main.
sophie
-
thanks a bunch
Hi sophie,
it worked liked a charm......thanks a lot
but can u enlighten me about what buffer=128 ,cin.clear(),cin.fail(),cin.ignore() funcns do??????
thanks once again.
-
firstly, Im glad you found what you were after:D as for what they do.
the buffer constant you declared can be any size...I usually use 128...just like the sound of it:p the cin.fail() condition checks and returns a value, that is if the input was anything other than a number cin.fail() would return true (which is the condition for entering a loop).
the cin.clear() and cin.ignore() does exactly as they say...they ignore and clear the buffer thus cin >> num will have a clean and empty buffer to get info from.
sophie
-
ok!got it!!
thanks,
I got it!!!!!!!!!
-
another dumb Q
Hi,
is there any function like cin.fail() to check for the input value is only string and not numbers??????/
thanks..........