Thread: Newbie

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie

    hey guys I need some help, I think something wrong with the if statement in main, I can't get it right. please can someone tell me what I am doing wrong. Thanks

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    typedef enum {Spades, Hearts, Diamonds, Clubs} Suit ;
    
    typedef struct 
    {
      Suit suit ;
      int  facevalue;
    }card ;
    
    typedef struct
    {
      char name[80];
      card card1;
      card card2;
      int score;
    }player;
    
    void deal(player* p);
    void calculate(player* p);
    void displayCard(const card* c, int count);
    void displayPlayerCard(const player* p);           
    
    int main(void)
    {
       /*initialize player1 and player2 */
       player player1 = {"Joe Smith", {5,Spades}, {5,Hearts},0};
       player player2 = {"David Jones", {9,Clubs}, {9, Diamonds}, 0};
      
        srand(time(0));
    
       /* Player 1 */
    
       deal(&player1);
       calculate(&player1);
       printf("Player 1 :%s \n", player1.name);
       displayPlayerCard(&player1);
       printf("Total score is %d.\n\n", player1.score);
    
    /* Player 2 */
       deal(&player2);
       calculate(&player2);
       printf("Player 2 :%s \n", player2.name);
       displayPlayerCard(&player2);
       printf("Total score is %d.\n\n", player2.score);
       return 0;
    
    if (player1.score > player2.score)
    
    {
    printf("%s is the winner\n", player1.name);
    
    else if (player1.score < player2.score)
    
    printf("%s is the winner\n", player2.name);
    
    else printf("Game tied\n") 
    
    }
    }
    
    
    void deal(player* p)
    {
      (*p).card1.facevalue = rand()% 13 + 1;
      (*p).card1.suit = rand( ) % 4  ;
    
       (*p).card2.facevalue = rand()%13 +1;
       p->card2.suit = rand( ) % 4  ;
    }
    
    
    
    void calculate(player* p)
    {
     p->score = p->card1.facevalue + p->card2.facevalue;
    }
    
    void displayPlayerCard(const player* p)
    { 
       displayCard(&(p->card1), 1);
       displayCard(&(p->card2), 2);
    }
    
    void displayCard(const card* c, int count)
    {
      int number = c->facevalue;
      int suit = c->suit;
      printf("Card %d: ", count);
    
      switch(number) 
      {
        case 1 : printf("Ace"); break;
        case 11 : printf("Jack"); break;
        case 12: printf("Queen"); break;
        case 13 : printf("King"); break;
        default: printf("%d", number);
      }
    
      switch(suit)
      {
        case 0 : printf(" of Spades\n"); break;
        case 1 : printf(" of Hearts\n"); break;
        case 2 : printf(" of Diamonds\n"); break;
        case 3 : printf(" of Clubs\n"); break;
      }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Tell us what you want it to do and what it is doing. At first glance it would appear that you're doing something funky with your braces.
    edit: And I can't find where main() is returning anything.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    the player with higher total points win the game and the if() satement is there is do that. Acutally once I get the if statement right I will write it as a different function.

    Thanks
    Last edited by jat421; 03-27-2005 at 12:08 PM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by jat421
    the player with maximum total points win the game and the if() satement is there is do that. Acutally once I get the if statement right I will write it as a different function.

    Thanks
    What's up with this?

    Code:
       return 0;
    
    if (player1.score > player2.score)
    Is this the "if" statement that you are talking about?


    Well, remove the return, and you have this:
    Code:
    if (player1.score > player2.score)
    
    {
    printf("%s is the winner\n", player1.name);
    
    else if (player1.score < player2.score)
    
    printf("%s is the winner\n", player2.name);
    
    else printf("Game tied\n") 
    
    }
    Misplaced "else", and missing semicolon (the semicolon is easy; let's fix the "else"):

    In general, you can have something like this for your if statements:

    Code:
      if (Something) {
        /* do something */
      }
      else if (SomethingElse) {
       /* do something else */
      }
      else {
        /* do whatever is appropriate if the first two failed */
      }
    
      return 0;
    Try it. (I recommend using the braces {} even if you don't need them, but that's just me.)

    Regards,

    Dave

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Thanks that worked , lol what a stupid mistake...I think I need some cofffe

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    It shouldn't be easy to make a mistake like this. Everything looks like it is indented fine except for that if/else if structure and that is probably why you lost track of your braces etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM