What's My Range Again?

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.


(Not really sure why my code is not working)






#include <iostream>
#include <string>
using namespace std;

int main()

{
int age;
cout << "";
cin >> age;

if (5 <= age && age <= 10)
{
age = age - 5;
cout << age;
}
if (0 <= age && age <= 4)
{
age = age * 2;
cout << age;
}

else
{cout << age;}

return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    int age;
    std::cout << "age? ";
    std::cin >> age;

    if( 5 <= age && age <= 10 ) age -= 5 ; // subtract 5 from it
    else if( 0 <= age && age <= 4 ) age *= 2 ; // multiply it by 2 
    // else leave it unchanged as the the original value

    std::cout << age << '\n' ; // in any case, print it out
}
1
2
3
4
5
6
7
8
9
if (5 <= age && age <= 10)
{
age = age - 5;
cout << age;
}
if (0 <= age && age <= 4)
{
age = age * 2;
cout << age;

There are integers where both these conditions hold true. Lets use 6 as our example.
> age = 6;
> First condition
> 5 < age < 10
> age - 5
> cout << age // which is 1
> Second conditon
> 0 < age < 4
> age * 2
> cout << age // which is 2
>>> displays: 12

You are close, you need to double check your if statement
Last edited on
Topic archived. No new replies allowed.