Integer returned is some seemingly random number that is really big or really small

The following is my entire program. To use it, run it then input this string:

4H 8D 6S 3S 2C 9D KH 9C 6H 7D QC 5D 7C

Hit enter to go to a new line then type a period and hit enter again for the program to evaluate the string.

The output should be a number that is reasonably small (between -50 and 50). I can't give an exact answer because I haven't been able to test it since all these values are distorted. You can cout each of the values to see which ones are messed up. What I get are really big or really small numbers that have zero context to what I'm trying to do. I believe it's some memory-related error after a brief googling but I can't pinpoint where.

Really desperate and any help is appreciated.

To terminate program, initiate EOF via CTRL + Z or CTRL + D.

EDIT: Fixed code, still same problem.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

int suitCount(int C, int D, int H, int S){
    int points = 0;
    if (C > 4){
        points += C - 4;
    }
    else if (C == 0){
        points += 3;
    }
     else if (C == 1){
        points += 2;
    }
    else if (C == 2){
        points += 1;
    }
    else if (D > 4){
        points += D - 4;
    }
    else if (D == 0){
        points += 3;
    }
    else if (D == 1){
        points += 2;
    }
     else if (D == 2){
        points += 1;
    }
    else if (H > 4){
        points += H - 4;
    }
    else if (H == 0){
        points += 3;
    }
    else if (H == 1){
        points += 2;
    }
    else if (H == 2){
        points += 1;
    }
    else if (S > 4){
        points += S - 4;
    }
    else if (S == 0){
        points += 3;
    }
    else if (S == 1){
        points += 2;
    }
    else if (S == 2){
        points += 1;
    }
    return points;
}

int main(){

    string input;
    istringstream sso;
    vector <string> hand;
    int match = 0;
    bool badFormat = false;
    bool duplicate = false;
    int J;
    int Q;
    int K;
    int C;
    int D;
    int A;
    int H;
    int S;
    int points = 0;
    string handConcatenate = "";
    string cardsArray[52] = {"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
                            "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",  
                            "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
                            "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"};
    vector<string> cards(&cardsArray[0], &cardsArray[0]+52);
    /* initializing vector--requires g++ "-std=c++11" flag)
    vector <string> cards {"2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC",
                            "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD",  
                            "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", "AH",
                            "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"};
    */

    cout << "Enter your string: " << endl;

    while (cin >> input){
        if (input != "."){
            sso.str(input);
            while(getline(sso, input, ',')) {
                hand.push_back(input);
            }
        sso.clear(); 
        }
        else {
            // BAD FORMAT
            for (int i=0;i<hand.size();i++){  
                if (find(cards.begin(), cards.end(), hand.at(i)) != cards.end()){
                    match++;
                }
            }
            if (match != hand.size()){
                cout << "BAD FORMAT" << endl;
                badFormat = true;
            }
            // WRONG NUMBER OF CARDS
            else if (hand.size() != 13){
                cout << "WRONG NUMBER OF CARDS" << endl;
            }
            // DUPLICATE CARD
            else{
                for(int i = 0; i < hand.size(); i++){
                    for(int j = i+1; j < hand.size(); j++){
                        if(hand.at(i) == hand.at(j)){
                            duplicate = true;
                        }
                    }
                }
                if (duplicate){
                    cout << "DUPLICATE CARDS" << endl;
                }
            }
            if ((!badFormat) && (hand.size() == 13) && (!duplicate)){
                for(int i=0;i<hand.size();i++){
                    handConcatenate += hand.at(i);
                }
                //cout << handConcatenate << endl;
                for(int i=0;i<handConcatenate.length();i++){
                    if (handConcatenate[i]== 'J'){
                        points += 1;
                        J++;           
                    }    
                    if (handConcatenate[i] == 'Q'){
                        points += 2;     
                        Q++;      
                    }  
                    if (handConcatenate[i] == 'K'){
                        points += 3; 
                        K++;          
                    }  
                    if (handConcatenate[i] == 'A'){
                        points += 4; 
                        A++;          
                    }  
                    if (handConcatenate[i] == 'C'){
                        C++;          
                    }  
                    if (handConcatenate[i] == 'D'){
                        D++;          
                    }  
                    if (handConcatenate[i] == 'H'){
                        H++;          
                    }  
                    if (handConcatenate[i] == 'S'){
                        S++;          
                    }  

                    if (A == 0){
                        points -= 1;
                    }
                    else if (A == 4){
                        points += 1;
                    }
                    else if ((J + Q + K) == 1){
                        points -= 1;
                    }
                }

                cout << points << endl;
                cout << points + suitCount(C, D, H, S) << endl;
            }
            // Empty hand vector to build a new one
            hand.clear();
            handConcatenate = "";
            // Reset match count
            match = 0;
            points = 0;
        }
    }   

    return 0;

}




          
Last edited on
Your if statements are incorrect. This is a mistake a lot of people make.

= is an assignment operator.
== is a comparison operator.

else if (C = 2){
Here for example, you want to compare C to 2, which you are not doing. Change all of these to

else if (C == 2){ // note ==
Thank you. I checked and changed all of them thoroughly but I still have the same problem (half of it was fixed from this): When I do

1
2
cout << points << endl;
cout << points + suitCount(C, D, H, S) << endl;


The first number seems fine now but the second number which involves the function returns a really high number so that's where the problem is most likely. I changed it all to "==".

Are there other potential problems? Perhaps syntax?

EDIT: Actually problem is not within the function either--I just copied it and used it in a new program with just itself.
Last edited on
You need to initialize the integers at lines 70-77 to zero.
Topic archived. No new replies allowed.