Cant figure out whats wrong with my code
Jan 27, 2016 at 4:28am UTC
Receive an integer input from the user.
If the original value is in the range 5 <= x <= 10 then subtract 5 from it and print it out.
Otherwise, if the original value is in the range 0 <= x <= 4 then multiply it by 2 and print it out.
If the original value is not in either range, simply print out the original value given to you by the user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
cout << "" ;
cin >> age;
if ( 5<= age <= 10)
{
age= age - 5;
cout << age;
}
else if (0 <= age <= 4)
{
age= age * 2;
cout << age;
}
else
cout << age;
return 0;
}
Jan 27, 2016 at 4:38am UTC
It's the condition in your if statement.
You want to say "if age is greater than or equal to 5 AND age is less than or equal to 10"
And in c++ you do that like:
if (age >= 5 && age <= 10)
Jan 27, 2016 at 4:38am UTC
5 <= age <= 10
does not mean what you think it does. Use 5 <= age && age <= 10
instead.
Last edited on Jan 27, 2016 at 4:38am UTC
Jan 27, 2016 at 4:43am UTC
Perfect! Thanks so much! It worked.
Topic archived. No new replies allowed.