accept only numbers

Hi, I just wanna ask how can I do this?

I wrote a program like that on a store and during transactions I want some conditions like this:

sample:
The total amount to be paid is: ____
Enter the amount tendered: ________ (how can I make this to accept only numbers and no other -- no letters,etc just numbers)
I tried including an If condiiton but it never works, how can I address a letter that is input on the question..

Please help, thanks.
Try iterating over it with a loop and using something like
if (str.find("a") != std::string::npos) // ask user to try again
Or you could put the whole input thing in a while loop:
1
2
3
do {
    // Ask for input, if a letter is found, ask again.
} while (str.find("a") != std::string::npos);

You'll need to define several more clauses - one for each letter.

You could even have a boolean variable that's set to true once you've iterated once, so you can inform the user that they typed a letter.
Use std::cin >> some_number; and then check to see if(std::cin).
I always got errors if a letter was entered into a string.
thanks for the reply..

is there a way to just do it on one condition? I mean using the ascii value?
well there is no such thing in the std if thats what you're asking.

You're right about the ascii values, and this is about as simple as it gets

read a character from stream
check if its >= '0' and <= '9'
if so its ok, if not, ask to input again



I also imagine you'd like only the numbers to appear on screen, to do that I use OS functions (SetConsoleMode() for windows) and set them not to echo. Than, when you get a valid number you just write it yourself.


EDIT: an working snippet
void echo(){//Changes echo, include windows.h call to turn off/on
DWORD mode;
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode^ENABLE_ECHO_INPUT);
};

Last edited on
you could try getch()/_getch() which does not echo the character entered...

1
2
3
4
5
6
7
    while(true)
   {
         c = _getch();
         if(c >= '1' || c <= '9')
             break;    
   }
 ...
Don't use getch.
You have to include conio.h, and as you should be able to guess by the .h extension, it's non-standard and depreciated.
Why go the lazy getch route when someone just posted a better method?

And if you find a UNIX method, using the preprocessor you can make binaries for*nix systems too.
If you simply want to force an integer value, then you must check that the user ENTERed only an integer value.

Here's something fun I wrote a while back:
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827

Even if you don't use the class, the principle of looping until an appropriate input is obtained is the important point here.

Hope this helps.
interesting pice of code Duoas : ]
I just read an FAQ on this the other day that I really liked. It is nearly identical to Duoas's simpler example however it also includes info on how to get the newline out of the input stream after the successful read. I think that the other IOSTREAM FAQs on this site are also worth reading.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
The problem with the FAQ is, of course, that you can enter invalid input after the number. So, if I were to run it as:
D:\prog\foo> a.exe
How old are you? foo
That's not a number; How old are you? 23 hex
You are 23 years old

D:\prog\foo> a.exe
How old are you? 0x23
You are 0 years old

What is valid input, then? You need to be explicit. My example accounts for this kind of nonsense:
D:\prog\foo> a.exe
Please enter an integer> foo
Please, enter only an INTEGER> 23 hex
Please, enter only an INTEGER> 0x23
Please, enter only an INTEGER> 35
Good job!
You entered the number 35

The OP of the other thread I linked was specifically interested in this kind of error checking.

For the record, you should always read the newline whenever user input is involved, because the user expects to press ENTER after every input.

Anyhow, thanks for the feedback!
Topic archived. No new replies allowed.