problem playing with vectors

Hey guys, I decided to have a go at using vectors while writing a program involving my favourite card game (bridge). For those who aren't familiar with the game, one of the most common ways of evaluating ones hand is known as High Card Points (HCP) where an ace is valued at 4 points, a king is 3, a queen is 2 and a jack is 1. For now, I'm trying to keep it basic and trying to work out the average HCP given a HCP range with no suit distribution to consider (planning to add it later). When I tried to compile this, I got 4 errors all saying

C:\C++ Game\bridge\main.cpp|69|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]| (well, the line number changes, but the error is the same)

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
98
99
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    string cards[52]={"SA", "SK", "SQ", "SJ", "ST", "S9", "S8", "S7", "S6", "S5", "S4", "S3", "S2",
                    "HA", "HK", "HQ", "HJ", "HT", "H9", "H8", "H7", "H6", "H5", "H4", "H3", "H2",
                    "DA", "DK", "DQ", "DJ", "DT", "D9", "D8", "D7", "D6", "D5", "D4", "D3", "D2",
                    "CA", "CK", "CQ", "CJ", "CT", "C9", "C8", "C7", "C6", "C5", "C4", "C3", "C2"};
    srand ( unsigned (time(0) ) );
    vector<string> deck;
    for (int i=0; i<52; i++)
    {
        deck.push_back(cards[i]);
    }
    bool programactive=true;
    while (programactive)
    {
        cout << "press Q to quit or S to simulate" << endl;
        string command;
        cin >> command;
        if (command=="Q" || command=="q")
        {
            programactive=false;
        }
        else if (command=="S" || command=="s")
        {
            cout << "enter min-max HCP" << endl;
            int minHCP, maxHCP;
            cin >> minHCP;
            cin >> maxHCP;
            /*cout << "enter min-max spades" << endl; // for suit distribution later
            int minSpades, maxSpades;
            cin >> minSpades;
            cin >> maxSpades;
            cout << "enter min-max hearts" << endl;
            int minHearts, maxHearts;
            cin >> minHearts;
            cin >> maxHearts;
            cout << "enter min-max diamonds" << endl;
            int minDiamonds, maxDiamonds;
            cin >> minDiamonds;
            cin >> maxDiamonds;
            cout << "enter min-max Clubs" << endl;
            int minClubs, maxClubs;
            cin >> minClubs;
            cin >> maxClubs;*/
            cout << "how many hands do you want to test?" << endl;
            int Hands;
            cin >> Hands;
            int totalHCP=0;
            int qualifiedHands=0;
            while (qualifiedHands < Hands)
            {
                int HCP=0;
                int spades=0;
                int hearts=0;
                int diamonds=0;
                int clubs=0;

                std::random_shuffle(deck.begin(),deck.end());
                for (int i=0; i<13; i++)
                {
                    if (deck[i][1]=="A") // this is where the first error is, I need to extract the second character from each element
                    {
                        HCP+=4; 
                    }
                    else if (deck[i][1]=="K")
                    {
                        HCP+=3;
                    }
                    else if (deck[i][1]=="Q")
                    {
                        HCP+=2;
                    }
                    else if (deck[i][1]=="J")
                    {
                        HCP++;
                    }
                }
                if (HCP>=minHCP && HCP<=maxHCP)
                {
                    totalHCP+=HCP;
                    qualifiedHands++;
                }
            }
            double averageHCP;
            averageHCP=totalHCP/Hands;
            cout << averageHCP << endl;
        }
    }
    return 0;
}
Last edited on
You declared the vector as

vector<string> deck;

so expression deck[i] has type std::string that is you can write

std::string s = deck[i];

In this case this expression

deck[i][1]

will be equivalent to

s[1]

Expression s[1] has type char. However you are trying to compare it with a string literal

deck[i][1]=="A")

that in expressions is implicitly converted to type const char *

So you are trying to compare an object of type char with an object of type const char * and the compiler reports that there is no such conversion function that would allow to convert one type to anothyer that the objects could be compared.
I'm not too sure why, but apparently changing "A" to 'A' fixes the problem. I'd have thought I would still be having the same issue comparing a char to a const char, or is it to do with the compiler evaluating "A" as 65 (hence the error talking about the illegal pointer to integer comparison)?
I explained you why the error occurs. So I do not understand your words that

"I'm not too sure why, but apparently changing "A" to 'A' fixes the problem"

So you are trying to compare an object of type char with an object of type const char * and the compiler reports that there is no such conversion function that would allow to convert one type to anothyer that the objects could be compared.


I'm not too sure why, but apparently changing "A" to 'A' fixes the problem


Quite simple really, by saying:
 
if (deck[i][1]=='A')

Am I not doing the same comparison I was before (comparing an object of type char to an object of type const char*)?
Last edited on
Am I not doing the same comparison I was before

no, before you were comparing deck[i][1] which was of type char to "A" which of type string. Changing "A" to 'A' fixes the problem because you are then comparing two of the same types, char and char. A string is a series of char's.

essentially before it was 'A' == "A", and now its 'A' == 'A'
Last edited on
Topic archived. No new replies allowed.