How can I check in c++ if a string or character was entered in an integer field.
Exg.
Enter a number:
user enters: a
I want the program to output an error message.
2 replies to this topic
#1
Posted 22 November 2010 - 06:35 PM
|
|
|
#2
Posted 22 November 2010 - 10:40 PM
Hi,
you must take the input into a character array. Then through a loop check all the characters in the array are between '0' && '9'. Try it, and let us know how does it go.
Munir
you must take the input into a character array. Then through a loop check all the characters in the array are between '0' && '9'. Try it, and let us know how does it go.
Munir
#3
Posted 22 November 2010 - 10:54 PM
You may use the ctype functions in ctype.h header to check input, a small example:
#include <iostream>
#include <ctype.h>
using namespace std;
int main ()
{
unsigned char myinput;
cin >> myinput;
if(!isdigit(myinput)) {
cout << "Please enter an integer.\n";
}
return 0;
}
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









