473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

call a function

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
3 1035
donbock
2,426 Expert 2GB
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
9,208 Expert Mod 8TB
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
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

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

Similar topics

5
by: Lucy Randles | last post by:
I've written a VBA function that I then need to call in a form. Previously to do this I have written the call procedure in the same module as the function - i.e. Function callfunctionname() Call...
2
by: PawelR | last post by:
Hello Group, In my application I have few class, and I want call function with "master class". This is as master form (startClass) and option window (ClassA). My question is how call function...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
2
by: moondaddy | last post by:
I'm using vb.net and have an aspx page where I want to call a function in the code behind to do something on the backend and I want to call this function from a jscript function in the aspx page. ...
7
by: Tiraman | last post by:
Hi , I have 3 files , middle.aspx file include the header.aspx and footer.aspx files . in each of the include files there is a function and from some reason the call to the Footer() function...
12
by: leaf | last post by:
Hi, How to call function at runtime, based on a struct that contains the information for the function call: struct func_to_call { int function_id; // function id to call unsigned int nparams;...
5
by: yaru22 | last post by:
Hi. I'm wondering if there is a way to call function that a variable refers to. I was writing a script to load xml file. And what I've written so far looks like this: var xmlDoc; ...
1
by: Khai Doan | last post by:
I have function A, which need to call function B with the exact same argument list. What is the correct way to do this? I had function A: function A { B(arguments); } but it does not...
9
by: Andrew Poulos | last post by:
Say I have this: var foo = function(x, y) { return x + y; }; var bar = function(fname, param1, param2) { /* * how do I call the function "foo" * and pass parameters to it?
1
by: intermanch | last post by:
how call function php($varible) into javascript withod ajax: <!-- this code only call funnctions php but how call function php intermanch@gmail.com www.intermanch.blogfa.com
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.