# Thread: Determining whether input is an integer.

1. ## Determining whether input is an integer.

if i ask a user to input an integer, how would i go about stopping somone from entering a character because my program would crash?

Thanks

2. Check each character to see if its a digit. Use the isdigit() function included with ctype.h to do that. Allow a negative sign at the beginning if you want negative, too.

3. Look into isdigit(). Or you could look into isalpha().

4. i know about the isdigit() and isalpha() but they take a character. I want to know if i can find out using the integer only.

5. any other suggestions?

6. I don't understand... can you show us some code?

7. posting code is bad i have over 1500 lines... i think ill just let somone dl it instead.

the problem is when a user inputs a length or a width or any numerical value. I want this to be error proof.

8. i want to know how i can let the program know when the user inputs a character instead of the integer because it crashes if i don't avoid this.

9. well if you run the program you will see that when you are supposed to enter an double, and you accidentally enter a character, the program will crash because i don't have a way of catching that. I just want to knwo how to stop it from happening.
An example :

Code:
```                                     while(x)
{
x = true;
z = true;
cout<<"Enter the Length of the beam (mm): ";
cin>>l;
if (l <= 0)
{
cout<<"The Beam must have a Length!  Enter Again."<<endl;
x = true;
}
else
{
while (z)
{
string q;
cout<<"Is this Length correct: "<<l<<" (y/n)?"<<endl;
cin>>q;
if (q == "y" || q == "Y")
{
x = false;
z = false;
}
else if (q == "n" || q == "N")
{
x = true;
z = false;
}
else
{
cout<<"You have not entered a Proper Response!"<<endl;
z = true;
}
}
}
}```
as you can see the problem lies here in these lines:

Code:
```                                             cout<<"Enter the Length of the beam (mm): ";
cin>>l;
if (l <= 0)
{
cout<<"The Beam must have a Length!  Enter Again."<<endl;
x = true;
}```
Now how do i stop people from entering a character because as you can see it'll ruin the program. If it was a character i could do it with isdigit() but its not. Hope this is more clear.
Oh yeah there is no way that functions will shorten the code up, only the main. also all the funtions i need are in 2 classes. The rest of the stuff in the main is for input

10. oh almost forgot i have another small problem. Whenever i run through the program using a certain "path", when i chose the same path again (to enter different values) , the program just skips the input and outputs exactly what i did the last time. (There is a while loop to allow the user to start over from teh top with new input). Does anyone have any ideas as to what is causing the problem. My latest version is here. Just take a run through it and chose the same options after the run and see for yourself.
Thanks again

11. This ought to fix your problem:

Code:
```#include <iostream>
#include <string>
#include <cstdlib> // for atof()

using namespace std;

int main()
{	string temp;
double value;
do
{
cout << "Enter a double: ";
cin >> temp;
value = atof(temp.c_str());
}
while (!value);
cout << value << " accepted as a valid double" << endl;
return 0;
}```

12. AFAIK there are two main data validation techiques.

The first goes something like this. Only accept user input as a string. Then parse the string and remove anything that invalidates input as the desired type or reject entire input if any part of it invalidates input for use as the desired type. Then convert input to the desired type if it is valid.

The alternative I'm aware of is to use the fail(), clear() and ignore() methods for istreams. That approach goes something like this:
Code:
```int limit = size limit of ints availabe in climit or limit.h
double temp;
//attempt to read input into desired variable
cin >> temp;
//if unable to read input into desired variable
if(cin.fail())
{
//clear the fail bit for the stream
cin.clear();
//then ignore up to limit (whatever it is) number of char
//this clears the input buffer about as well as it can be cleared
cin.ignore(limit);
}```
This might have a problem if single char input accepted by compiler as int/double/float (assigning the ASCII value of a char to a numerical type) or if you try to read an int into a char (again based on ASCII values). Another problem here is if somebody stands on a key and ends up trying to enter more char than limit (say they hold their finger on the 'k' key for three minutes), but that would also probably be a potential problem for the string method as well).

13. elad, for that to work i need to include the header file climit?

Correct?

If so how do i determine the limit size?

14. i decided it would be easier to use strings with stringstreams.

Although now i have one problem. The isdigit call is not taking characters its taking integers! This is the first time i see this. Not only that but just to see if there actually was something wrong the isalpha call is also asking for integers only.

Does anyone kow why it doesn't take the usual character and is asking only for integers?

15. here's an example:

Code:
``` 				while(x)
{
x = true;
z = true;
// Problem starts here
string test;
cout<<"Enter the Length of the beam (mm): ";
cin>>test;
if(isdigit(test.c_str()))
{
l = string_to_double(test);
}
// Problem ends here
else
{
cout<<"This program will only accept digits."<<endl;
x = true;
}

if (l <= 0)
{
cout<<"The Beam must have a Length!  Enter Again."<<endl;
x = true;
}
else
{
while (z)
{
string q;
cout<<"Is this Length correct: "<<l<<" (y/n)?"<<endl;
cin>>q;
if (q == "y" || q == "Y")
{
x = false;
z = false;
}
else if (q == "n" || q == "N")
{
x = true;
z = false;
}
else
{
cout<<"You have not entered a Proper Response!"<<endl;
z = true;
}
}
}
}```
Is this even correct? I mean should this work?
Thanks Again.