-
Question With Functions
hi! i have a question with functions!
i created a function which deals a card, basically what i wanted to do is use the first return value again.
i dunno how!
example:
Code:
DealCard();
/* codes codes codes */
/*then i want to use/print the value first generated by the DealCard Function */
i tried
Code:
nDCard=DealCard();
/*code code code */
printf ("%c", nDCard);
but what happened was it dealt another card.
help me!
thanx!
-
A guess would be that the problem lies either here
Code:
/*codes codes codes */
or here
Code:
/*code code code */
-
meaning..
Code:
nDCard1=DealCard();
printf ("%c", nDCard1);
is a correct syntax for printing the first generated value?
DealCard() is a function for randomly generating the card disregarding the suit.
-
okay so here is the program flow..(this is a blackjack game)
1.deals one card to the dealer then prints it
2. prints concealed as the second card of the dealer should be concealed.
3. the next codes would be dealing the card for the player so that really doesnt matter
4. >>>if the player stands, the first value printed should be displayed..<<<
5. then deals a second card to the dealer
6. then codes to determine if the dealer is busted or not.
algo number is my question, how do i do that? cuz with the code above, it randlmly deals another card to the dealer instead of printing the first dealt card..
please i really need help!
-
someone?? pls pls pls pls
-
First, you shouldn't bump your own posts like that.
Second, a function is only called as many times as you call it. That's one of the beautiful things about C. There's no behind-the-scenes magic.
Third, maybe you should post all of the relevant code instead of making everyone guess at what the problem is.
-
i'm sorry.
Code:
/*Function to Deal a card*/
char DealCard (nDCard)
{ char cCard;
nDCard=rand()%13+1;
switch (nDCard)
{ case 1: printf ("Ace of");
break;
case 2: printf ("Two of");
break;
case 3: printf ("Three of");
break;
case 4: printf ("Four of");
break;
case 5: printf ("Five of");
break;
case 6: printf ("Six of");
break;
case 7: printf ("Seven of");
break;
case 8: printf ("Eight of");
break;
case 9: printf ("Nine of");
break;
case 10: printf ("Ten of");
break;
case 11: printf ("Jack of");
break;
case 12: printf ("Queen of");
break;
case 13: printf ("King of");
break;
}
return (cCard);
}
Code:
/*Function to determine card suit*/
char Suit (nSuit)
{ char cSuit;
nSuit=rand()%4+1;
switch (nSuit)
{ case 1: printf (" Hearts"); break;
case 2: printf (" Diamonds"); break;
case 3: printf (" Spades"); break;
case 4: printf (" Clubs"); break;
}
return (cSuit);
}
Code:
cCard=DealCard();
cSuit=Suit();
printf ("\n");
printf ("%c", cCard);
printf ("%c", cSuit);
DealCard();
Suit();
/*expected output, assuming the value random generated is Three of Hearts*/
Three of Hearts
Three of Hearts
Four of Spades
Code:
/*what happens is this, assuming the same value is generated*/
Three of Hearts
Four of Spades
i'm sorry again. pls help me and thanx a lot
-
You're totally ignoring what you pass to your functions. Look at this one:
Code:
char Suit (nSuit)
{ char cSuit;
nSuit=rand()%4+1;
switch (nSuit)
{ case 1: printf (" Hearts"); break;
case 2: printf (" Diamonds"); break;
case 3: printf (" Spades"); break;
case 4: printf (" Clubs"); break;
}
return (cSuit);
}
You pass it a value, but then you overwrite it with your call to rand() right away.
-
i didnt quite get what u mean...
-
If you do:
Code:
{
int a = 5;
a = 8;
}
Is a still going to be 5?
-
nsuit is an integer, the formula that i did was for suit function to generate a number from one to 4, but i dont need the number, what i need is the suit itself so i used switch. what i want to do is transfer the return value to a variable which i can reuse again without it changing value.
and that i dont have any idea how to do..oh my god i'm so confused
-
It has nothing to do with the way you're calling the function. Just look inside the function. You're overwriting what you passed to the function with the call to rand().
-
we havent studied functions yet so i really dont know what i'm doing, i just followed the syntax on our book..wait...so u mean ..ah ok now i'm geting it, now what am i suppose to put it inside the formal parameters (which i dunno what it is?) can i leave it blank ? like..
Code:
char Suit()
{ char cSuit;
int nSuit;
nSuit=rand()%4+1;
switch (nSuit)
{ case 1: printf (" Hearts"); break;
case 2: printf (" Diamonds"); break;
case 3: printf (" Spades"); break;
case 4: printf (" Clubs"); break;
}
return (cSuit);
}
?
-
call it like you had: char Suit(int nSuit)
It has nothing to do with the fact that it's a function. You're simply overwriting the value of your variable without ever using it
-
wait so how am i gonna print its value then? then use the function again to deal a card? i really hope i'm not bugging you