By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

call a function

P: 18
Hi everyone!

i'm stuck! i'm trying to run the program i've build bu calling a func but i dont know how to go from there!

my code is !!

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<stdbool.h>

int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling ); // prototype
keep_rolling = 'y';

int main()
{
int d1, d2, d3, money, r;
char keep_rolling = 'n';
money = 100;
r = rolling(d1, d2, d3, money,keep_rolling);
srand((int)time(NULL));
printf("Všlkommen till Spelet! \n \n");
//spelregler
printf("1.it cost you 10kr to play \n");
printf("2.if all the dicies are sixes you win 100kr \n");
printf("3. if all the dicies are alike except number (6) you win 50kr \n");
printf("4. if you get at least two alike you 1 \n");
printf("5. otherwise you win nothing\n");

printf("you have %d kr, if you wnat to play press (n) \n\n", money);
fflush(stdin);
keep_rolling = ((getchar()));
d1 = (rand() % 6 + 1);
d2 = (rand() % 6 + 1);
d3 = (rand() % 6 + 1);


}

int rolling(int dice1, int dice2, int dice3, int moneyinput, char keep_rolling) // def my function
{
keep_rolling = 'y';
do {
dice1 = (rand() % 6 + 1);
dice2 = (rand() % 6 + 1);// from 1 to 6
dice3 = (rand() % 6 + 1);

if (moneyinput < 10)
{
printf("you do not have enough money \n");
break; // exit the program
}
moneyinput -= 10;
if (dice1 == 6 && dice2 == 6 & dice3 == 6)
{
printf("you have won 100\n ");
moneyinput += 90;
}
else if (dice1 == dice2 == dice3)
{
printf("you have won 50kr \n");
moneyinput += 40;
}
else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3)
{
printf("you have won 10kr \n");
moneyinput += 10;
}
else
{
printf("Sorry! you have lost. god luck next time \n");
}


} while (keep_rolling == 'y');
system("pause");
}
Nov 5 '15 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 2,398
It is easier for us to comment on your code if you enclose it in CODE tags (the [CODE/] button).

You don't say what is wrong; but I noticed the following issues:
  • Immediately below the prototype for roll(), you assign 'y' to variable keep_rolling. No type is specified for this variable. This particular keep_rolling variable is not used elsewhere.
  • Immediately upon entry to rolling(), you override the passed value of keep_rolling by setting it to 'y'.
  • At the bottom of rolling() you loop back if keep_rolling is 'y'. It is always equal to that, so you have an infinite loop.
  • You define rolling() as returning an int, but it has no return statement.
  • Why are dice1, dice2, and dice3 passed as parameters? These look more like local variables within rolling().
  • main should call srand() before rolling().
Nov 5 '15 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
There are several errors in this code.

The big one is in your rolling() function, In main() a variable r is assigned the return value from rolling()except there is no return statement in rolling().

rolling() is doing more then rolling dice. There should never be displays to the screen popping out from various places in the code. That makes control of the screen layout impossible. All the displays need to be in main() or you write a display() function that handles all the displays.

rolling should make one roll and return the value. The loop inside rolling() is really part of your main().

This code:
Expand|Select|Wrap|Line Numbers
  1. if (dice1 == 6 && dice2 == 6 & dice3 == 6)
  2.  {
probably is supposed to be:
Expand|Select|Wrap|Line Numbers
  1. if (dice1 == 6 && dice2 == 6 && dice3 == 6)
  2.  {
There is char keep_rolling outside main()and there is char keep_rolling inside main(). Not good. Use the one inside
main() and remove the one outside main().

Once you have the compile errors fixed, then you can step through the code using your debugger. If you don't know how to use it, then this is a great opportunity to learn it. Avoid putting in various display statements to see what's going on. You want to test the code that's going to run and not the code that needs to have a bunch of displays deleted because you could screw up the program in removing the displays.

Post again and let me know what's happening.
Nov 5 '15 #3

P: 18
thank u and sorry for being unclear! i'm really freshmen and really new to c programming ! my problem is i dont really know how to call the function! and the assignment is to run it by calling a function!

sorry for my English!

my orginal code is

Expand|Select|Wrap|Line Numbers
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int main()
  7. {
  8.     int dice1, dice2, dice3, moneyinput;
  9.     char keep_rolling = 'y';
  10.     moneyinput = 100;
  11.     srand((int)time(NULL));
  12.     printf("Všlkommen till Spelet! \n  \n");
  13.     //gameroll
  14.     printf("1.it cost you 10kr to play \n");
  15.     printf("2.if all the dicies are sixes you win 100kr \n");
  16.     printf("3. if all the dicies are alike except number (6) you win 50kr \n");
  17.     printf("4. if you get at least two alike you 1 \n");
  18.     printf("5. otherwise you win nothing\n");
  19.     do
  20.     {
  21.         // dice function 
  22.         dice1 = (rand() % 6 + 1);
  23.         dice2 = (rand() % 6 + 1);// from 1 to 6
  24.         dice3 = (rand() % 6 + 1);
  25.         printf("you have %d kr, if you wnat to play again press (y) \n\n", moneyinput);
  26.         fflush(stdin);
  27.         keep_rolling = ((getchar()));
  28.         if (moneyinput < 10)
  29.         {
  30.             printf("you do not have enough money \n");
  31.             break; // exit the program
  32.         }
  33.         moneyinput -= 10;
  34.         if (dice1 == 6 && dice2 == 6 & dice3 == 6)
  35.         {
  36.             printf("you have won 100\n ");
  37.             moneyinput+=90; 
  38.         }
  39.         else if (dice1 == dice2 == dice3)
  40.         {
  41.             printf("you have won 50kr \n");
  42.             moneyinput+=40;
  43.         }
  44.         else if (dice1 == dice2 || dice1 == dice3 || dice2 == dice3)
  45.         {
  46.             printf("you have won 10kr \n");
  47.             moneyinput+= 10;
  48.         }
  49.         else
  50.         {
  51.             printf("Sorry god luck next time \n");
  52.         }
  53.  
  54.  
  55.     }
  56.     while (keep_rolling == 'y');
  57.     system("pause");
  58. }
Nov 5 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.