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

1. ## 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. Hi...

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

3. Originally Posted by JohnGM
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.

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. Originally Posted by jimblumberg

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?