Card Guessing Game, comparisons not correct

Wondering if anyone can help me, I've created a card guessing (higher or lower) but for some reason I can't get the comparisons 100%. For instance it will show a 10 of hearts, I'll say lower, and it was output "Wrong" even though the next card is lower. This doesn't always happen, but I'm really confused as to why it does.

card.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CARD_H
#define CARD_H
#include <iostream>
using namespace std;

class Card {
public:
        Card();
        Card(int card);
        string getSuit() const;
        string getRank() const;

private:
	int _card;
	static const string Rank[];
	static const string Suit[];

};
#endif 


card.cpp
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
#include <string>
#include <sys/time.h>
#include <cmath>
#include "card.h"
#include "game.h"
using namespace std;

const string Card::Rank[] =  {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}; //initializes ranks

const string Card::Suit[] = {"Spades", "Hearts", "Clubs", "Diamonds"};  //initializes suits

Card::Card(){
	_card=0;
}

Card::Card(int card){
	_card=card;
}

string Card::getSuit() const{    //returns suit
return Suit[_card/13];
}

string Card::getRank() const{    //returns Card rank
return Rank[_card%13];

}



game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
using namespace std;
#ifndef GAME_H
#define GAME_H
#include "card.h"
class Game {
	public:
	Game();
	void shuffle();
	void play();
	void menu();
	void demo();
	void announceWinner();

	private:
    Card cards[52];
	int playerscore;
	int housescore;
};
#endif 


and I've included the part of game.cpp that is important.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <sys/time.h>
#include <string>
#include <iostream>
#include <ctime>
using namespace std;
#include "game.h"
#include "card.h"

Game::Game() {                  //initializes deck
    playerscore=0;
    housescore=0;
    for (int i=0; i<52; i++) {
        cards[i] = Card(i);
    }
}

void Game::shuffle(){           //shuffles deck
    srand ( time(NULL) );

    for (int i=0; i<(51); i++) {
        int r = rand() % 52;
        Card temp = cards[i];
        cards[i] = cards[r];
        cards[r] = temp;
    }
}

void Game::announceWinner(){} //Announces winner (not shown)

void Game::demo(){} //game demo (not shown)

void Game::menu(){}  //game menu (not shown)

void Game::play(){     //plays game
    string input;
    char choice;
    shuffle();

    cout <<endl;
    cout <<endl;
    cout <<"The Computer will deal you a card one at a time"<<endl;
    cout <<"Press 'H' if you think the next card will be higher"<<endl;
    cout <<"Press 'L' if you think the next card will be lower"<<endl;
    cout <<"Press 'Q' to return to Quit"<<endl;


    for (int i=0; i<51; i++){

        Card current = cards[i];  //initializes current card index to variable
        Card next = cards[i+1];   //initializes next card index to variable

        cout << "The computer draws "<< current.getRank() << " of " << current.getSuit()<<endl;
        cout << next.getRank()<<" of " <<next.getSuit()<<endl;

        cin >> input;
        choice = input.at(0);
        choice=tolower(choice);

        if ((choice=='l'&& next.getRank()<current.getRank())){
            cout <<"Correct!"<<endl;
           playerscore=playerscore+2;
        }

        else if ((choice=='l' && next.getRank()>current.getRank())){
            cout <<"Wrong!"<<endl;
            housescore=housescore+2;
        }

        else if ((choice=='h' && next.getRank()>current.getRank())){
            cout <<"Correct!"<<endl;
            playerscore=playerscore+2;
        }

        else if ((choice=='h' && (next.getRank()<current.getRank()))){
            cout <<"Wrong!"<<endl;
            housescore=housescore+2;
        }

        else if ((((choice=='h') || (choice=='l')) && (current.getRank()==next.getRank()))){
            cout << "The cards were equal, its a draw!"<<endl;
            playerscore++;
            playerscore++;
        }

        else if (choice=='q'){
            announceWinner();  //announces game winner if user quits early
        }

        else {
            cout <<"Invalid input, please enter 'H' for Higher 'L' forlower or 'Q' to quit"<<endl;
            i--;
        }
    } //end for
    announceWinner(); //announces game winner if entire game is played
} //end function
Also it seems that it happens the most when a "10" or "Ace" is involved
Last edited on
You are comparing the ranks of the cards as strings against one another.

So when you are comparing say an Ace against a 3 the program looks at this as:
"Ace" < "3". When it compares 2 strings, it just goes by alphabetical order. Since numbers come before letters, the above will return False because "A" comes after "3".

This is the main reason 10 and the face cards have the problems. Say you are comparing "10" against "3" now. 3 should obviously be the lower card, but again they are stored as strings, so it goes by alphabetical order. "10" starts with "1", which is actually lower than "3", so "10" < "3" would actually come out true when this is obviously false.

You are going to have to convert each number to an integer rather than a string ("Jack" = 11, "Queen" = 12, and "King" = 13, and "Ace" = either 1 or 14 depending on how you are counting them as high or low) and then compare them.
Yeah, you're completely right. I'm just not exactly sure how to go about that.
Last edited on
Topic archived. No new replies allowed.