### simple question !!

hii everybody

simple question ... how could i force the user to input 5 digits ONLY
like if the user enter 123456 error message will appear and ask him to enter it again ???
 ``1234567891011121314151617`` ``````#include using namespace std; int main() { int x; cout<<"Please enter a five digit number:\n"; cin>> x; if(x >= 10000 && x <= 99999) //some code else cout<<"Go back to kindergarten and learn how to count.\n"; }``````
yo maaan easy .... i am not the geek here ...
tell me how to make the programe ask the user for new input ...
well the if statement will only work if five digits are input. im sorry but you don't understand this? This is basic of the basic
okay... i create a function for inputs Only .. for example void input(int &x , int &b)
so if if-statement is true its going to save it as x only .... nothing more if its not error mes. will apear !!! how could i do that????...!!!
Last edited on
no the the number goes into x and is tested to see if it is a five digit number by the if statement
In order to exclude all cases where user input some invalid input (like when you expect an int and get a string) you need to get the input as string (e.g. `std::string`).

Also since at least one input need to be entered you can use `do..while` loop.

 ``123456789101112131415161718192021222324252627282930`` ``````#include #include #include #include using namespace std; int main() { int c; std::string s; cout << "Please enter a 5-digit number!" << endl; do { cin >> s; if(s.size()!=5) { cout << "You entered: " << s << "\nPlease enter a 5-digit number "; continue; } else if(isdigit(s[0]) & isdigit(s[1]) & isdigit(s[2]) & isdigit(s[3]) & isdigit(s[4])) { c = atoi(s.c_str()); break; } }while(1); cout << "Number is " << c; return 0; }``````

I will try to explain the code above:

I declare 2 variables: 1 int for storing the number you want and 1 string to store the string the user enters.

I start an infinite loop `do.. while(1)` that will never stop until I use `break`. I could use a condition in while to set it to false with the same result.
 ``1234567`` ``````condition = true; do {... //(when it's time) condition = false; ... }while(condition)``````

`if(s.size()!=5)` this checks if input has 5 characters. If it doesn't it just reloops (continues to next loop with `continue;`) and if it does where this condition is false

it checks again if the (now 5) characters are all digits:
 ``12`` ``````else if(isdigit(s[0]) & isdigit(s[1]) & isdigit(s[2]) & isdigit(s[3]) & isdigit(s[4]))``````

Here is the same as using && but faster since this is binary AND. So it could be
 ``12`` ``````else if(isdigit(s[0]) && isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3]) && isdigit(s[4]))``````
with no difference

If they are (above condition is true) the it does 2 things:
First `c = atoi(s.c_str());` converts `string` to `char *` in other words converts `std::string` to c string (from c++ to c) because `atoi` expects a `char *` argument

`atoi()` converts char * (that is c string) to int. So by the combination `c = atoi(s.c_str());` we get the number we want.

Now we don't need to loop any more so we break the loop (using `break;`)

The main parts are that you get an input and check to see if it's OK. If it is then continue using it as you please. If on the other hand it does not the ask again for an input until you get a proper input.

Hope it helped
Topic archived. No new replies allowed.