Why cannot i use (==) or (>=) in Xcode?

I do not know why i cannot use == or >= on Xcode on mac to the this simple code.

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
#include <iostream>
#include <string>

using namespace std;

int main(){
    
    
    string user1;
    string user2;
    
    cout << "Enter the first user's age: " << "\n";
    getline( cin, user1 , '\n' );
    
    cout << "Enter the second user's age: " << "\n";
    getline( cin, user2, '\n');
    
    if (user1 > user2){
        cout << "user1 is older than user2 \n";
    }
    else if ( user1 < user2 ){
        cout << "user2 is older than user1 \n";
    }
    
    else if (user1 >= 100){    //it says here invalid operands to binary expression ("string'{aka 'basic_string<char,char_traits<char>,allocator<char> >') and 'int')
        cout << "user's age must not exceed 100 \n";
    }
    
    else if (user2 >= 100){     //it says here invalid operands to binary expression ("string'{aka 'basic_string<char,char_traits<char>,allocator<char> >') and 'int')
        cout << "user's age must not exceed 100 \n";
    }
    else {
        cout << "Both user have the same age \n";
        return 0;
    }
    
    
}



This was a practice problem from the book "Jumping into C++" by Alex Allain.

The problem says: Ask the user for two users' ages, and indicate who is older; behave differently if both are over 100.
Last edited on
Compiler says:
invalid operands to binary expression ("string'{aka 'basic_string<char,char_traits<char>,allocator<char> >') and 'int')

yet, you say:
I do not know why


Should you rather say:
"I do not understand what invalid operands to binary expression ('string' and 'int') means."

What is this "binary expression"? The user1 >= 100
There is operator >= that takes two operands. That is why it is called binary.

The operands are the user1 and the 100.

The user1 is a string. The 100 is an int.

How are you supposed to compare a number and a piece of text? The compiler does not know. It needs instructions from you.


You do ask for "age" that is intuitively a numeric value (integer), but you you read and store everything that the user writes in one line as a text object.

I could write "fortytwo and some" as the 'age' of a user and your program would be ok with that (but comparisons could yield odd outcome (for example, "fortytwo and some" < "two").

Store ages as integers.


The logic of your if..else needs more thinking. For example, you don't implement the
behave differently if both are over 100
Thank you very much keskiverto. I fixed the error, with your clarification everything makes sense now :)

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
#include <iostream>
#include <string>

using namespace std;

int main(){
    
    
    int user1 = 0;
    int user2 = 0;
    
    cout << "Enter the first user's age: " << "\n";
//    getline( cin, user1 , '\n' );
    cin >> user1;
    cout << "Enter the second user's age: " << "\n";
    cin >> user2;
    
    if (user1 > user2){
        cout << "user1 is older than user2 \n";
    }
    else if ( user2 > user1 ){
        cout << "user2 is older than user1 \n";
    }
    
    else if (user1 >= 100) {    //it says here invalid operands to binary expression ("string'{aka 'basic_string<char,char_traits<char>,allocator<char> >') and 'int')
        cout << "user's age must not exceed 100 \n";
    }
//    else if ((user1 > 100) || (user2 > 100)){    //it says here invalid operands to binary expression ("string'{aka 'basic_string<char,char_traits<char>,allocator<char> >') and 'int')
//        cout << "user's age must not exceed 100 \n";
//    }
    
//    else if (user2 == "100"){     //it says here invalid operands to binary expression ("string'{aka 'basic_string<char,char_traits<char>,allocator<char> >') and 'int')
//        cout << "user's age must not exceed 100 \n";
//    }
    else {
        cout << "Both user have the same age \n";
        return 0;
    }
    
    
}


Topic archived. No new replies allowed.