Thread: Question With Functions

  1. #16
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't know how to explain it any better than I have. It has nothing to do with the fact that it's a function. If you were doing it in main() it would have the same problem. You're overwriting the value of nSuit before you use it. The value you're passing to the function is doing meaningless because you're overwriting it.

    Maybe someone else will have better luck explaining it.
    If you understand what you're doing, you're not learning anything.

  2. #17
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    ok so i'm gonna try things out, imma try to bring nSuit=rand()%4+1; out of the function and see what happens, thanx for the help!

  3. #18
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yes, try that. That should give you a consistent output.
    If you understand what you're doing, you're not learning anything.

  4. #19
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also, if you use an array you could avoid all the switch/case stuff. For example, something like:
    Code:
    {
      char *suits[] = { "Hearts", "Spades", "Diamonds", "Clubs" };
      int nSuit = rand()%4;
    
      puts(suits[nSuit]);
    }
    If you understand what you're doing, you're not learning anything.

  5. #20
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Also, the return values of your functions, aka cCard and cSuit are never assigned anything. That may also be why you're getting some unexpected results.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #21
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    i'm going to try learn arrays when all my bugs in my program are gone lol! and right now there's a LOT! all are logical errors really, whew.

    Code:
    /*BLACK JACK WITH FUNCTIONS*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    /*Function to Deal a card*/
    
    char DealCard (nDCard)
    { char cCard;
    
    
    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);
    }
    
    
    int main(void)
    {
          int nDCard;
          char cDCard1, dev_c;
          
          srand(time(NULL));
    
          nDCard=rand()%13+1;
          cDCard1=DealCard(nDCard);
          printf ("\n");
          printf ("%c", cDCard1);
          
          
          scanf ("%c", dev_c);
    }
    
    /*
    Expected Output:
    Six of
    
    Six of
    
    Actual Output:
    Six of
    
    */
    what's wrong with my code now? it just ignores the printf! my book clearly says "..., we may also use the value at once.This can be done ... by printing the value on the screen..." but how come it doesnt do anything!

  7. #22
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Read my comment above. Also, you won't be able to print the entire string generated in your DealCard function unless you make DealCard return a character pointer (but I don't know if you're learned that yet).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #23
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    Quote Originally Posted by Happy_Reaper
    Also, the return values of your functions, aka cCard and cSuit are never assigned anything. That may also be why you're getting some unexpected results.

    i didnt assign any expression to it cuz i just need a variable to store whatever the switch would return.

    ur right, the example in my book put an expression in the return varibale, but that's because the function example in my book is mathematical, it's computing for something, in my case i just want it to return a string...

  9. #24
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I think this is kind of what you're trying to do. At any rate, maybe it will help you see how functions work:
    Code:
    itsme@itsme:~/C$ cat card.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    int pick_random_card(void)
    {
      return rand() % 52;
    }
    
    char *card_to_str(int card)
    {
      char *values[] = { "Ace", "Two", "Three", "Four", "Five", "Six",
                         "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
      char *suits[] = { "Hearts", "Spades", "Diamonds", "Clubs" };
      int value;
      int suit;
      static char buf[100];
    
      value = card % 13;
      suit = card % 4;
    
      sprintf(buf, "%s of %s", values[value], suits[suit]);
      return buf;
    }
    
    int main(void)
    {
      int card;
      char *str;
      int i;
    
      srand(time(NULL));
    
      // Show 5 random cards
      for(i = 0;i < 5;++i)
      {
        card = pick_random_card();
        str = card_to_str(card);
        puts(str);
      }
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./card
    Six of Diamonds
    Five of Clubs
    Six of Spades
    Nine of Spades
    Ace of Diamonds
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  10. #25
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by jarvis
    i didnt assign any expression to it cuz i just need a variable to store whatever the switch would return.
    Yeah, ok, but even if that's what you want to do, you're not doing it.

    Quote Originally Posted by jarvis
    ur right, the example in my book put an expression in the return varibale, but that's because the function example in my book is mathematical, it's computing for something, in my case i just want it to return a string...
    Then your function will have to return a char*.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #26
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    thank you for all the help. i just have to think of other ways do print that damn dealer card. i cant continue looking for bugs unless i knew how to solve this bug!

    thanks again.

    by the way, do i really need to learn character pointers to print strings from a function? got to ask our teacher about it, havent learned that yet.

  12. #27
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    do i really need to learn character pointers to print strings from a function?
    You do if you want to return a string from a function. Alternatively, you could pass your array to the function and fill it in the function. Then you could just print it in main() without your function having to actually return it.

    While trying to learn how functions work, I would definitely start with something simpler than strings. Play with integers or something first until you see how those work.
    If you understand what you're doing, you're not learning anything.

  13. #28
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by jarvis
    by the way, do i really need to learn character pointers to print strings from a function? got to ask our teacher about it, havent learned that yet.
    Yes, you will have to, so you might want to get on that as soon as possible.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #29
    Registered User
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    27
    i understood the code, somewhow, but some parts of it were never taught yet, or i guess i wouldnt be taught to us, we have to research on our own, the arrays and buf parts things like that, i dont know that yet...thanx for the code i'll study it closely

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 09:39 AM