Mac Help needed with intro level C program!

SuperCompu2

macrumors 6502a
Original poster
Jul 23, 2006
852
1
MA
Hi guys, I need a bit of help with a C program I'm writing for a class. The program will compile fine (gcc), but I need it to output values and it simply will not have it. Here's the code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
enum Status { CONTINUE, WON, LOST };
int rollDice( void ); /* function prototype */ 
int playgame(void);

int rolls;


int main(void)
{
 /* Roll dice, calculate sum and display results */ 
    int rollDice( void ) 
      { 
        int die1; /* first die */ 
        int die2; /* second die */
        int workSum; /* sum of dice */ 
        die1 = 1 + ( rand() % 6 ); /* pick random die1 value */ 
        die2 = 1 + ( rand() % 6 ); /* pick random die2 value */
        workSum = die1 + die2; /* sum die1 and die2 */ 
   
        /* display results of this roll */
          
      return workSum; /* return sum of dice */ 
      } /* end function rollRice */ 


//end Main

int playgame(void)
    {
      int sum; /* sum of rolled dice */ 
      int myPoint; /* point earned */ 
      enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */ 

      /* randomize random number generator using current time */ 
      srand( time( NULL ) ); 
      sum = rollDice(); /* first roll of the dice */ 
      /* determine game status based on sum of dice */ 

	  switch( sum ) 
	    { 
	    /* win on first roll */ 
	      case 7: case 11: gameStatus = WON; 
	      break; 

	    /* lose on first roll */ 
	      case 2: case 3: case 12: gameStatus = LOST; 
	      break; 

	    /* remember point */ 
	      default: gameStatus = CONTINUE; 
	      myPoint = sum; 
	      break; /* optional */ 
	    } /* end switch */

      /* while game not complete */ 
      while ( gameStatus == CONTINUE ) 
       { 
	  sum = rollDice(); /* roll dice again */ 
	  /* determine game status */ 
	      if ( sum == myPoint ) 
	        { 
	         /* win by making point */ 
	         gameStatus = WON; /* game over, player won */ 
	        } /* end if */ 
	      else 
	        { 
	          if ( sum == 7 )
		    { 
		      /* lose by rolling 7 */
		      gameStatus = LOST; /* game over, player lost */ 
		    } /* end if */ 
	        } /* end else */ 
       } /* end while */ 


    if((gameStatus = WON)) {
	rolls = sum; 
	printf( "%d", sum);
 }
    else if((gameStatus = LOST)) {
 	rolls = (sum * (-1));
	printf("%d", sum);
	}
   
   

    return sum; /* indicates successful termination */ 
  } /* end playgame */ 


return 0;
} //end Main
I know it's a little long, but if someone notices something blaringly obvious please let me know!

Thanks!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
I don't know what this code is supposed to do yet, but I can tell you right off the the bat, this shouldn't compile without some special options. main needs to invoke playgame, and rollDice should not be nested inside of main. Beyond that it will take a few more minutes to try to decipher what this should do, to see if it's doing it with the changes i already mentioned.

-Lee

EDIT: Still working, but = does not test equivalence, it performs assignment. Check your conditionals to make sure you are using ==.

EDIT 2: Not sure what exactly you're needing to print. I threw a print statement just inside the while displaying the value of sum at the time, so the value of all rolls is displayed. Inside the conditionals where you're checking (not quite yet, see the first edit) if the player has won or lost, i changed these to state if the player won or lost. Right now you're not doing anything with rolls after assigning a value to it. A variable named that, i would think, would be used to count up how many rolls are made, but again, i don't know what exactly you're going for here.

EDIT 3: Not sure why rolls is global. I'd just make it local to playgame and initialize it to 0, and increment it each time you call rollDice. If you'd like, you could pass it by reference and increment it inside rollDice since rollDice is called a couple of different places, or you could just increment just before or after you call rollDice, but be sure to do it anywhere a roll happens.
 

SuperCompu2

macrumors 6502a
Original poster
Jul 23, 2006
852
1
MA
First off, thanks for looking Lee!

Secondly, this program is supposed to be playing the game of craps, and prior assignments have had the game doing various other things.

What I'm trying to do here is write a new portion that tells me how many rolls it took to either win or lose the game. If it's a win, the number of rolls returns positive, if it's a loss, then the rolls return negative.

That's just the beginning of the program, but to move on I need this to operate correctly!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
OK. How are you keeping track of rolls currently? In the copy of the code you posted (which i still don't believe compiles, btw, it didn't w/ gcc for me) rolls appears three times:
Code:
int rolls;
...
        rolls = sum;
...
        rolls = (sum * (-1));
None of these have anything to do with the number of rolls. You do set rolls negative or positive based on winning or losing, but you set it to the signed value of the last roll that was made, not anything to do with the number of rolls.

So you need to actually track rolls. I made a few suggestions on how to do this in one of the edits in my post above. I'm not sure why you need to place the win/loss status and the number of rolls in a single variable, but it should be pretty trivial to count rolls, then at the end multiply by -1 in the case of a loss.

-Lee
 

SuperCompu2

macrumors 6502a
Original poster
Jul 23, 2006
852
1
MA
OK. How are you keeping track of rolls currently? In the copy of the code you posted (which i still don't believe compiles, btw, it didn't w/ gcc for me) rolls appears three times:
Code:
int rolls;
...
        rolls = sum;
...
        rolls = (sum * (-1));
None of these have anything to do with the number of rolls. You do set rolls negative or positive based on winning or losing, but you set it to the signed value of the last roll that was made, not anything to do with the number of rolls.

So you need to actually track rolls. I made a few suggestions on how to do this in one of the edits in my post above. I'm not sure why you need to place the win/loss status and the number of rolls in a single variable, but it should be pretty trivial to count rolls, then at the end multiply by -1 in the case of a loss.

-Lee
Ok, great. And yes, that is what I was originally trying to do (track the number of rolls, I'm not too code-savvy just yet), so my methodology is obviously flawed.

You're saying I should look to track the rolls from within the While loop instead of outside the function like I had done before? I'll try to give this a shot and see how it goes.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
I hate global variables, but certainly having a global makes it easier to just increment in rollDice, be able to access it in playgame, etc. The way i just did it was to declare rolls in playgame, and pass it by reference to rollDice where it is incremented, then *= -1 if the player loses. If you haven't learned how to pass variables by reference, then simply incrementing in playgame is probably the best way to go. Again, using a global variable will get the job done, but it's a bad habit.

-Lee
 

lloyddean

macrumors 65816
May 10, 2009
1,002
16
Des Moines, WA
Your code rearranged, minor corrections and a few additions -

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

enum Status { CONTINUE, WON, LOST };


/* Roll die returning results */
int rollDie(void)
{
    return 1 + (rand() % 6);
}

/* Roll dice, calculate sum and display results */
int rollDice(int* rolls)
{
    int workSum = 0;
    
    int die1    = rollDie(); /* first die */
    int die2    = rollDie(); /* second die */
    
    workSum = die1 + die2; /* sum die1 and die2 */
    
    *rolls = ((*rolls) + 1);
    
    /* display results of this roll */
//  printf("The sum of die1 + die2 is %d + %d = %d\n", die1, die2, workSum);
    printf("The cast is %d\n", workSum);
    
    return workSum; /* return sum of dice */
} /* end function rollRice */

int playgame(void)
{
    int rolls = 0;
    
    int cast; /* sum of rolled dice */
    int myPoint; /* point earned */
    enum Status gameStatus; /* can contain CONTINUE, WON, or LOST */
    
    cast = rollDice(&rolls); /* first roll of the dice */
    /* determine game status based on sum of dice */
    
    gameStatus = CONTINUE;
    switch ( cast )
    {
        /* win on first roll */
        case  7:
        case 11:
            gameStatus = WON;
            break;
            
        /* lose on first roll */
        case  2:
        case  3:
        case 12:
            gameStatus = LOST;
            break;
            
        /* remember point */
        default:
            myPoint = cast;
            printf("Point is %d\n", myPoint);
            break; /* optional */
    } /* end switch */
    
    /* while game not complete */
    while ( gameStatus == CONTINUE )
    {
        cast = rollDice(&rolls); /* roll dice again */
        
        /* determine game status */
        if ( myPoint == cast )
        {
            /* win by making point */
            gameStatus = WON; /* game over, player won */
            break;
        } /* end if */
        else if ( 7 == cast )
        {
            /* lose by rolling 7 */
            gameStatus = LOST; /* game over, player lost */
            break;
        } /* end else if */
    } /* end while */
    
    
    if ((gameStatus == WON))
    {
        printf("We have a winner with a cast of %d on roll %d!\n", cast, rolls);
    }
    else if ((gameStatus == LOST))
    {
        printf("We have a loser with a cast of %d on roll %d!\n", cast, rolls);
    }
    
    return rolls; /* indicates successful termination */
} /* end playgame */

int main(void)
{
    int rolls = 0;
    
    /* randomize random number generator using current time */
    srand( time( NULL ) );
    
    rolls = playgame();
    
    return 0;
} //end Main
 

lloyddean

macrumors 65816
May 10, 2009
1,002
16
Des Moines, WA
Lloyddean:
this is for a class. Don't write/fix code.

-Lee
Lee, I understand and agree with you. But in this particular case looking at his posting history, as well as his level of confusion, I thought it best to give a working solution as an encouragement to return. Not to do his homework for him, but to let him know he was doing something right and that it didn't require much more work to get it working.

Some people learn quicker from working examples than by bashing their head against a problem. If he comes back great, he'll find that the help is both useful and available.

There is also the fact that a lot of these threads could be made more useful for future searchers with the posting of completed and working code from which to learn.

School presumably starts back up shortly and understanding early is more important (in my opinion) then not understanding enough to continue.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.