If I write 1 letter, I get the error message once.
If I write 3 letters, I get the error message three times......

How can I fix this :S?

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <stdlib.h>
using namespace std;


int main()
{
    //vars
   char user_numberr;
   //int user_number;

cout
<<"Welcome! \nPlease pick a number between 1 and 5."
<<endl;
cin>>user_numberr;

while(!isdigit(user_numberr))
{
 cout 
 <<"I said a number bewtween 1 and 5!"
 <<endl
 <<"Try again :";
    cin>>user_numberr;
 }
int user_number = atoi(&user_numberr);
    
cout
<<user_number
<<"WEEEEEEEEEEEEEEEEEE PIIIIIIIII CIIIII LIIII ;OOO"
<<endl;

 system("pause");
 return 1;
}

Recommended Answers

All 5 Replies

Once you put in a number (1st character) and hit enter (2nd character), the input buffer has the extra '\n' from enter. Get rid of it with a cin.ignore() after each of the cin statements.

Once you put in a number (1st character) and hit enter (2nd character), the input buffer has the extra '\n' from enter. Get rid of it with a cin.ignore() after each of the cin statements.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <stdlib.h>
using namespace std;


int main()
{
    //vars
   char user_numberr;
   int user_number;

cout
<<"Welcome! \nPlease pick a number between 1 and 5."
<<endl;
cin>>user_numberr;
cin.ignore();

while(!isdigit(user_numberr))
{
 cout 
 <<"I said a number bewtween 1 and 5!"
 <<endl
 <<"Try again :";
    cin>>user_numberr;
    cin.ignore();
 }
user_number = atoi(&user_numberr);
    
cout
<<user_number
<<"WEEEEEEEEEEEEEEEEEE PIIIIIIIII CIIIII LIIII ;OOO"
<<endl;

 system("pause");
 return 1;
}

If I type in 1 or 2 letters, it will only output the error once.
But if I input 3 letters, it will give me the error twice,
if I put in 4 letters, it will give me the error three times.

I didn't know you wanted to get that fancy. Just kidding. That usage of cin.ignore assumes a streamsize of 1, change it to cin.ignore(256,'\n'); to get it to ignore up to 256 characters (you can obviously make it ignore more) until it hits a newline.

That stops the repeated errors but it still accepts letters - try entering "9t2" or anything in that order.

The question in the code states "Please pick a number between 1 and 5.". Is that important?

You are correct about that Dingbats. I guess I envisioned the OP checking the number as a next step.

I had suggested that the OP use a character for input in another thread (I think it's the same OP) and check the ASCII value was between '1' and '5', but I don't know what happened with that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.