continue to get two answers.

HELP!?!? When I run the code, any number that I input bigger than 1 I get two answers. I am not sure what I am doing wrong, I have tried changing the brackets and checking the if...else statements but when I change that xCode gives me an error.


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
  #include <iostream>
using namespace std;

int main() {
    int x, y = 0;
    
    cout << "Please input an integer: ";
    cin >> x;
    
    if (x < 1)
        cout << x << "=y" << endl;
    {
        if (x < 10)
        y = (2 * x - 1);
        cout << y << "=y" << endl;
    }
    
    {
        if (x >= 10)
        y = (3 * x - 11);
        cout << y << "=y" << endl;
    }

    return 0;
}
Perhaps
1
2
3
4
5
6
7
8
9
10
11
if (x < 1) {
	cout << x << "=y" << endl;
}
else if (x < 10) {
	y = (2 * x - 1);
	cout << y << "=y" << endl;
}
else {
	y = (3 * x - 11);
	cout << y << "=y" << endl;
}
@Grime Thank you! That wasn't quite it but it got me to figure out how to fix it. I appreciate the assistance.
Topic archived. No new replies allowed.