Monday, May 24, 2010

Who can help me figure out?below is the question and my C++ code,but it cannot be compiled.
me figure out?

Implement the Stock Ticker structures:


* a gameState structure, that contains the number of times remaining to roll the dice, plus current values for all stocks;


* a Player structure, that records the player's stock holdings and the amount of cash that player has.


In your main program, create and initialize a gameState variable (5 dice to be rolled, all stocks at 1.00) and a 6-member array of Players (no stocks held, $5000 in cash for each player).





#include%26lt;iostream%26gt;


using namespace std;





int main()


{


struct GameState


{


int rollsRemainingInTurn;


float currentStockValues[6];


}


struct playerState


{


int stickHoldings[6];


float cash;


}


int i;


int j;


int k;


playerState player[6];


for(i=0;i%26lt;6;i++)


{


players[i].cash=5000.00;


}





for(j=0;j%26lt;6;j++)


{


players[i].stockHoldings[j]=0;


}


GameState game;


game.rollsRemainingInTurn=5;


for(k=0;k%26lt;6;k++)


{


game.currentStockValues[k]=1.00;


}

Who can help me figure out?below is the question and my C++ code,but it cannot be compiled.


me figure out?
Firstly, you shouldnt have your structures inside of your main() function. Pull them out so they are 'along side' it.





Next, I am pretty sure you're going to need semicolons ( ; ) after your structs ( }; )





Your loops for j is wrong. Your j loop needs to be inside of your i loop, just after you set the cash. That is, for each of your players you set its cash and you set each stock holding in each player.





Why do you have a ... after setting the currentStockValues ? That will not compile, you need a semicolon





You dont have an end brace } after your main function, nor do you have a return statement at the end.





It needs a lot of work!!!! :)





Do yourself a favor, dont try to put so much in the code at once. Start out with just getting an empty main() function to compile. Then put the standard "Hello World!" output in there.. Then add your structures, make sure they compile... add your initialization loops one-by-one. Keep testing your code as you go, it'll make things MUCH MUCH EASIER!





Edit- DarkTrooper's code below probably does compile now, but compiling and being correct are different things. :) His does not implement the loop correctly!
Reply:Hello,





Here's your code, now compilable with the appropriate fixes. I've commented those lines where you made errors.





int main()


{


struct GameState


{


int rollsRemainingInTurn;


float currentStockValues[6];


}; /* need semicolon here */





struct playerState


{


int stockHoldings[6]; /* renamed stickHoldings to stockHoldings */


float cash;


}; /* need semicolon here */





int i;


int j;


int k;


playerState players[6]; /* renamed variable player to players */





for(i=0;i%26lt;6;i++)


{


players[i].cash=5000.00;


}





for(j=0;j%26lt;6;j++)


{


players[i].stockHoldings[j]=0;


}


GameState game;


game.rollsRemainingInTurn=5;





for(k=0;k%26lt;6;k++)


{


game.currentStockValues[k]=1.00;


}





return 0; /* need to return an int value */


}


No comments:

Post a Comment