I am a real beginner with C++ and I am trying to make an if statement to work. My problem is that the variable age gets the value of 50 if at cin >> age a number greater that 30 in inserted. If I insert a number smaller that 30 the variable works normally.
So, only the if part works in my code. In other cases the output is the code inside of else if AND the variable age gets 50 as its value.
Seems weird i ran that statement and it take the right values, if there's more in your code beside that maybe there's the problem or in the way you define your variables.
tl;dr: works fine for me, check values and that stuff.
#include<iostream>
//#include<cmath> // not necessary, commented
usingnamespace std;
int main()
{
int age;
cout << "put a number 30 to 50: ";
cin >> age;
while (age <= 29 || age >= 51)
{
cout << endl << "try again, number 30 to 50: ";
cin >> age;
}
cout << endl <<"the age you entered is " << age;
return 0;
}
simpler is better. started with a simple if statement, you get a simple while loop instead.