Help with simple C program please

Help with simple C program please

I have this program that I have to write for one of my classes. The question is:

A player rolls two dice. Each die has six
faces. These faces contain 1, 2, 3, 4, 5,
and 6 spots. After the dice have come to
rest, the sum of the spots on the two
upward faces is calculated.
 If the sum is 7 or 11 on the first throw, the
player wins.
 If the sum is 2, 3, or 12 on the first throw,
the player loses.
 If the sum is 4, 5, 6, 8, 9, or 10 on the
first throw, then sum becomes the
player’s point. To win, you must continue
rolling the dice until you make your
points. The player loses by rolling a 7
before making the point.

This is my program:

Code: Select all
`#include <stdio.h>#include <stdlib.h>#include <time.h>int rollDice (void);main ( ){int Status, sum, Point;srand (time(0));sum = rollDice ( );switch (sum) {case 7: case 11:Status = 1;break;case 2: case 3: case 12:Status = 2;break;default:Status = 0;Point = sum;printf("Point is %d\n", Point);break;}while (Status == 0) {sum = rollDice ( );if (sum == Point)Status = 1;elseif (sum == 7) Status = 2;}if (Status == 1)printf ("Player wins\n");elseprintf ("Player loses\n");}int rollDice (void){int die1, die2, workSum;die1 = 1 + (rand ( ) % 6);die2 = 1 + (rand ( ) % 6);workSum = die1 + die2;printf ("Player rolled %d + %d = %d\n", die1, die2, workSum);}`

I am really close to completing the program but my problem is that the player will always win and the program will not end at the correct spot. Any help that you can give me would be much appreciated. This is my first time on this forum so please excuse me if I have done something wrong.
Thanks
dakes112
New User

Posts: 2
Joined: Tue May 22, 2012 2:31 am
Blog: View Blog (0)

Re: Help with simple C program please

rollDice ( ); does not return anything.

ghost107
Poster

Posts: 316
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)

Re: Help with simple C program please

Along with what ghost said. It looks like you are doing to much work. I would try to get that switch case inside the while loop and preset status = 0 before the while loop. This way you do everything in one loop and just test for the outcome. If it is 0 then it re-iterates.

LoGiCaL__

Posts: 1080
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)

Re: Help with simple C program please

Thanks for your help so far.
So ghost what should I have instead of rollDice( ). I have trouble understanding my teacher and as far as I could tell this is what he indicated I should do, but if you know of another way I would much appreciate it.
Thanks again
dakes112
New User

Posts: 2
Joined: Tue May 22, 2012 2:31 am
Blog: View Blog (0)

Re: Help with simple C program please

Code: Select all
`sum = rollDice ( );`

sum is expecting to get a value from rollDice (), but in your code the function rollDice () does not return anything.

Code: Select all
`int rollDice (void){    int die1, die2, workSum;     //creates the 3 variables    die1 = 1 + (rand ( ) % 6);     //generates the value of dice 1    die2 = 1 + (rand ( ) % 6);     //generates the value of dice 2    workSum = die1 + die2;       //calculates the sum    printf ("Player rolled %d + %d = %d\n", die1, die2, workSum);//prints a message    //but this function should return a int(the return is missing)}`

Basic C functions:
Code: Select all
`return_type function_name(function_parameters){    //do stuff    return something;}//void means this fnction does not return anythingvoid myFunction(){    //do stuff but this function does not return anything}//this means the function will return something of the type type(int, float, char,etc)type  myfunction(){     type something;   //do stuff, but this function should return a type(int, float, char, etc), if not it will work like a void function   return something;}`

ghost107
Poster

Posts: 316
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)