### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203`` `````` #include #include #include #include #include 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 < 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.