I got this code (written when sleepy, sorry for lack of "expertness")

#include <stdio.h>
#include<time.h>
int card1; int card2; int card3;
int total;
int other;
srand(time(NULL));
card1=(rand()%22)+1;
card2=(rand()%22)+1;
other=(rand()%22)+1;
total=card1+card2;
int main()
{

 printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");

 printf("Your cards are: %d and %d\n", card1, card2);
 printf("total is: %d\n", total);
 
 if(total<21)
 { turn(); }
 
 else if (total>21);
 { printf("BUST! play again?\n"); }
return 0;
}
#include<stdio.h>
void turn();
void turn()
{ char ans;
 printf("Twist or bust (t/b)\n");
 ans = getchar();
 switch(ans) 
 { case 't' : draw(); break;
  case 'b' : neg(); break;
 }
}
#include <stdio.h>
void neg();
void neg()
{
 if(total > other)
 { printf("you won! play again?\n"); } 
 else if (other > total)
 {printf("damn nice try. play again?\n"); }
}
#include<stdio.h>
void draw();
void draw()
{
 srand(time(NULL));
card3=(rand()%22)+1;
 total+=card3;
}

it's a card game (twist or bust). so two random numbers are drawn, if they're over 21, you lose, under and you can take another card. if the three cards are over 21 you lose, if not anohter card.. so the total is always changing, how can i pass the value of "total" between the functions? i know there's a way in java placing the variable name in the () of the method, (well i think. it didn't work but i'm new at all of this). any pointers would be appriciated, but like i said, sleepy when i wrote it :confused:

Use parameters to pass data between functions.

For example:

void myFunction(int amount);

// etc.

    myFunction(aNumberUserEntered);
}

void myFunction(int amount) {

    // 'amount' contains whatever was passed inside the ()
    // blah blah
}

A few problems with your code:

  • When splitting up your source files, create a .h that contains ONLY prototypes -- no actual function definitions, those go in the .cpp files
  • Try to avoid the use of globals in your program, like you did before main(). Whenever possible, pass needed data between functions. It's much easier to track down errors
  • You're going to have potential problems with getchar because you aren't removing the newline left behind

thanks, i moved the variables, and put everything into one files so it looks like this:

#include <stdio.h>
#include<time.h>


int main()
{

int card1; int card2; int card3;
int total;
int other;

srand(time(NULL));
card1=(rand()%22)+1;
card2=(rand()%22)+1;
other=(rand()%22)+1;

total=card1+card2;


	printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");


	printf("Your cards are: %d and %d\n", card1, card2);

	printf("total is: %d\n", total);
	
	if(total<21)
	{ turn(total, other); }
	
	else if (total>21);
	{ printf("BUST! play again?\n"); }

return 0;
}

void turn(int total, int other);

void turn(int total, int other)
{	char ans;
	printf("Twist or bust (t/b)\n");
	ans = getchar();
while (getchar() != '\n');



	switch(ans)	
	{	case 't' : draw(total); break;
		case 'b' : neg(total, other); break;
	}

}



void draw(int total);

void draw(int total)
{
	srand(time(NULL));
int card3=(rand()%22)+1;

	total+=card3;
total=(int) total;

/*will do switch for play again*/

}



void neg(int total, int other);

void neg(int total, int other)
{
	if(total > other)
	{ printf("you won! play again?\n"); } 

	else if (other > total)
	{printf("damn nice try. play again?\n"); }
/*going to do a switch block here linking back to main()*/
}

when i compile that i get these errors:

main2.c:36: error: conflicting types for 'turn'
main2.c:28: error: previous implicit declaration of 'turn' was here
main2.c:55: error:conflicting types for 'draw'
main2.c:47: error: previous implicit declaration of 'draw' was here
main2.c:71: error: conflicting types for 'neg'
main2.c:48: error: previous implicit declaration of 'neg' was here

what does that mean?

void turn(int total, int other);
void draw(int total);
void neg(int total, int other);

These prototypes should be before the main() function or in another head of file included, so the compiler can see what are they.
Also the int total and int other that you wrote inside each function are
different variables with the same name. When you pass those as a
parameter to a function you are creating a copy with the same value, if that value is the one being passed.

Just because it has the same name doesn't mean are the same variables.

I moved those three lines an it worked! except the first two numbers totaled 23 so it went straight out.. guess i should get to work on the switch statements eh? thanks for the help :)

Aia, a copy is being passed, that makes sense. so the total in main is total(copy1), that gets sent to turn but as total( copy2). So if i sent it back to the main (for the if>21 evaluation) would it be total(copy1) or total(copy2)thats evaluated?

#include <stdio.h>
#include<time.h>

void turn(int total, int other);
void draw(int total, int other);
void neg(int total, int other);
void eval(int total, int other);
int main()
{
int card1; int card2; int card3;
int total;
int other;
srand(time(NULL));
card1=(rand()%13)+1;
card2=(rand()%13)+1;
other=(rand()%13)+1;
total=card1+card2;

 printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");

 printf("Your cards are: %d and %d\n", card1, card2);
 printf("total is: %d\n", total);
 
 eval(total, other);

return 0;
}

void eval(int total, int other)
{ char again;
 if(total<21)
 { turn(total, other); }
 
 if (total>21);
 { printf("BUST! play again?\n"); }
 again = getchar();
while (getchar() != '\n');
 switch(again)
 { case 'y' : main(); break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
}

void turn(int total, int other)
{ char ans;
 printf("Twist or bust (t/b)\n");
 ans = getchar();
while (getchar() != '\n');
 
 switch(ans) 
 { case 't' : draw(total, other); break;
  case 'b' : neg(total, other); break;
 }
}
void draw(int total, int other)
{
 srand(time(NULL));
int card3=(rand()%13)+1;
printf("next card is: %d\n", card3);
 total+=card3; printf("new total: %d\n", total);
total=(int) total; eval(total, other);
}
 
 
void neg(int total, int other)
{ char win, lose;

 if(total > other)
 { printf("Opponents cards: %d\n", other);
  printf("you won! play again?\n");  
win = getchar();
while (getchar() != '\n');

switch(win)
 { case 'y' : main(); break;
  case 'n' : printf("ok, see ya!\n"); break;
 }}
 
 else if (other > total)
 {printf("Opponents cards: %d\n", other);
  printf("damn nice try. play again?\n"); 
 
lose = getchar();
while (getchar() != '\n');

switch(lose)
 { case 'y' : main(); break;
  case 'n' : printf("ok, see ya!\n"); break;
 }}
}

thats the new one. here's a game:

Hey, welcome to HBK's Twist or bust game. Lets begin
Your cards are: 3 and 5
total: 8
Twist or bust? (t\b):
t
next card is: 10
new total: 18
BUST! play again?
n
ok, see ya!
BUST! play again?
n
ok, see ya!
BUST! play again?


It got stuck in a loop :-\ . if i don't twist and say no. then it exits as it should. why does it jump back up to the eval function?

now it's getting interesting :-D


Aia, a copy is being passed, that makes sense. so the total in main is total(copy1), that gets sent to turn but as total( copy2). So if i sent it back to the main (for the if>21 evaluation) would it be total(copy1) or total(copy2)thats evaluated?

No my friend. The total being evaluated in main is not being changed
at all by the functions because you are not returning any value from
the function. So actually, nothing it is send back to main.

I am bad explaining these in words, so I'll give you this little example
and hopefully it would help to understand it.

#include <stdio.h>

/* function not returning a value */
void double_not(int num)
{
    num *= num;    /* integer is being doubled, but nothing is returned */
}

/* same function returning a value */
int double_it(int num) /* a integer is being passed */
{
    return num *= num; /* return that integer doubled */
}

int main(void)
{
    int number = 2;
    
    printf("What's in number?: %d\n", number);

    double_not(number);

    printf("What's in number after double_not function?: %d\n", number);
    
    number = double_it(number); /* number is being here modified */
    printf("What's in number after double_it function?: %d\n", number);
    
    getchar();
    return 0;
}

To avoid confution, I don't like to give the same variable names passed as parameters to functions

Simply said, in pass by value, a copy of the value of the variable is paseed to the function while the original variable remains unchanged.

void doubleValue (int i)
{
    i = i * 2;
    printf ("\ni inside function is %d", i);
}

int main (void)
{
    int i = 9;
    printf ("\ni before function call is %d", i);
    doubleValue (i);
    printf ("\ni after function call  is %d",i);    

    getchar ();
    return 0;
}

Run the above code and you would understand the function has its own copy of the variable "i". I used the same variables so that you would know they are not the same.

ok, that makes sense. so i need to use the return keyword to change the original value (up date it)

int draw(int total, int other)
{
 srand(time(NULL));
int card3=(rand()%13)+1;
printf("next card is: %d\n", card3);
 total+=card3; printf("new total: %d\n", total);
total=(int) total; return total; eval(total, other);
}

like that? will that mean the total in eval function (in my previous post) is updated? .. except apparently not because the total for one go is 15, which is less than 21 but it goes to BUST! play again?

Don't know what this line's supposed to do:

total=(int) total;

But whatever.

>will that mean the total in eval function (in my previous post) is updated?
Only if you assign the return value to a variable, eg.

// ...
    total = draw(blah, blah);
}

hbk619 your code is getting very confusing right now. And very hard
to read because is not following a proper format. Read these sugestions about formating.

Also use tags when you post your code. For example, for C you write
[[B]code=c[/B]]at the beginning, and end of your c code[[B]/code[/B]]
For C++ you write
[[B]code=cplusplus[/B]]at the start of your C++ code, and end of it [[B]/code[/B]]

Don't know what this line's supposed to do:

total=(int) total;

But whatever.

when i complied it before, the error came back as total was not an interger data type, or something similar, so i thought that would convert it. :-|

how come i can't go back and edit my previous posts?... i am logged in..

#include <stdio.h>
#include<time.h>
/*functions i'm going to use*/
void turn(int total, int other);
int draw(int total, int other);
void neg(int total, int other);
int eval(int total, int other);
 
int main()
{
/*variables for cards*/
 
int card1; int card2; int card3;
int total;
int other;
/*intializing them with the random function*/
srand(time(NULL));
card1=(rand()%13)+1;
card2=(rand()%13)+1;
other=(rand()%22)+1;
total=card1+card2;
 
 printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
 
 printf("Your cards are: %d and %d\n", card1, card2);
 printf("total is: %d\n", total);
 
/*go to eval function*/
 eval(total, other);
 
return 0;
}
 
int eval(int total, int other)
{ char again;
/*if total less than 21 go to turn function*/
 
 if(total<21)
 { turn(total, other); }
 
 else if (total>21);
 { printf("BUST! play again?\n"); }
 again = getchar();
while (getchar() != '\n');
/*(if they say yes, go back to main, if no exit*/
 switch(again)
 { case 'y' : main(); break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
}
 
void turn(int total, int other)
{ char ans;
 printf("Twist or bust (t/b)\n");
 ans = getchar();
while (getchar() != '\n');
/*if "twist" (draw another card) go to draw function, if "bust" go to neg function*/
 switch(ans) 
 { case 't' : draw(total, other); break;
  case 'b' : neg(total, other); break;
 }
}
 
int draw(int total, int other)
{
/*create variable card3 of a random number*/
 
 srand(time(NULL));
int card3=(rand()%13)+1;
printf("next card is: %d\n", card3);
 
/*total variable from above now equales total + card three*/
  total+=card3; printf("new total: %d\n", total);
/*this is where it goes back to the "eval" function with the new total*/
total = eval(total, other);
}
 
 
void neg(int total, int other)
{ char win, lose;
 
/*if total is bigger than the other in the main function then say you won etc*/
if(total > other)
 { printf("Opponents cards: %d\n", other);
  printf("you won! play again?\n");
 
win = getchar();
while (getchar() != '\n');
 
/*if the answer is yes, go back to main, if no finish*/
 
switch(win)
 { case 'y' : main(); break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
 }
 
/*if other in main is bigger than total say you lose etc*/
 
 else if (other > total)
 {printf("Opponents cards: %d\n", other);
  printf("damn nice try. play again?\n"); 
lose = getchar();
while (getchar() != '\n');
 
/*if the answer is yes, go back to main, if no finish*/
switch(lose)
 { case 'y' : main(); break;
  case 'n' : printf("ok, see ya!\n"); break;
 }}
}

right, that all works except same problem as before after asking play again? i enter n. "ok see ya!" is printed, the "BUST!" play again?" and i'm stuck in a loop. only way to exit is to close the command prompt. how do i stop that loop and when 'n' is entered just "ok see ya!" prints then end? would a goto statement work? (i know it's "bad programming".. welll thats what C programming in Easy Steps told me anyway :cheesy: )

Member Avatar for iamthwee

how do i stop that loop and when 'n' is entered just "ok see ya!" prints then end?

Push the power button thingy on your tower.

Please format your code so it's readable. I see this has already been suggested. Please do it...

switch(lose)
{ case 'y' : main(); break;
case 'n' : printf("ok, see ya!\n"); break;
}}
}

Never call main() . Put the program into a loop instead. Then you can simply read the response and automatically loop back to where you need to restart.

I would go back and edit my previous posts but there's no edit button at the bottom (where i saw it before).

Push the power button thingy on your tower.

I'm using it through the command prompt and just want it to stop, but yeah i know thats another option :-P

and walt, i used the return; now instead of main(); and it works, thanks for the advice.

still can't get out of the play again? n BUST play again? loop :-\

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.