## Xcoding (1st attempt):Can someone please tell me why this isn't working?

340 Views 9 Replies Latest reply: Mar 31, 2012 8:43 AM by Jongware
Calculating status...
Currently Being Moderated
Mar 30, 2012 12:53 AM

Hi Folks,

Please forgive me if this is the wrong forum/area,  I am new here/first post.

I recently have been trying out XCODE and learning Objective-C.  Coming from a background

of Microsoft Basic 2.0, it's been a challenge to say the least!

Here's what I wrote so far,  it's a basic 'wager/dice' game.  I basically want the game

to keep repeating until the player runs out of money (I know I still have to program

that into the 'while' function, so I defaulted it for 10 times in the interim).

Here's what I need to figure out:

- The "While" loop is working, yet the 'cash' constantly resets to 1000 each time it starts again. I tried

{bracketing} the functions, but whether I win/lose the dice rolls, it always resets to 1000

There's a bunch more things I need to figure out, but if someone can tell me why the 'cash'

keeps reseting to '1000' each time,  I'd greatly appreciate it!

- CF

/* dice wager game */

#include <stdio.h>

#include <Foundation/Foundation.h>

int main(int argc, const char * argv[])

{

int r = arc4random() % 6+1;

int bet;

int dice;

int cash=1000;

int total;

int loop=1;

while (loop<10){

printf("\n");

printf ("You have %i dollars, How much do you want to bet? \n", cash);

scanf ("%d",&bet);

printf ("What is the number you want to bet on (1 out of 6)? \n");

scanf ("%d",&dice);

if ( dice == r )

{

printf("You guessed it right %i \n",r);

total=bet+cash;

printf("You now have %i \n",total);

}

else if (r!=dice)

{

printf("Your number %i was, the number is %i \n", dice,r);

total=bet-cash;

printf("You now have %i \n",total);

if (total==0)

{

printf("You have no more money, game over \n");

break;

}

}

loop++;

}

}

#### More Like This

• Retrieving data ...

#### Legend

• This solved my question - 10 points
• This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.