What's wrong

Im new to programming, i'm running out of time i need someone to help me to fix this code. This is monopoly code, after i made 2 player, it gone wrong, if there is another way to make 2player, plz tell me . thx
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

#include <iostream>
#include <ctime>
#include <string>
#include <stdio.h>
#include <conio.h>

using namespace std;

void declare(); //prototype
void func_pos(player play);//functions

struct places {
    string name;
    int price;
    int rent;
    int number;
    int owner;
};

struct player {
    int money;
    int position;
};

player p1;
player p2;
places square[ 13 ];

void declare()
{
       p1.money = 350;
       p1.position = 0;
       p2.money = 350;
       p2.position = 0;

       square[ 0 ].name = "Start";
       square[ 0 ].number = 0;
       square[ 0 ].owner = 0;
       square[ 0 ].price = 0;
       square[ 0 ].rent = 0;

       square[ 1 ].name = "Leytonstone (Cost 200)";
       square[ 1 ].number = 1;
       square[ 1 ].owner = 0;
       square[ 1 ].price = 200;
       square[ 1 ].rent = 50;

       square[ 2 ].name = "Leyton (Cost 160)";
       square[ 2 ].number = 2;
       square[ 2 ].owner = 0;
       square[ 2 ].price = 160;
       square[ 2 ].rent = 40;

       square[ 3 ].name = "Stratford (Cost 150)";
       square[ 3 ].number = 3;
       square[ 3 ].owner = 0;
       square[ 3 ].price = 150;
       square[ 3 ].rent = 35;

       square[ 4 ].name = "Mile End (Cost 80)";
       square[ 4 ].number = 4;
       square[ 4 ].owner = 0;
       square[ 4 ].price = 80;
       square[ 4 ].rent = 15;

       square[ 5 ].name = "Bethnal Green (Cost 160)";
       square[ 5 ].number = 5;
       square[ 5 ].owner = 0;
       square[ 5 ].price = 160;
       square[ 5 ].rent = 40;

       square[ 6 ].name = "Liverpool Street (Cost 120)";
       square[ 6 ].number = 6;
       square[ 6 ].owner = 0;
       square[ 6 ].price = 120;
       square[ 6 ].rent = 30;

       square[ 7 ].name = "Bank (Cost 90)";
       square[ 7 ].number = 7;
       square[ 7 ].owner = 0;
       square[ 7 ].price = 90;
       square[ 7 ].rent = 20;

       square[ 8 ].name = "St. Paul's (Cost 110)";
       square[ 8 ].number = 8;
       square[ 8 ].owner = 0;
       square[ 8 ].price = 110;
       square[ 8 ].rent = 35;

       square[ 9 ].name = "Chancery Lane (Cost 80)";
       square[ 9 ].number = 9;
       square[ 9 ].owner = 0;
       square[ 9 ].price = 80;
       square[ 9 ].rent = 15;

       square[ 10 ].name = "Holborn (Cost 60)";
       square[ 10 ].number = 10;
       square[ 10 ].owner = 0;
       square[ 10 ].price = 60;
       square[ 10 ].rent = 10;
       
       square[ 11 ].name = "Tottenham Court Road (Cost 60)";
       square[ 11 ].number = 10;
       square[ 11 ].owner = 0;
       square[ 11 ].price = 60;
       square[ 11 ].rent = 10;

       square[ 12 ].name = "Oxford Circus (Cost 150)";
       square[ 12 ].number = 10;
       square[ 12 ].owner = 0;
       square[ 12 ].price = 60;
       square[ 12 ].rent = 10;

}



void func_pos(player play) //position function
{
    int choose;
    int x = play.position;

    cout<< "Player1 are at "<< square[ x ].name <<'\n';

    if( play.position != 0 & square[ x ].owner == 0)
    {
        cout << "1.Buy\n";
        cout << "2.Don't buy\n";
        cin >> choose;

        switch(choose)
        {
            case 1:
                play.money -= square[ x ].price;
                square[ x ].owner = 1;
                cout<< "You've bought " << square[x].name <<endl;
                cout<< "You have " << play.money <<" left."<<endl;
                
            case 2:
                cout<< "End of Player1 turn "<<endl<<endl;
        }
    }

    else if( play.position != 0 & square[ x ].owner != 0)
    {
        cout<< "Player1 paid " << square[ x ].rent << " as rent\n";
    }


}

int dice( ) //roll 2 random dices
{
    int d1,d2,roll;

    d1 = rand()%6 + 1;
    d2 = rand()%6 + 1;
    roll = d1 + d2;

    cout<<"Player1 rolled "<< d1 <<" and " << d2<<"." <<endl <<"You moved " << roll <<" spaces." << endl;
        
    return roll;
}

int who_start()
{
    int p1_die, p2_die;
    p1_die = rand()%6 + 1;
    p2_die = rand()%6 + 1;
    cout<<"This is 2 player monopoly\n";
    cout<<"To determine who goes first both player will roll dices\n";
    cout<<"Player1 rolled "<<p1_die<< endl <<"Player2 rolled "<<p2_die<<endl;
    
     if (p1_die > p2_die)
    {
        cout<<"Player1 has higher value therefore  goes first\n ";
    }
    else
    {
        cout<<"Player2 has higher value therefore goes first\n ";
    }
}

int main()
{
    declare();
    srand(time(NULL));
    who_start();            
    do
    {
        int d;
        d = dice();
                
        play.position += d;
                        
        func_pos(player play);

    } while( p1.money||p2.money <= 0);


    return 0;
}
Need to know what error you are getting
this is the part in main is not functioning, i think it needs to be in global variable, "play" is to see whos turn it is

play.position += d;

func_pos(player play);
actual error
1>------ Build started: Project: fromscratch, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(11): error C2065: 'player' : undeclared identifier
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(11): error C2146: syntax error : missing ')' before identifier 'play'
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(11): error C2182: 'func_pos' : illegal use of type 'void'
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(11): error C2059: syntax error : ')'
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(120): error C2365: 'func_pos' : redefinition; previous definition was 'data variable'
1> c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(11) : see declaration of 'func_pos'
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(126): warning C4554: '&' : check operator precedence for possible error; use parentheses to clarify precedence
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(145): warning C4554: '&' : check operator precedence for possible error; use parentheses to clarify precedence
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(188): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(195): error C2065: 'play' : undeclared identifier
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(195): error C2228: left of '.position' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(197): error C2275: 'player' : illegal use of this type as an expression
1> c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(21) : see declaration of 'player'
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(197): error C2146: syntax error : missing ')' before identifier 'play'
1>c:\users\vengeur\documents\visual studio 2010\projects\fromscratch\fromscratch\main.cpp(197): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
it gone wrong

A very useless description of the errors. What's your compiler telling you?

Here's a few things anyway:
1. move this:
void func_pos(player play);
BELOW your struct delclaration for player (which starts on line 21)

2. This:
if( play.position != 0 & square[ x ].owner == 0)
needs 2 ampersands, not one. I'd also put some brackets in to make sure you are checking exactly what you think you are checking.

3. This:
play.position += d;

You have not told your compiler what a "play" is.

4. This:
func_pos(player play);
You dont need to pass the type in.

(Looks like on line 194 you want something like this:
 
player play;


5. you have told your compiler that who_start() returns an int, but your method does not do this.
Last edited on
here is my working single player, could you just add to it second player or reference where i can find that cod eplz i dont have time to fix codes, the previous code is ruined badly

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

void declare();
int dice();
void func_pos();

struct places {
string name;
int price;
int rent;
int number;
int owner;
};

struct player1 {
int money;
int position;
};



player1 p1;
places square[ 13 ];



void declare()
{
p1.money = 350;
p1.position = 0;


square[ 0 ].name = "Start";
square[ 0 ].number = 0;
square[ 0 ].owner = 0;
square[ 0 ].price = 0;
square[ 0 ].rent = 0;

square[ 1 ].name = "Leytonstone (Cost 200)";
square[ 1 ].number = 1;
square[ 1 ].owner = 0;
square[ 1 ].price = 200;
square[ 1 ].rent = 50;

square[ 2 ].name = "Leyton (Cost 160)";
square[ 2 ].number = 2;
square[ 2 ].owner = 0;
square[ 2 ].price = 160;
square[ 2 ].rent = 40;

square[ 3 ].name = "Stratford (Cost 150)";
square[ 3 ].number = 3;
square[ 3 ].owner = 0;
square[ 3 ].price = 150;
square[ 3 ].rent = 35;

square[ 4 ].name = "Mile End (Cost 80)";
square[ 4 ].number = 4;
square[ 4 ].owner = 0;
square[ 4 ].price = 80;
square[ 4 ].rent = 15;

square[ 5 ].name = "Bethnal Green (Cost 160)";
square[ 5 ].number = 5;
square[ 5 ].owner = 0;
square[ 5 ].price = 160;
square[ 5 ].rent = 40;

square[ 6 ].name = "Liverpool Street (Cost 120)";
square[ 6 ].number = 6;
square[ 6 ].owner = 0;
square[ 6 ].price = 120;
square[ 6 ].rent = 30;

square[ 7 ].name = "Bank (Cost 90)";
square[ 7 ].number = 7;
square[ 7 ].owner = 0;
square[ 7 ].price = 90;
square[ 7 ].rent = 20;

square[ 8 ].name = "St. Paul's (Cost 110)";
square[ 8 ].number = 8;
square[ 8 ].owner = 0;
square[ 8 ].price = 110;
square[ 8 ].rent = 35;

square[ 9 ].name = "Chancery Lane (Cost 80)";
square[ 9 ].number = 9;
square[ 9 ].owner = 0;
square[ 9 ].price = 80;
square[ 9 ].rent = 15;

square[ 10 ].name = "Holborn (Cost 60)";
square[ 10 ].number = 10;
square[ 10 ].owner = 0;
square[ 10 ].price = 60;
square[ 10 ].rent = 10;

square[ 11 ].name = "Tottenham Court Road (Cost 60)";
square[ 11 ].number = 10;
square[ 11 ].owner = 0;
square[ 11 ].price = 60;
square[ 11 ].rent = 10;

square[ 12 ].name = "Oxford Circus (Cost 150)";
square[ 12 ].number = 10;
square[ 12 ].owner = 0;
square[ 12 ].price = 60;
square[ 12 ].rent = 10;
}

int dice( ) //roll 2 random dices
{
int d1,d2,roll;

d1 = rand()%6 + 1;
d2 = rand()%6 + 1;
roll = d1 + d2;

cout<<"You rolled "<< d1 <<" and " << d2<<"." <<endl <<"You moved " << roll <<" spaces." << endl;

return roll;
}

void func_pos()
{
int choose;
int x = p1.position;


cout<< "You are at "<< square[ x ].name <<'\n';

if( p1.position != 0 & square[ x ].owner == 0)
{
cout << "1.Buy\n";
cout << "2.Don't buy\n";
cin >> choose;

switch(choose)
{
case 1:
p1.money -= square[ x ].price;
square[ x ].owner = 1;
cout<< "You've bought " << square[x].name <<endl;
cout<< "You have " << p1.money <<" left."<<endl;

case 2:
cout<< "End of your turn "<<endl<<endl;
}
}

else if( p1.position != 0 & square[ x ].owner != 0)
{
cout<< "You paid " << square[ x ].rent << " as rent\n";
}
}

int main()
{
declare();
srand(time(NULL));


do
{
int g;
g = dice();


p1.position += g;
func_pos();
}while( p1.money <= 0);

return 0;


}
Topic archived. No new replies allowed.