Help with simple C program please

Help with simple C program please

Post by dakes112 on Tue May 22, 2012 2:42 am
([msg=66515]see Help with simple C program please[/msg])

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;
else
if (sum == 7)
Status = 2;
}
if (Status == 1)
printf ("Player wins\n");
else
printf ("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
New User
 
Posts: 2
Joined: Tue May 22, 2012 2:31 am
Blog: View Blog (0)


Re: Help with simple C program please

Post by ghost107 on Tue May 22, 2012 11:46 am
([msg=66519]see Re: Help with simple C program please[/msg])

rollDice ( ); does not return anything.
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Help with simple C program please

Post by LoGiCaL__ on Tue May 22, 2012 12:45 pm
([msg=66523]see Re: Help with simple C program please[/msg])

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.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Help with simple C program please

Post by dakes112 on Wed May 23, 2012 2:42 am
([msg=66541]see Re: Help with simple C program please[/msg])

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
New User
 
Posts: 2
Joined: Tue May 22, 2012 2:31 am
Blog: View Blog (0)


Re: Help with simple C program please

Post by ghost107 on Wed May 23, 2012 5:45 am
([msg=66546]see Re: Help with simple C program please[/msg])

in your code here:
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.

So in your code:
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 anything
void 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
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests