Thread: Help with very simple war game (Displaying Ace, King, Queen, Jack)

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    3

    Help with very simple war game (Displaying Ace, King, Queen, Jack)

    Aloha everyone!

    This is my first time posting on this thread and I was wondering if I could get some help with my homework assignment.

    The code below is a very simple game of war that we need to create. We have only gone over int, floats, while, if and else statements and also basic functions (such as srand and making your own). so basically im a super noob... T.T

    This is what I need help on:
    "Instead of displaying numbers, show 2 thru 9, T(ten),J,Q,K, and A to represent cards (still 13 unique values)"


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
       /*Declaring variable*/
       int ans, card1, card2, card3, card4;
       int cardcalc();
       /* asking the user if he/she would like to play a game of war */
    
    
       printf("Would you like to play a game of war? Enter 1 for yes and 0 for no: ");
       scanf("%d" , &ans);
       
       while( 1 == ans)
       {
          /* RNGesus */
    
    
          srand(time(NULL));
          card1 = rand() % 13 +1; 
          card2 = rand() % 13 +1;
          card3 = rand() % 13 +1;
          card4 = rand() % 13 +1;
    
    
          printf("Player A recieved a %d and %d\n", card1, card2);
          printf("Player B recieved a %d and %d\n", card3, card4);
       /* card calculation to determin winner*/
    
    
          cardcalc( card1, card2, card3, card4);
       /* this is to either end the game or play again*/
          
    
    
          ans = ans - 1;
          
          printf(" Try again? Enter 1 for yes and 0 for no: ");
          scanf("%d", &ans);   
          
       
          ans = ans;
    
    
       }
       while( 0 == ans)
       {
          printf("Good bye\n");
          exit(0);
       }
    
    
    }
    
    
    /*functions*/
    
    
    cardcalc(c1, c2, c3, c4 )
    {
       int handA;
       int handB;
       
       handA = c1 + c2;
       handB = c3 + c4;
       
       if( handA > handB)
       {
          printf("Player A wins!@#$!@#$!@#$!@#$\n");
       }
       
       if( handA < handB)
       {
          printf("Player B wins!@#$!@#$!@#$!@#$#$%^^%&*\n");
       }  
    }

    - Mahalo

  2. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi...

    As it stands now the program spits out a few warnings....have you tried to fix those...?

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    3
    Quote Originally Posted by JohnGM View Post
    Hi...

    As it stands now the program spits out a few warnings....have you tried to fix those...?

    it doesn't have any errors when I compile and run it.. I'm using Vim editor through my schools unix.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,612
    Then you need to add some compile options to your compile line. Look what my compiler tells me about your code:

    main.c|3|error: return type defaults to ‘int’|
    main.c||In function ‘main’:|
    main.c|19|error: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]|
    main.c|59|error: return type defaults to ‘int’|
    main.c|59|warning: no previous declaration for ‘cardcalc’ [-Wmissing-declarations]|
    main.c||In function ‘cardcalc’:|
    main.c|59|error: type of ‘c1’ defaults to ‘int’|
    main.c|59|error: type of ‘c2’ defaults to ‘int’|
    main.c|59|error: type of ‘c3’ defaults to ‘int’|
    main.c|59|error: type of ‘c4’ defaults to ‘int’|
    main.c|74|warning: unknown conversion type character ‘^’ in format [-Wformat=]|
    main.c|74|warning: unknown conversion type character ‘&’ in format [-Wformat=]|
    main.c|76|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build failed: 7 error(s), 4 warning(s) (0 minute(s), 1 second(s)) ===|
    You should be compiling with (at a minimum) the -Wall -Wextra flags.

    Since I'm compiling using the C11 C standard (-std=c11) this program fails to properly compile. You should not be using default return values always explicitly state the return type, if you don't want to return a value use the void return type. The function main() should be defined to return an int.

    Also be careful about "special" characters in your format specifier strings, like the '%' character.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Then you need to add some compile options to your compile line. Look what my compiler tells me about your code:



    You should be compiling with (at a minimum) the -Wall -Wextra flags.

    Since I'm compiling using the C11 C standard (-std=c11) this program fails to properly compile. You should not be using default return values always explicitly state the return type, if you don't want to return a value use the void return type. The function main() should be defined to return an int.

    Also be careful about "special" characters in your format specifier strings, like the '%' character.

    Jim
    How do I go about doing this?

    sorry I barley know anything about this. This is my first programming class... ever.

    -keith

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Black Jack Game
    By AwesomeMMan in forum C Programming
    Replies: 2
    Last Post: 05-22-2009, 10:19 AM
  2. First program (Black Jack game)
    By 74466 in forum C++ Programming
    Replies: 17
    Last Post: 02-25-2006, 05:38 AM
  3. Help with Black Jack Game
    By Frantic- in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2005, 09:16 PM
  4. Black Jack Game
    By zMan in forum Windows Programming
    Replies: 1
    Last Post: 05-31-2003, 07:34 AM
  5. Console Black Jack game
    By Magma in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2003, 10:53 AM

Tags for this Thread