Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,616 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 992 people online right now. Registration is fast and FREE... Join Now!




Dice Game

 
Reply to this topicStart new topic

Dice Game

duygugvn
post 28 Mar, 2006 - 12:38 PM
Post #1


New D.I.C Head

*
Joined: 31 Oct, 2005
Posts: 36


My Contributions


CODE

#include<stdio.h>
#include<stdlib.h>

int dice(void);
int rolldices(int count);

int dice(void)
{
    int a;
    srand(10);
    a=rand()%10+1;
    return a;
}

int rolldices(int count)
{
    int x=0,i,a;
    {
 for(i=1;i<=a;i++)
     x=dice();
    }

 return x;
}

int main(void)
{
    int a,x,continuo;
    do
    {
 printf("how many dice(-1 to exit)");
 scanf("%d",&a);
 x=rolldices(a);
 printf("the sum of dices %d",x);
 printf("-1 to exit\n");
 scanf("%d",&continuo);
    }
    while(continuo!=-1);
    return 0;
}

here it is my code????I coud not find the output of the functionn.I try to do throw dices which is determined by the user .and rand function generate number randomly and according to genrated number I get the sum of dice numbers and my program have to finish wheen the user enter -1!!!
I try to do that but it could not work.where am I wrong??? sleepy.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Mar, 2006 - 12:44 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


In the function rolldices:
CODE

for(i=1;i<=a;i++)

a is your terminating condition, but it has not been given a value in the function. there are also two extra braces in that function. Does the code compile correctly for you?
User is offlineProfile CardPM

Go to the top of the page

duygugvn
post 28 Mar, 2006 - 12:57 PM
Post #3


New D.I.C Head

*
Joined: 31 Oct, 2005
Posts: 36


My Contributions


QUOTE(Amadeus @ 28 Mar, 2006 - 12:36 PM)
In the function rolldices:
CODE

for(i=1;i<=a;i++)

a is your terminating condition, but it has not been given a value in the function. there are also two extra braces in that function. Does the code compile correctly for you?

my compiler give me 0 error and 0 warning.but it does not wotkkk
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 28 Mar, 2006 - 01:29 PM
Post #4


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


variable a is never initalized. PLEASE MAKE SURE YOU READ YOUR CODE A FEW TIMES BEFORE POSTING WITH SUCH PROBLEMS.

This is dangerous, always initialize your variables to 0!

Also in your case i think you mean

#include<stdio.h>
#include<stdlib.h>

int dice(void);
int rolldices(int count);

int dice(void)
{
int a;
srand(10);
a=rand()%10+1;
return a;
}

int rolldices(int count)
{
int x=0,i,a;
{
for(i=1;i<=count;i++)
x=dice();
}

return x;
}

int main(void)
{
int a,x,continuo;
do
{
printf("how many dice(-1 to exit)");
scanf("%d",&a);
x=rolldices(a);
printf("the sum of dices %d",x);
printf("-1 to exit\n");
scanf("%d",&continuo);
}
while(continuo!=-1);
return 0;
}



Also the function rolldices is quite pointless.. it doesnt matter if you roll it 1x or 1000x you still get 1 random number. Try using srand only once in the begining of your program.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month