Mac and PC

I wrote this program in class today, a simple paper, rock, scissors game. When I compiled it and ran it in class everything worked. I tried compiling on my Mac in both xcode, and code blocks. In code blocks i get errors in line 47, and xcode i get an error in line 11, So my question is, Is there anything special that needs to be done when programing on a mac, or is it an operating system thing, any insight would be great. Thanks

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using   namespace std;
//Name: get user choice
//Parameter: NONE
//Return type: string - paper, rock, or scissors, based on whatever the user enters
//Body: Prompts the user for paper, rock, or sciccors.
string get_user_choice()
{
    string user_input;
    cout << "Enter paper, rock, or scissors" << endl;
    cin >> user_input;
    return user_input;
}
//Name: get cpu_choice
//Parameter: NONE
//Return type: string - paper, rock, or scissors, (randomly generated)
//Body: randomly generate paper, rock, or sciccors.
string get_cpu_choice()
{
    //generate 0,1,2
    int num = rand() % 3;
    if (num == 0)
    {
        return "paper";
    }
    else if (num == 1)
    {
        return "scissors";
    }
    else
    {
        return "rock";
    }
}
//Name: show_winner
//Parameters: user- the user selection
//            cpu- the cpu selection
//Return type: void
//Body: displays who won the game
void show_winner(string user, string cpu)
{
    //list all win configurations
    if (user == "paper" && cpu == "rock"
       || user == "rock" && cpu == "scissors"
       || user == "scissors" && cpu == "paper")

    {
        cout << "You win!" << endl;
    }
    //if the selections matched....
    else if (user == cpu)
    {
        cout << "Tie. Nobody wins." << endl;
    }
    //only other parameter tie
    else
    {
        cout << "You lost!" << endl;
    }
}
int main()
{
    //seed the rbd;
    //if we didnt do this, then the cpu would select the same thing every time
    srand(time(NULL));
    string response, user_choice, cpu_choice;
    cout << "Paper-Rock-Scissors v1.0" << endl;

    do
    {
        //get user selection
        user_choice = get_user_choice();
        //get cpu selection
        cpu_choice = get_cpu_choice();
        //determine who won
        show_winner(user_choice, cpu_choice);
        cout << "Play again?" << endl;
        cin >> response;
    } while (response == "yes");

    return 0;
}
I believe you're getting warning not error in Xcode.
I just compiled this using clang, which is what Xcode uses and I'm getting your so-called "error" on line 47.
It is not an error. It is a warning. It indicated that there is probably something wrong with your program.
In this case, it wants brackets around the ands This is because of the operator precedence in c++.
c++ gives the && precedence over ||.

Lesson to take away: whenever you do somethinbg like that, put the 2 && compared values in parentheses like this:
1
2
3
4
5
if (((user == "paper" && cpu == "rock")
       || (user == "rock" && cpu == "scissors"))
       || (user == "scissors" && cpu == "paper"))

    {

Try it like that, and you won't get any more "errors".
Further Reading: http://www.cprogramming.com/tutorial/compiler_warnings.html
Topic archived. No new replies allowed.