Making a chutes and ladder game for my CS2010 class and go it knocked out pretty quick. So I thought everything was running smoothly until I tried to run the program. The problem has to do something about my "winner" function, but I've gone over my teachers notes and cannot for the life of me figure what I'm doing wrong. The error are on line 13 and 71 respectively. Hopefully this is enough info, being the first time I've posted.

//Filename: 175chutesladders.cpp
//Description: Project 4 - Chutes and Ladders Game
//Author: 175 John Prebul
//Date: 1/18/11

#include <string>
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

void winner(&int, &int);
int CheckChutesandLadders(int);
int DiceRoll();

//This is were all the magic happens! All the functions and input are combined into one beautiful program

int main()
{
	string name1, name2;
	int player1, player2, dicenum, player1rolls, player2rolls;

	
	cout<< "Chutes and Ladders (2 players)";
	cout<< "**********************************";
	cout<< "Please enter the name of Player 1: ";
	cin>> name1;
	cout<<endl;
	cout<< "Please enter the name of Player 2: ";
	cin>> name2;
	cout<<endl;
	cout<< "Alright" <<name1 <<"and" <<name2 <<", lets play Chutes and Ladders! First to 81 wins!" <<endl;
	cout<<setw(9) <<"Player" <<setw(9) <<"Move Number" <<setw(9) <<"Current Position" <<setw(9) <<"Dice Roll" <<setw(9) <<"NewPosition";
	cout<<"******************************************************************************************************************************";
	
	player1 = 1;
	player2 = 1;
	player1rolls = 1;
	player2rolls = 1;
	
	while(player1 < 81 && player2 < 81)
	{
	cout <<setw(9) <<name1 <<setw(9) << player1rolls <<setw(9) <<player1;
	dicenum = DiceRoll();
	player1 = player1 + dicenum;
	player1 = CheckChutesandLadders(player1);
	cout <<setw(9) << dicenum;
	cout<<setw(9) <<player1;
	
	player1rolls++;
	
	if(player1 >= 81)
		player1 = player1 + 0;
		
	else
	{
		cout <<setw(9) <<name2 <<setw(9) << player2rolls <<setw(9) <<player2;
		dicenum = DiceRoll();
		player2 = player2 + dicenum;
		player2 = CheckChutesandLadders(player2);
		cout <<setw(9) << dicenum;
		cout<<setw(9) <<player2;
	
		player2rolls++;
	}
	cout<<"---------------------------------------------------------------------------------------------------------------------------------------";
	
	}
	
	winner(& player1, & player2);
	
	if(player1 = -1)
		cout<< player1 <<"has won the game! Congratulations " <<player1 <<"!" <<endl;
		
	else 
		cout<< player2 <<"has won the game! Congratulations " <<player2 <<"!" <<endl;
		
	return 0;
}
//To confirm if one of the players have won the game
void winner(int & player1, int & player2)
	{
	if(player1 >= 81)
		{
			player1 = -1;
		}
	else if(player2 >= 81)
	{
		player2 = -1;
	}	
	}
// To get a number for the dice roll	
int DiceRoll()
	{
   return rand() % 6 + 1;    
	}

//To see if one of the players activated one of the chutes or ladders
int CheckChutesandLadders (int player)
{
	if(player == 11)
		return player = 4;
	
	else if (player == 15)
		return player = 7;
	
	else if (player == 30)
		return player = 21;
		
	else if (player == 44)
		return player = 31;
	
	else if (player == 58)
		return player = 43;
		
	else if (player == 64)
		return player = 54;
	
	else if (player == 8)
		return player = 16;
		
	else if (player == 12)
		return player = 27;
	
	else if (player == 28)
		return player = 39;
		
	else if (player == 33)
		return player = 46;
	
	else if (player == 48)
		return player = 55;
	
	else if (player == 59)
		return player = 75;
	
	else 
		return player;
	}

Recommended Answers

All 4 Replies

Copy line 82 into line 13. They need to match and line 13 is wrong. It needs to be "int&", not "&int".

The call on line 71 is incorrect. You are passing by reference, which is similar to passing the address behind the scenes, but not the same. You need to pass the integer, not the address. Line 71 should be...

winner(player1, player2);

I love you. Thank you.

I love you. Thank you.

I love you too. You're welcome.

I now pronounce you student and guru. You may now Genuflect toward the great VD.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.