Beginner question.

I'm having a problem getting this code to work properly. When I run it, no matter what selection I put in it gives me the "Invalid Selection" error. Any ideas?

Please excuse any sloppiness and feel free to critique any methods as I am only looking to improve upon what I already know.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

class test{
public:
    int m, n;
    string c;
};

int main()
{
    test to;
    char a;
    string y, n;
    y = "Yes";
    n = "No";
    cout << "Do you want to compare:" << endl;
    cout << "1. Greater than or equal to." << endl;
    cout << "2. Less than or equal to.\n" << endl;
    cout << "Enter selection: ";
    cin >> a;
    cout << "\n";
    if (a == 1){
        cout << "Enter first number: ";
        cin >> to.m;
        cout << "\nEnter second number: ";
        cin >> to.n;
        to.c = to.m>=to.n ? y : n;
        cout << "\nThe result of if " << to.m << " is greater than, or equal to, " << to.n << " is: " << to.c << "!";
    }
    else if (a == 2){
        cout << "Enter first number: ";
        cin >> to.m;
        cout << "\nEnter second number: ";
        cin >> to.n;
        to.c = to.m<=to.n ? y : n;
        cout << "\nThe result of if " << to.m << " is less than, or equal to, " << to.n << " is: " << to.c << "!";
}
    else {
        cout << "Invalid selection.";
    }
    getch();
    return 0;
}
@wreckatech

Main thing I see as a problem, is that your variable for the selection is a char, but you're checking the input as an integer. Put single quotes around the 1 and 2 on lines 26 and 34, and that should be a big improvement.

Also, I'm not sure if it'll cause problems or not, but you have a variable called n on line 9 and it's an integer, but you've also declared an n, as a string, on line 17. Not sure, as I don't use classes very often.
@whitenite1

That worked! Thank you so much!
@wreckatech

You're very welcome.
Topic archived. No new replies allowed.