Problem with rock paper scissors game

I'm making a rock paper scissors game with password for a school project. It goes like this:
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
88
89
90
91
92
93
94
95
96
97
98
99
  #include <iostream>
#include <stdlib.h>

using namespace std;
main()
{
int x=3141592653, y, z, as, cc, v, az;
char ad[1];
char aa[1]={'a'}, ab[1]={'b'}, ac[1]={'c'};
srand (time(NULL));
z=1;
while(x==1)
{
cout<<"Welcome. To use this program, enter the password first.\nHint:The password is the first 10 digits of pi multiplied by 10x10^10."<<endl;  
cin>>y;
if(y==x)
{
z++;
}
else
{
cout<<"The password you have entered is invalid"<<endl;
}
}
while(as<=4&&cc<=5)
{
cout<<"Rock Paper Scissors:\nType (a) for rock, (b) for paper, (c) for scissors.";
cin>>ad;
if (ad==aa)
{
az=rand()%4;
else if (az==1)
{
cout<<"Player:Rock\nComputer:Rock!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
}
else if(az==2)
{
cout<<"Player:Rock\nComputer:Paper!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
cc++;
}
else
{
cout<<"Player:Rock\nComputer:Scissors!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
as++;
}
if (ad==ab)
{
az=rand()%4;
else if (az==1)
{
cout<<"Player:Paper!\nComputer:Rock!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
as++;
}
else if(az==2)
{
cout<<"Player:Paper!\nComputer:Paper!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
}
else
{
cout<<"Player:Paper!\nComputer:Scissors!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
cc++;
}
if (ad==ac)
{
az=rand()%4;
else if (az==1)
{
cout<<"Player:Scissors!\nComputer:Rock!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
cc++;
}
else if(az==2)
{
cout<<"Player:Scissors!\nComputer:Paper!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
as++;
}
else
{
cout<<"Player:Scissors!\nComputer:Scissors!"<<endl;
cout<<"Player:"<<as<<"\nComputer: "<<cc<<endl;
}
}
if(as>cc)
{
cout<<"You won!";
}
else
{
cout<<"You lost!";
}
return 0;
}

But when I run it, it shows this error message:
$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In function 'int main()':
main.cpp:32:1: error: expected '}' before 'else'
else if (az==1)
^
main.cpp:52:1: error: expected '}' before 'else'
else if (az==1)
^
main.cpp:72:1: error: expected '}' before 'else'
else if (az==1)
^

I can't figure out what's wrong with it. Some help would be appreciated.
line 32: you are missing a '}' before else
line 52: you are missing a '}' before else
line 72: you are missing a '}' before else
I take it that the password is somehow a requirement of the assignment?
I think the missing braces ending your if statements may account for you errors, as MiiniPaa pointed out.
How you space, format, and comment your code can help pinpoint problems like this considerably. I prefer indented paired braces so that any that are missing on any if statement or function are easily spotted.

I also created a rock, paper, scissors game but chose a different means of implementing it through the use of ternary/conditional operators, which condense if/else statements. You may get some ideas from this that help simplify and organize your code. It's also heavily commented so even if you are not familiar with some of the included commands, it should give you some ideas.
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <iostream>

using namespace std;

//global variables
int num,entry,wins,losses,ties,array[10];

//function for random numbers in specific range
int random(int low,int high)
{
    srand(time(NULL));//set random number seed
    return rand()%(high-low+1)+low;
}

//function for simplifying color changing console output
void changeColor(int color)
{
    HANDLE hCon;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon,color);
}

int main()
{
    //1 is rock, 2 is paper, 3 is scissors

    //loop ten times, once for each round
    for (int i=1; i<11; i++)
    {
        num=random(1,3);//computer choice each turn
        cout<<"Please make your move (1=rock, 2=paper, 3=scissors): ";
        cin>>entry;

        while (entry<1 || entry>3)
        {
            cout<<entry<<" is not a valid entry!\n";
            cout<<"Please select 1 for rock, 2 for paper, or 3 for scissors: ";
            cin>>entry;
        }
        //only continue to this point with a valid entry

        //if win
        if ((num==3 && entry==1) || (num==2 && entry==3) || (num==1 && entry==2))
        {
            cout<<"You chose ";
            cout<<((entry==1) ? "rock" : (entry==2) ? "paper" : "scissors");
            cout<<" and I chose ";
            cout<<((num==1) ? "rock." : (num==2) ? "paper." : "scissors.");
            changeColor(10);
            cout<<" You win!\n";
            wins++;
            array[i-1]=1;
        }
        //if loss
        if ((entry==3 && num==1) || (entry==2 && num==3) || (entry==1 && num==2))
        {
            cout<<"You chose ";
            cout<<((entry==1) ? "rock" : (entry==2) ? "paper" : "scissors");
            cout<<" and I chose ";
            cout<<((num==1) ? "rock." : (num==2) ? "paper" : "scissors.");
            changeColor(12);
            cout<<" You lose!\n";
            losses++;
            array[i-1]=2;
        }
        //if tie
        if (entry==num)
        {
            cout<<"We both chose ";
            cout<<((entry==1) ? "rock." : (entry==2) ? "paper." : "scissors.");
            changeColor(14);
            cout<<" Tied!\n";
            ties++;
            array[i-1]=3;
        }
        changeColor(7);//switch back to default color before continuing to next round
    }//after 10 rounds

    changeColor(10);cout<<"\t\tW"; changeColor(7);cout<<" = Win   ";
    changeColor(12);cout<<"L"; changeColor(7);cout<<" = Loss   ";
    changeColor(14);cout<<"T"; changeColor(7);cout<<" = Tie  "<<endl;
    cout<<"Round:    1    2    3    4    5    6    7    8    9   10\n";
    cout<<"Result:   ";

    //go through array, outputting results of each round
    for (int i=1;i<11;i++)
    {
        //switch to appropriate color based on win/loss/tie
       if (array[i-1]==1) changeColor(10);
       if (array[i-1]==2) changeColor(12);
       if (array[i-1]==3) changeColor(14);
       cout<<((array[i-1]==1) ? "W" : (array[i-1]==2) ? "L" : "T")<<"    ";
       changeColor(7);//change back to default color
    }

    cout<<"\nYou won "<<wins;
    cout<<((wins>1) ? " times, " : " time, ");
    cout<<"and lost "<<losses;
    cout<<((losses>1) ? " times, " : " time, ")<<"while ";
    if (ties!=0) cout<<ties;
    cout<<((ties>1) ? " games were ties.\n" : (ties==1) ? " game was a tie.\n" : " no games were ties.")<<"\n";

    if (wins>losses) changeColor(10);
    if (losses>wins) changeColor(12);
    if (wins==losses) changeColor(14);
    cout<<((wins>losses) ? "You win!" : (wins<losses) ? "You lose!" : "Tie game!");
    changeColor(7);//switch back to default color before program termination
    cout<<endl;
}
Last edited on
Topic archived. No new replies allowed.