### Problem getting correct score in simple Blackjack program

I'm having trouble spotting the error that is causing the score to be computed from the two random ints instead of being processed through the if statements I have. I think it has something to do with the variable declaration for the computeScore function.

#include <iostream>
#include <ctime>
#include "graph1.h"

using namespace std;

//Prototypes
void getCoords(int& x, int& y);
void deal(int& card1, int& card2);
int displayCard(int card,int x, int y);
int computeScore(int card, int card2);
void displayResults(int x, int y, int score);

int main()
{
//Variable Declaration/Initialization
int card1 = 0; //Random # for card1
int card2 = 0; //Random # for card2
int score = 0; //score accumulated for both scores
int x = 0; //x-coord where leftmost card is displayed
int y = 0; //y-coord where leftmost card is displayed

//Display graphics
displayGraphics();

//Deal the cards
deal(card1,card2);

//Get the coords for the cards
getCoords(x,y);

//Display the 1st card
displayCard(card1,x,y);

//Display 2nd card
displayCard(card2,x+150, y);

//Compute the score
score = computeScore(card1,card2);

//Display the score even with leftmost card at y-coord = 400
displayResults(x,400,score);

return 0;

}

//Implement all functions here
void getCoords(int& x, int& y)
{
cout << "Enter coordinates for card display: " << endl;
cin >> x >> y;
}
void deal(int& card1, int& card2)
{
srand(time(0));

card1 = rand()%13 + 2;

do
{
card2 = rand()%13 + 2;

}while(card1 == card2);
}
int displayCard(int card, int x, int y)
{
int obj_no = 0;
if (card == 2)
obj_no = displayBMP("h2.bmp", x, y);
else if (card == 3)
obj_no = displayBMP("h3.bmp", x, y);
else if (card == 4)
obj_no = displayBMP("h4.bmp", x, y);
else if (card == 5)
obj_no = displayBMP("h5.bmp", x, y);
else if (card == 6)
obj_no = displayBMP("h6.bmp", x, y);
else if (card == 7)
obj_no = displayBMP("h7.bmp", x, y);
else if (card == 8)
obj_no = displayBMP("h8.bmp", x, y);
else if (card == 9)
obj_no = displayBMP("h9.bmp", x, y);
else if (card == 10)
obj_no = displayBMP("h10.bmp", x, y);
else if (card == 11)
obj_no = displayBMP("hj.bmp", x, y);
else if (card == 12)
obj_no = displayBMP("hq.bmp", x, y);
else if (card == 13)
obj_no = displayBMP("hk.bmp", x, y);
else if (card == 14)
obj_no = displayBMP("ha.bmp", x, y);

return(obj_no);
}
int computeScore(int card1, int card2)
{
int score = (card1 + card2);
if (card1 == 11)
{ if (card2 == 11)
int score = (10 + 10);
else if (card2 == 12)
int score = (10 + 10);
else if (card2 == 13)
int score = (10 + 10);
else if (card2 == 14)
int score = (10 + 11);
else
int score = (10 + card2);
}
else if (card1 == 12)
{ if (card2 == 11)
int score = (10 + 10);
else if (card2 == 12)
int score = (10 + 10);
else if (card2 == 13)
int score = (10 + 10);
else if (card2 == 14)
int score = (10 + 11);
else
int score = (10 + card2);
}
else if (card1 = 13)
{ if (card2 == 11)
int score = (10 + 10);
else if (card2 == 12)
int score = (10 + 10);
else if (card2 == 13)
int score = (10 + 10);
else if (card2 == 14)
int score = (10 + 11);
else
int score = (10 + card2);
}
else if (card1 = 14)
{ if (card2 == 11)
int score = (11 + 10);
else if (card2 == 12)
int score = (11 + 10);
else if (card2 == 13)
int score = (11 + 10);
else if (card2 == 14)
int score = (11 + 11);
else
int score = (11 + card2);
}
else if (card2 = 11)
{ if (card1 == 11)
int score = (10 + 10);
else if (card1 == 12)
int score = (10 + 10);
else if (card1 == 13)
int score = (10 + 10);
else if (card1 == 14)
int score = (10 + 11);
else
int score = (10 + card1);
}
else if (card2 = 12)
{ if (card1 == 11)
int score = (10 + 10);
else if (card1 == 12)
int score = (10 + 10);
else if (card1 == 13)
int score = (10 + 10);
else if (card1 == 14)
int score = (10 + 11);
else
int score = (10 + card1);
}
else if (card2 = 13)
{ if (card1 == 11)
int score = (10 + 10);
else if (card1 == 12)
int score = (10 + 10);
else if (card1 == 13)
int score = (10 + 10);
else if (card1 == 14)
int score = (10 + 11);
else
int score = (10 + card1);
}
else if (card2 = 14)
{ if (card1 == 11)
int score = (11 + 10);
else if (card1 == 12)
int score = (11 + 10);
else if (card1 == 13)
int score = (11 + 10);
else if (card1 == 14)
int score = (11 + 11);
else
int score = (11 + card1);
}
else
int score = (card1 + card2);

return(score);
}
void displayResults(int x, int y, int score)
{
gout << setPos(x,400) << "Total score is: " << score << endg;
if (score == 21)
gout << setPos(x,415) << "Blackjack!!!" << endg;
else if (score >= 22)
gout << setPos(x,415) << "Bust!!!" << endg;
}
Edit this post using the source code format.
As of right now this is too annoying to read.
this is how prototypes are declared.
 ``12345`` ``````void getCoords(int&, int&); void deal(int&, int&); int displayCard(int,int, int); int computeScore(int, int); void displayResults(int, int, int);``````

And from what I see in your code, some function which was supposed to pass the value of card1 and card2 to the function `computeScore(int, int)` did not. so the default values of '0' set (default value) in card1 and card2's declaration was used.

Last edited on
Another problem is that you keep declairing another "score" in every if/else if statement. The "scores" inside those statements go out of scope when the statement is over.
Topic archived. No new replies allowed.