472,340 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,340 software developers and data experts.

How to prevent a letter insert in a int var ?

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.

--
oooO Oooo
( ) YUS ( )
\ ( Para todos, todo, ) /
\_ ) nada para nosotros. ( _/
Jan 31 '06 #1
6 5568
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 31 '06 #2
YUS
Alf P. Steinbach wrote:
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!

--
oooO Oooo
( ) YUS ( )
\ ( Para todos, todo, ) /
\_ ) nada para nosotros. ( _/
Jan 31 '06 #3

YUS wrote:
Alf P. Steinbach wrote:
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!


http://www.parashift.com/c++-faq-lit....html#faq-15.2

Gavin Deane

Jan 31 '06 #4

"YUS" <mo******@NOSPAMtin.it> wrote in message
news:43***********************@reader1.news.tin.it ...
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

Jan 31 '06 #5

"YUS" <mo******@NOSPAMtin.it> wrote in message
news:43***********************@reader1.news.tin.it ...
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
Jan 31 '06 #6
YUS
YUS wrote:
Thanks to all.


I repeat, thanks to all guys =)
Very useful.

--
oooO Oooo
( ) YUS ( )
\ ( Para todos, todo, ) /
\_ ) nada para nosotros. ( _/
Jan 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: phil_nospam_schmidt | last post by:
I am trying to prevent a user from resizing a frame beyond its "natural" size as given by winfo_reqwidth and winfo_reqheight, without any success....
3
by: kjaggi | last post by:
I am trying to either write a trigger or a check constraint to prevent duplicates in my table. There are two columns I need to look at for the...
1
by: em_lom | last post by:
I have a simplelist of names that I got from the database. I have written all the names from the simple list to the screen. what I'm looking...
4
by: sanjana | last post by:
hi i m using the win32_diskdrive class for detecting a memory card insertion and removal..and this class detects memory card(sd card..etc)...
1
by: ntuyen01 | last post by:
Hi All, I have this data file with fix length(see below). I am able to insert it into the database using bcp, but now I want to skip (do not...
3
by: groups2 | last post by:
When you press the down key while in an input field the default behavior for some event creates a dropdown of the previously input text. What...
9
by: Ulterior | last post by:
Hi, everyone, I have a simple problem, which bothers me for some while. I will try to explain it - There is some string, whith different...
3
by: Kalaine | last post by:
MSAccess 2003: Users tab through the data entry fields on a form, and accidently tab to the next record thereby erroneously inserting a record....
6
by: teser3 | last post by:
I have my PHP inserting into Oracle 9i. But how do I prevent duplicate record entries? I only have 3 fields in the insert in the action page: ...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.