How to prevent a letter insert in a int var ?

Y

YUS

Hi,
I've got a problem.

I have a program that ask for a number to the user.
If the user put a letter instead of a number the program crashes (loop).

How can I prevent this ?
I've tried with isdigit but with no results.

int choise;

cout << "\nChoose one: ";
cin >> setw(sizeof(choice)) >> choise;
if(isdigit(choice)) cout <<"all ok";
if(isalpha(choice)) cout<<"error - number needed";

but the code doesn't work.
=(

Thanks to all.
 
A

Alf P. Steinbach

* YUS:
I've got a problem.

I have a program that ask for a number to the user.
If the user put a letter instead of a number the program crashes (loop).

How can I prevent this ?
I've tried with isdigit but with no results.

int choise;

cout << "\nChoose one: ";
cin >> setw(sizeof(choice)) >> choise;
if(isdigit(choice)) cout <<"all ok";
if(isalpha(choice)) cout<<"error - number needed";

but the code doesn't work.

Consider that attempted conversion to 'int' has already happened by the
time you call 'isdigit' and 'isalpha'.

Instead check the error state of the stream.
 
Y

YUS

Alf said:
Consider that attempted conversion to 'int' has already happened by
the time you call 'isdigit' and 'isalpha'.

Instead check the error state of the stream.

Sorry but.. how ???
:p
I really got no idea!
 
H

Howard

YUS said:
Hi,
I've got a problem.

I have a program that ask for a number to the user.
If the user put a letter instead of a number the program crashes (loop).

How can I prevent this ?
I've tried with isdigit but with no results.

int choise;

cout << "\nChoose one: ";
cin >> setw(sizeof(choice)) >> choise;
if(isdigit(choice)) cout <<"all ok";
if(isalpha(choice)) cout<<"error - number needed";

but the code doesn't work.
=(

A common solution is to always enter data into a std::string, and then see
if the string can be converted into an integer (or double or whatever). If
it cannot be converted, then tell the user there's an error and loop back to
allow them to re-enter the data.

-Howard
 
M

Mike Wahler

YUS said:
Hi,
I've got a problem.

I have a program that ask for a number to the user.
If the user put a letter instead of a number the program crashes (loop).

How can I prevent this ?
I've tried with isdigit but with no results.

int choise;

cout << "\nChoose one: ";
cin >> setw(sizeof(choice)) >> choise;
if(isdigit(choice)) cout <<"all ok";
if(isalpha(choice)) cout<<"error - number needed";

but the code doesn't work.

Look again at the documentation for 'isdigit()'.
It doesn't do what you apparently believe.

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

int get_int()
{
int result(0);
std::string text;
std::istringstream iss;;

do
{
std::cout << "Enter an integer value: ";
std::cin >> text;
iss.clear();
iss.str(text);

if(!(iss >> result))
std::cerr << " * Not a valid integer value\n";

} while(!iss);

return result;
}

int main()
{
int i(0);

i = get_int();
std::cout << "You entered: " << i << '\n';
return 0;
}

-Mike
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top