Input

I was wondering if there is a way that i could refuse a user's input if it was a float data type, or rather make the user's input strictly to integers? For example;

1
2
3
4
5
6
7
8
9
int x;

cout<<"enter a number: " <<endl;
cin >> x;
while(!(cin>>x))|| x < 0) // refuses char and negative 
   {                     // how to also refuse float?
   cout<<"Re-enter a number: "
   cin>>x
   }
Yup, it's known as "Type Casting" in this case "Explicit Conversion". Check this out: http://www.cplusplus.com/doc/tutorial/typecasting/

It can lead to trouble in some cases but you should be alright as long as you're only increasing the percision of the number.


Wow, I could not have mis read that any more then I did. I will go sulk in a corner now.
Last edited on
How about inputting the data into a string and then searching for non-digit non-minus characters including decimal points and the letter E?

@Computergeek01:
I think what the OP wants is for the program to demand another input if it resembles anything other than an integer.

-Albatross
Last edited on
Topic archived. No new replies allowed.