Help ( war card game)

Hi i dont get why this doesn't work.... Could someone lend me a hand?
The code works fine when changing numbers higher than 10 into Jack, Queen, King and Ace the first time, but the second time even though it's the same code it doesn't work... and also it always assigns the same values to both the computer and the human...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    srand(time (0));
    
    int a = (2 + rand() % 13);
    int b = (rand() % 4);
    cout << "The computer's card is a ";
    
    
    if (a <= 10) cout << a;
    if (a == 11) cout << "Jack";
    if (a == 12) cout << "Queen";
    if (a == 13) cout << "King";
    if (a == 14) cout << "Ace";
    
    if (b == 0) cout << " of Spades." << endl;
    if (b == 1) cout << " of Diamonds." << endl;
    if (b == 2) cout << " of Hearts." << endl;
    if (b == 3) cout << " of Clubs." << endl;
    //end of computer card calculation
    
    int c = (2 + rand() % 13);
    int d = (rand() % 4);
    cout << "Human's card is a ";
    
    if (c <= 10) cout << a;
    if (c == 11) cout << "Jack";
    if (c == 12) cout << "Queen";
    if (c == 13) cout << "King";
    if (c == 14) cout << "Ace";
    
    if (d == 0) cout << " of Spades." << endl;
    if (d == 1) cout << " of Diamonds." << endl;
    if (d == 2) cout << " of Hearts." << endl;
    if (d == 3) cout << " of Clubs." << endl;
    //human  card calculation ends
    
    if (a == c) cout << "It's a tie!" << endl;
    if (a >= c) cout << "Computer wins!" << endl;
    if (a <= c) cout << "Human wins!" << endl;
    cout << endl;   
}
Last edited on
@MakoBelgium

I see you show the problem is solved, but did you actually figure out the cause of the problem?

If not..

Line 31, should be if (c <= 10) cout << c;
Line 44 and 45, should not have the = in them. Just the > and <
i saw the error in line 31 that's how i got it to work
using >= & <= works fine for me though?
@MakoBelgium

What would get printed if you and the computer had the same face card? All three lines would get printed, cause they are all true. In order for no tie, one card must have a higher value, so the program should be checking only if either players card is the higher of the two, or of equal value.
Last edited on
Topic archived. No new replies allowed.