The code doesn't recognize the range

Hi there.
I started writing some c++ codes a week ago and I run into this issue.
I wanna write a bmi calculator and this piece of code gives me problems.
It should work like a COPPA screen for a kids game, allowing you to input only numbers between a chosen range and no other characters.

1
2
3
4
5
6
7
8
   double w; //user weight
   while ((w <= 20 || w >= 250)&&(!(cin >> w))){
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');
                    cout << endl;
                    cout << "Invalid input. Please try again: \n";
                    cout << "Your weight is: ";
                    cin >> w;}

Before introducing the (!(cin >> w)){cin.clear();cin.ignore(numeric_limits<streamsize>::max(), '\n');} condition when the user entered anything else than a number the program would enter a loop and crash. Now it does't allow the user to input other characters but it does't recognize the range.
Please help me. Thank you
Last edited on
closed account (SECMoG1T)
You probably want to use this

while ((w <= 20 || w >= 250)||(!(cin >> w))) because if you give 5 as input

truth table

(w <= 20 || w >= 250)  ,  (!(cin >> w))
    true                   false               


but true && false==false so your loop will exit because it's conditions are broken.
Last edited on
I change the operator from && to || but still doesn't work as intended
closed account (SECMoG1T)
if so then this is what you want
1
2
3
4
5
6
7
8
9
10
11
12
double w; //user weight
   std::cout<<"enter your weight : ";cin>>w;
   
   while ((w <= 20 || w >= 250)||(!(cin)))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << endl;
        cout << "Invalid input. Please try again: \n";
        cout << "Your weight is: ";
        cin >> w;
  }
it works properly now. thank you very much.
you changed the && operator and not mentioned the data type in the !(condition).
nice.
Last edited on
closed account (SECMoG1T)
:} hehe you have to try this
1
2
3
4
5
6
7
8
9
10
11
double w; //user weight
   std::cout<<"enter your weight : ";

   while ((!(cin>>w))||(w <= 20 || w >= 250))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << endl;
        cout << "Invalid input. Please try again: \n";
        cout << "Your weight is: ";
  }



See now, you need to know how this operators are evaluated.
Last edited on
:( how can i restrict the use of the space bar? so you have to input e.g. "1986" and not " 1986".
Last edited on
closed account (SECMoG1T)
but your variable is of type double, doubles will never take spaces
it takes space before the input
closed account (SECMoG1T)
Really , i totally disagree with that , maybe what might be giving you the notion that it's taking spaces is that ' ' between the colon in your prompt...
cout << "Your weight is: "; ///: "; this might be it. but doubles or other integral types will never accept spaces as input.
no
in the statement
1
2
cout<<"enter your weight : ";
cin>>w;

if the user presses 10 times the space bar or just the enter button and after that inputs a valid number it works. I would like to limit that
Last edited on
Guuuuuuyyyyyyyyysssssss, He just means that. You can press space , then enter 55.

1
2
3
double x;
cin >> x;
cout << x;


input : " 55" (with a space before entering the numbers as you see, he wants the program to not let the user enter any spaces, what OP doesnt get, is that the space is not taken as an input. My thoughts about this continue at the end of this post.)

output:
55


(It continues here ->) What Im wondering is, why OP gives a shit.
Last edited on
too picky maybe :)
Well @grui_sanger You cant get around that. Because the spaces are not taken in as input, so the program cant react to that in the first place. Because the program doesnt even recognize the spaces.
Thank you @TarikNeaj, I didn't knew that
You could test this by yourself. Test this

1
2
3
double x;
cin >> x;
cout << x;


And input 25 spaces, and then one number. You will see only the one number will be printed out and the spaces will be ignored.
Topic archived. No new replies allowed.