how to allow user to input only integer?
hello,
my programm must allow user to enter only integer values, if user enters char or string programm gives him a warning and asks to enter value again..
its nothing hard but i am missing here something..
ill write u what i have tried so ull better understand the problem and i hope u guys can help me
Code:
#include <iostream>
#include <iomanip.h>
#include <sstream>
using namespace std;
int main()
{
string mystr;
int number=0;
cout<<"please enter a number!"<<endl;
cin>>mystr;
stringstream(mystr) >> number; //converting string to int
if(number==0)
cout<<"U've entered not an integer!!"<<endl;
<and the rest of the code>
return 0;
}
if i enter character then function stringstream(mystr) returns 0 and all is great but if i enter 0 program doesnt understand that its a number and sends it to if(number==0) structure and i get message "U've entered not an integer!!"
i could solve the problem if i used char instead of str, but char can contain only one character so with 2 or more digit numbers it isnt working..
can You guys tell me how can i be able to enter only integer values?
Thx You a lot!
p.s. One smart guy told me how to solve this, thx anyways ;)