floating exception. please help
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "dice.h"
const unsigned int MAXSQUARES = 40;
const unsigned int MAXPLAYERS = 4;
const unsigned int MAXCARDS = 25;
// The different kinds of cards.
enum cardKind_t {
C_NOP,
C_ABS,
C_REL,
C_UTIL,
C_RR
};
// A single card
struct card_t {
cardKind_t kind;
int value; // Either absolute position or relative movement
string name;
};
// A deck of cards
struct deck_t {
unsigned int curCard; // The next card to deal
unsigned int numCards; // The total number of cards
card_t cards[MAXCARDS];
};
// The different kids of squares.
enum squareKind_t {
S_PROP,
S_OTHER,
S_GO,
S_JAIL,
S_TOJAIL,
S_RR,
S_UTIL,
S_CHANCE,
S_CHEST
};
// A single square
struct square_t {
squareKind_t kind;
string name;
};
// The board
struct board_t {
int goSquare; // Which square is go
int jailSquare; // Which square is jail
unsigned int numSquares; // How many squares total
square_t squares[MAXSQUARES];
};
struct player_t {
bool inJail; // True if player "in jail", false otherwise
unsigned int throws; // If player "in jail", counts # of rolls so far
// to get out.
// If player is not "in jail", counts # of
// consecutive doubles thrown.
unsigned int location; // The player's current position
};
struct game_t {
board_t board;
deck_t chance;
deck_t chest;
unsigned int numPlayers; // Total number of players
player_t players[MAXPLAYERS];
unsigned int rounds; // Rounds remaining to play
bool isSim; // True->no move-by-move output
};
board_t * getBoard(char sFileName[], board_t * board);
// REQUIRES: a char* indicaiting the file name for the board file
// a board_t* - the same as the returned one - MUST be already initialized
// EFFECTS: reads data from a board file, puts it in a board_t struct and
// returns the pointer to this struct
deck_t * getDeck(char sFileName[], deck_t * deck);
// REQUIRES: a char* indicaiting the file name for the deck file
// a deck_t* - the same as the returned one - MUST be already initialized
// EFFECTS: reads data from a deck file, puts it in a deck_t struct and
// returns the pointer to this struct
void playGame(game_t game);
// REQUIRES: a game_t structure with all data
// EFFECTS: implements the game simulation
int main(int iNumArgs, char * sArgs[])
{
if (iNumArgs<7)
{
cout<<" Improper arguments."<<endl;
exit(1);
}
game_t game;
getBoard(sArgs[1], &(game.board)); //Reading the data from the boardfile
getDeck(sArgs[2], &(game.chance));//Reading the data form the chance deck file
getDeck(sArgs[3], &(game.chest));//Reading the data from the chest deck file
game.numPlayers=(unsigned int)atoi(sArgs[4]); //Getting the Players Number
if (game.numPlayers>MAXPLAYERS)
game.numPlayers=MAXPLAYERS;
game.rounds=(unsigned int)atoi(sArgs[5]); //Getting the Rounds Number
game.isSim=!strcmp("yes", sArgs[6]);
playGame(game);
}
board_t * getBoard(char sFileName[], board_t * board)
{
ifstream boardFile(sFileName);
string tmp; // the buffer string for file reading
unsigned int index;
boardFile >> tmp;
board->numSquares=atoi(tmp.c_str());
if (board->numSquares>MAXSQUARES)
board->numSquares=MAXSQUARES;
index=0;
while (index<board->numSquares && boardFile >> tmp)
{
if (tmp=="PROP")
board->squares[index].kind=S_PROP;
else
if (tmp=="RR")
board->squares[index].kind=S_RR;
else
if (tmp=="UTIL")
board->squares[index].kind=S_UTIL;
else
if (tmp=="OTHER")
board->squares[index].kind=S_OTHER;
else
if (tmp=="GO")
{
board->squares[index].kind=S_GO;
board->goSquare=index;
}
else
if (tmp=="JAIL")
{
board->squares[index].kind=S_JAIL;
board->jailSquare=index;
}else
if (tmp=="TOJAIL")
board->squares[index].kind=S_TOJAIL;
else
if (tmp=="CHANCE")
board->squares[index].kind=S_CHANCE;
else
if (tmp=="CHEST")
board->squares[index].kind=S_CHEST;
getline(boardFile, tmp, '\n');
board->squares[index++].name=tmp;
}
boardFile.close();
return board;
}
deck_t * getDeck(char sFileName[], deck_t * deck)
{
ifstream deckFile(sFileName);
string tmp; // the buffer string for file reading
unsigned int index;
deckFile >> tmp;
deck->numCards=atoi(tmp.c_str());
if (deck->numCards>MAXCARDS)
deck->numCards=MAXCARDS;
deck->curCard=0;
index=0;
while (index<deck->numCards && deckFile >> tmp)//can be done with a for loop
{
if (tmp=="NOP")
deck->cards[index].kind=C_NOP;
else
if (tmp=="ABS")
deck->cards[index].kind=C_ABS;
else
if (tmp=="REL")
deck->cards[index].kind=C_REL;
else
if (tmp=="UTIL")
deck->cards[index].kind=C_UTIL;
else
if (tmp=="RR")
deck->cards[index].kind=C_RR;
deckFile >> tmp;
deck->cards[index].value=atoi(tmp.c_str());
getline(deckFile, tmp, '\n');
deck->cards[index++].name=tmp;
}
deckFile.close();
return deck;
}
void playGame(game_t game)
{
unsigned int index; //used as index in loops
unsigned int currentPlayer; //the index of current player
unsigned int dice1; //the value of the first dice
unsigned int dice2; //the value of the second dice
bool visiting; // true the player is visiting the jail, is not in jail or is getting out from the jail grace to the 3d throw
bool moved; // true if current player throwed the dices, do not remains in jail and has not the 3d double
bool doubles; // true if current player have doubles and continue to trow the dices
bool passed; // true - 3d doubles, Absolute Card in Jail or ToJail Square
unsigned long freq[game.board.numSquares]; //the frequences array
card_t card; //will be used to store current card drowed from the chest or chance deck
//Initializing the frequences array
for (index=0; index<game.board.numSquares; index++)
freq[index]=0;
//Initializing the players array
for (index=0; index<game.numPlayers; index++)
{
game.players[index].inJail=0;
game.players[index].throws=0;
game.players[index].location=game.board.goSquare;
}
visiting=true;
doubles=false;
currentPlayer=0;
index=0; // the number of passed rounds
if (game.isSim && game.rounds)
cout<<endl<<"--- R O U N D "<<index+1<<" ---"<<endl;
//An iteraton for each dices throw
while (index<game.rounds)
{
moved=true;
passed=false;
//Throwing the dices
dice1=roll_die();
dice2=roll_die();
if (game.isSim)
cout<<" The Player "<<currentPlayer+1<<" rolled: "<<dice1<<"x"<<dice2<<" "<<endl;
//Processing the case of players in jail
if (game.players[currentPlayer].inJail)
{ //if the player is not just visiting the jail
if (!visiting)
{ //if the current player is not throwing the 3d time still he is in Jail
if (game.players[currentPlayer].throws==2)
{
if (game.isSim)
cout<<" !!! 3 Throws !!!"<<endl<<" The Player gets out from JAIL"<<endl;
visiting=true;
game.players[currentPlayer].throws=0;
game.players[currentPlayer].inJail=false;
}
else
//if the player has doubles
if (dice1==dice2){
if (game.isSim)
cout<<" !!! Doubled dices !!!"<<endl<<" The Player gets out from JAIL"<<endl;
game.players[currentPlayer].throws=0;
doubles=false;
}//else he remains in jail
else
{
if (game.isSim)
cout<<" !!! NON Doubled dices !!!"<<endl<<" The Player is still IN JAIL"<<endl;
game.players[currentPlayer].throws++;
moved=false;
}
}
else //if the player is visiting, then he will get out from the jail
game.players[currentPlayer].inJail=false;
}
//If the player is visiting the jail, is not in jail or is getting out from the jail grace to the 3d throw
if (visiting)
{ //if the player has doubles
if (dice1==dice2)
{
if (game.isSim)
cout<<" !!! Doubled dices !!!"<<endl;
//if the player just has got 3 consecutive doubles
if (game.players[currentPlayer].throws==2){
passed=true;
game.players[currentPlayer].location=game.board.jailSquare;
doubles=false;
}
else// if is the
{
game.players[currentPlayer].throws++;
doubles=true;
}
}
else //if the player did not got any doubles, but he got double the turn before
if (doubles)
{
doubles=false;
game.players[currentPlayer].throws=0; // for the case of any previous doubles
}
}
//if current player throwed the dices, do not remains in jail and has not the 3d double
// processing the players move
if (moved)
{ //if the player has not moved in the jail
// changing the players location
if (!passed)
{
game.players[currentPlayer].location+=dice1+dice2;
game.players[currentPlayer].location%=game.board.numSquares;
}
freq[game.players[currentPlayer].location]++;
if (game.isSim)
cout<<" The Player "<<currentPlayer+1<<" goes to"<<game.board.squares[game.players[currentPlayer].location].name<<endl;
//verifying if the current square is the chance or the chest one
// will continue while no more cards are cards must be drawed
while ((game.board.squares[game.players[currentPlayer].location].kind==S_CHEST
|| game.board.squares[game.players[currentPlayer].location].kind==S_CHANCE)
&& moved)
{
//watching if the player is on the Chest square or on the chance one
// and drawing a card from the deck
if (game.board.squares[game.players[currentPlayer].location].kind==S_CHEST)
{
card=game.chest.cards[game.chest.curCard];
game.chest.curCard=(game.chest.curCard+1)%game.chest.numCards;
}
else
{
card=game.chance.cards[game.chance.curCard];
game.chance.curCard=(game.chance.curCard+1)%game.chance.numCards;
}
if (game.isSim)
cout<<" The Player "<<currentPlayer+1<<" has drawed the \""<<card.name<<"\" card"<<endl;
//verifying for an absolute card
if (card.kind==C_ABS)
{
game.players[currentPlayer].location=card.value;
passed=true; // the absolute movement, if on Jail square then with "in jail" status
if (game.isSim)
cout<<" The Player "<<currentPlayer+1<<" goes to "<<game.board.squares[game.players[currentPlayer].location].name<<endl;
freq[game.players[currentPlayer].location]++;
}
else
//veryfing for a relational card
if (card.kind==C_REL)
{
game.players[currentPlayer].location+=card.value;
if (game.players[currentPlayer].location<0)
game.players[currentPlayer].location+=game.board.numSquares;
else
game.players[currentPlayer].location%=game.board.numSquares;
if (game.isSim)
cout<<" The Player "<<currentPlayer+1<<" goes to "<<game.board.squares[game.players[currentPlayer].location].name<<endl;
freq[game.players[currentPlayer].location]++;
}
else//for no-movement kind of cards: C_NOPE, C_UTIL and C_RR
moved=false; //will exit from the 'while'
}
//if curent square is TOJAIL
if (game.board.squares[game.players[currentPlayer].location].kind==S_TOJAIL)
{
game.players[currentPlayer].location=game.board.jailSquare;
freq[game.players[currentPlayer].location]++;
if (game.isSim)
cout<<" The Player "<<currentPlayer+1<<" goes to"<<game.board.squares[game.players[currentPlayer].location].name<<endl;
passed=true;
}
//if current square is JAIL
if (game.players[currentPlayer].location==(unsigned int)game.board.jailSquare)
{
game.players[currentPlayer].throws=0;
game.players[currentPlayer].inJail=true;
visiting=!passed;
}
}
//if current player has not any doubles, then the player change
if (!doubles)
currentPlayer=(currentPlayer+1)%game.numPlayers;
//if the new player sis the first one, then changind the round
if (currentPlayer==0 && !doubles)
{
index++;
if (game.isSim && index<game.rounds)
cout<<endl<<"--- R O U N D "<<index+1<<" ---"<<endl;
}
}
//outputting the Frequency Table
cout<<endl<<"Frequency Table"<<endl<<"-----"<<endl;
for (index=0; index<game.board.numSquares; index++)
{
cout.setf(ios::right);
cout.width(8);
cout<<freq[index];
cout.setf(ios::left);
cout<<game.board.squares[index].name<<endl;
}
}
it compiles fine, but when i test it, it gives floating exception errors. can anyone please tell me how to fix it?
[/code]

