testing for int

A

Andre

I know I am a newbie and this will sound very elementary. Still I need
help, and don't know where to find it.



OK, here it is:



----



int vhold;



cin >> vhold;



// vhold used in a subsequent while loop

----



If input is double, crazy things happen to the display. How do I test for
only integer input?



All answers will be appreciated, exact syntax most of all. Thanx,



Andre
 
A

Andre

I just want to know how to make sure that my progrram can handle keybord
input of let's say double or char, or string, if the expected value is an
int.
 
J

Jupiter5F

I know I am a newbie and this will sound very elementary. Still I need
help, and don't know where to find it.



OK, here it is:



----



int vhold;



cin >> vhold;



// vhold used in a subsequent while loop

----



If input is double, crazy things happen to the display. How do I test for
only integer input?



All answers will be appreciated, exact syntax most of all. Thanx,



Andre
This question came up once before and I saved this sample bit of code from the
FAQ's which i'm sure you'll find handy for this problem.

#include <iostream>
#include <limits>

int main()
{
int age = 0;

while ((std::cout << "How old are you? ")
&& !(std::cin >> age)) {
std::cout << "That's not a number; ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

std::cout << "You are " << age << " years old\n";
...
}
Of course you can also print the error message when the input is out of range.
For example, if you wanted the age to be between 1 and 200, you could change
the while loop to:


...
while ((std::cout << "How old are you? ")
&& (!(std::cin >> age) || age < 1 || age > 200)) {
std::cout << "That's not a number between 1 and 200; ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
...
 
H

Heinz Ozwirk

: I know I am a newbie and this will sound very elementary. Still I need
: help, and don't know where to find it.
:
: OK, here it is:
: ----
: int vhold;
: cin >> vhold;
: // vhold used in a subsequent while loop
: ----
:
: If input is double, crazy things happen to the display. How do I test for
: only integer input?

You can't. Everything, that looks like an int, is extracted from the input stream. The first character, that is not legal for an integral number, will stop input. Reading the next item from the stream will start at that character. If you want to do your own tests, you have to read the input as a string, test it and convert it to int yourself. Something like

std::string item;
std::cin >> item;
if (item.find_first_not_of("0123456789") == item.npos)
{
int vhold = strtol(item.c_str(), 0, 10);
// do something with vhold
}
else
{
// input does not represent an integral number
}

Regards
Heinz
 
S

Shane Neph

I just want to know how to make sure that my progrram can handle keybord
input of let's say double or char, or string, if the expected value is an
int.

Hi,
int a;
cin >> a;

If the user typed 's', then a will now hold the ASCII value of 's' (an
integer).

It sounds like you want to be able to cleanly reject a bad user input -
possibly, give them a small error message and let them try again?

Here is a quick possibility:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main( )
{
int goodInput;
string toCheck = "0123456789";
bool done = false;
while ( ! done )
{
std::string takeInput;
cin >> takeInput;

// if takeInput contains any unwanted character, fails next check
if ( takeInput.find_first_not_of(toCheck) == string::npos ) // good
input
{
stringstream convert;
convert << takeInput;
convert >> goodInput;
break;
}
else // bad input
cout << "Expected integer: Received " << takeInput << endl;
} // while

/* other stuff */
return(0);
}

Good Luck,
Shane
 
D

David Harmon

int a;
cin >> a;

If the user typed 's', then a will now hold the ASCII value of 's' (an
integer).
Nonsense!

std::string takeInput;
cin >> takeInput;

std::getline(cin, takeInput); // would be better
 
S

Shane Neph

David Harmon said:
std::getline(cin, takeInput); // would be better

OK, you got me on the ASCII 's'. It's been a while since I've worried about
these type of items. Type safety is a good thing.
I was only showing a quick implementation though I don't believe
getline( ) is necessarily "better" for this simple item. Better in what
ways? Perhaps you could catch "1 howdy" where cin >> takeInput would not?
 
W

Wouter Lievens

Shane Neph said:
OK, you got me on the ASCII 's'. It's been a while since I've worried about
these type of items. Type safety is a good thing.
I was only showing a quick implementation though I don't believe
getline( ) is necessarily "better" for this simple item. Better in what
ways? Perhaps you could catch "1 howdy" where cin >> takeInput would not?

Better because you can catch an entire line. If you would'nt use getline,
it'd buffer up the rest of the input beyond the first sapce and that would
cause weird unwanted results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top