Game of 21. A lot of Errors :(

I don't know what is wrong with this. And the IDE I'm using is Visual Studio 2008.

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);


void main()
{
	char keepPlaying = "n"; //loop control variable

	do 
	{
		play21();
          
		//keep playing?
		cout << "Do you want to play another hand (y/n)? ";
		cin >> keepPlaying;
	}while(keepPlaying == "Y" || keepPlaying == "y");
}

void play21(void)
{
	//play one hand of 21

	//randomize the cards
	srand((int) time(0));

	// deal the cards
	int person = dealCards(2, "Your Cards: ");
	cout << " = " << person << endl;
	int house = dealCards(2, "Computers Cards: ");
	cout << " = " << house << endl;

	// Ask if human wants a hit and keep hitting...
	hit(person);
	cout << endl;

	//Determine if computer takes a hit
	while ((house < person) && (house <= 21) && (person <= 21)) 
	{
		house += dealCards(1, "The Computer takes a card ");
		cout << endl;
	}

	//show who won....
	determineWinner(person, house);
}

void determineWinner(int humanScore, int houseScore) 
{
//Compare the scores to see who won
//Both the human and the house score totals are provided as arguments
//Display total scores and indicate winner
//possible outcomes: human wins, computer wins, tie
	if (humanScore > houseScore)
		cout << "You win!\n";
	else if (humanScore < houseScore)
		cout << "Computer wins!\n";
	else
		cout << "It was a tie.\n";
}

int dealCards(int numberOfCards, string message)
{
//This function deals the cards
//The number of cards to be dealt is provided as an argument
//A message indicating which player is receiving the cards is also
//given as an argument
//The player message and the cards dealt is displayed to the screen
//the total value of the dealt cards  is returned

	int playerScore;
	cout << message;

	srand(time(0));

	for (int i = 1; i <= numberOfCards; i++)
	{
		int cardValue = rand() % 10 + 1;
		playerScore = playerScore + cardValue;
	}

	return playerScore;
}

void hit(int &playerScore)
{
//This function asks the human if they want another card -- 'a hit'
//the player's score total is accumulated as they take cards
//the player can continue taking cards until they wish to stop or they exceed 21
//After a card is taken (use the dealCards function) the user's current total is displayed
//If the user goes over 21 'busted' is displayed
	char hit = "y";
	char playerBusted = "n";

		while ((hit == "y") || (hit == "Y"))
		{

			if (playerBusted == "n")

				cout << "Do you want to hit? (y/n) ";\
				cin >> hit;

				if ((hit == "y") || (hit == "Y"))

					dealCards(1, "Hit: ");

				else

					cout << "\n";

			if (playerBusted == "y")

				cout << "You have busted!\n";

				hit = "n";
		}
}

int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
	return 1 + rand() % (upperLimit - lowerLimit + 1);
}
What are the errors? Apologies but you probably won't find many people who will compile the code for you just to see what the problems are.
error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'

I have like 17 of these errors.
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
/* Specification:
First Name Last Name  
Lab 4 Exercise 3
This program plays a game 
of 21 with the user*/

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);


void main()
{
	int keepPlaying = 0; //loop control variable

	do 
	{
		play21();
          
		//keep playing?
		cout << "Do you want to play another hand?\n";
		cout << "Press 1 for Yes\n";
		cout << "Press 0 for No\n";
		cin >> keepPlaying;
	}while(keepPlaying == 1);
}

void play21(void)
{
	//play one hand of 21

	//randomize the cards
	srand(static_cast<int>(time(0)));

	// deal the cards
	int person = dealCards(2, "Your Cards: ");
	cout << " = " << person << endl;
	int house = dealCards(2, "Computers Cards: ");
	cout << " = " << house << endl;

	// Ask if human wants a hit and keep hitting...
	hit(person);
	cout << endl;

	//Determine if computer takes a hit
	while ((house < person) && (house <= 21) && (person <= 21)) 
	{
		house += dealCards(1, "The Computer takes a card ");
		cout << endl;
	}

	//show who won....
	determineWinner(person, house);
}

void determineWinner(int humanScore, int houseScore) 
{
//Compare the scores to see who won
//Both the human and the house score totals are provided as arguments
//Display total scores and indicate winner
//possible outcomes: human wins, computer wins, tie
	if (humanScore > houseScore)
		cout << "You win!\n";
	else if (humanScore < houseScore)
		cout << "Computer wins!\n";
	else
		cout << "It was a tie.\n";
}

int dealCards(int numberOfCards, string message)
{
//This function deals the cards
//The number of cards to be dealt is provided as an argument
//A message indicating which player is receiving the cards is also
//given as an argument
//The player message and the cards dealt is displayed to the screen
//the total value of the dealt cards  is returned

	int totalValue;
	int cardValue;
	cout << message;

	srand(static_cast<int>(time(0)));
	

	for (int i = 1; i <= numberOfCards; i++)
	{
		cardValue = Random(10, 1);
		cout << cardValue << " ";
		totalValue = totalValue + cardValue;
	}

	cout << "\n";

	return totalValue;
}

void hit(int &playerScore)
{
//This function asks the human if they want another card -- 'a hit'
//the player's score total is accumulated as they take cards
//the player can continue taking cards until they wish to stop or they exceed 21
//After a card is taken (use the dealCards function) the user's current total is displayed
//If the user goes over 21 'busted' is displayed
	char hit = "y";
	char playerBusted = "n";

		while ((hit == "y") || (hit == "Y"))
		{

			if (playerBusted == "n")
			

				cout << "Do you want to hit? (y/n) ";\
				cin >> hit;

				if ((hit == "y") || (hit == "Y"))
				
					dealCards(1, "Hit: ");
				
				else
				
					cout << "\n";

			if (playerBusted == "y")

				cout << "You have busted!\n";

				hit = "n";
			
		}
}

int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
	return 1 + rand() % (upperLimit - lowerLimit + 1);
}




My 15 errors are:

1>.\carlson_Lab4_Ex3.cpp(115) : error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'
1>.\carlson_Lab4_Ex3.cpp(116) : error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'
1>.\carlson_Lab4_Ex3.cpp(118) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>.\carlson_Lab4_Ex3.cpp(118) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>.\carlson_Lab4_Ex3.cpp(118) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>.\carlson_Lab4_Ex3.cpp(118) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>.\carlson_Lab4_Ex3.cpp(121) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>.\carlson_Lab4_Ex3.cpp(121) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>.\carlson_Lab4_Ex3.cpp(127) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>.\carlson_Lab4_Ex3.cpp(127) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>.\carlson_Lab4_Ex3.cpp(127) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>.\carlson_Lab4_Ex3.cpp(127) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>.\carlson_Lab4_Ex3.cpp(135) : error C2446: '==' : no conversion from 'const char *' to 'int'
1>.\carlson_Lab4_Ex3.cpp(135) : error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
1>.\carlson_Lab4_Ex3.cpp(139) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
line 16: void hit(int &);
line 44: hit(person);

then line 115: char hit = "y";
basically you have a function called hit, and your declaring a char called hit... won't work. call it something else...

with errors, always ignore all the other errors after the first, just look at the first few lines of errors, have a look at the first line number and go to that line... in this case line 115. then try and determine why it thinks hit is a pointer to int.

errors on lines 116-139 are redundant and are simply a result of this mismatch of names, if you change the name on the line causeing the problem (115) then the other errors dissapear.
Last edited on
Now my function name is hit and the variable name is playerHit but I'm still getting the same 15 error messages
My program finally runs but there is something wrong with the hit function because it never asks me if i want to hit or not. Here is my current code:

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
#include "stdafx.h"

/* Specification:
First Name Last Name  
Lab 4 Exercise 3
This program plays a game 
of 21 with the user*/


#include <iostream>
#include <ctime>
#include <string>

using namespace std;

//prototypes...
void play21(void);
int dealCards(int, string);
void hit(int &);
void determineWinner(int, int);
int Random(int, int);


int main()
{
	srand(static_cast<int>(time(0)));
	int keepPlaying = 0; //loop control variable

	cout << "\tSimple game of 21\n\n";
	cout << "The person closest to 21 wins!\n\n";

	do 
	{
		play21();
          
		//keep playing?
		cout << "\nDo you want to play another hand?\n";
		cout << "Press 1 for Yes\n";
		cout << "Press 0 for No\n";
		cin >> keepPlaying;
	}while(keepPlaying == 1);
	return 0;
}

void play21(void)
{
	//play one hand of 21

	//randomize the cards
	

	// deal the cards
	int person = dealCards(2, "\nYour Cards: ");
	cout << "= " << person << endl;
	int house = dealCards(2, "Computers Cards: ");
	cout << "= " << house << endl;

	// Ask if human wants a hit and keep hitting...
	hit(person);
	cout << endl;

	//Determine if computer takes a hit
	while ((house < person) && (house <= 21) && (person <= 21)) 
	{
		house += dealCards(1, "The Computer takes a card: ");
		cout << "= " << house << endl;
	}

	//show who won....
	determineWinner(person, house);
}

void determineWinner(int humanScore, int houseScore) 
{
//Compare the scores to see who won
//Both the human and the house score totals are provided as arguments
//Display total scores and indicate winner
//possible outcomes: human wins, computer wins, tie
	if ((humanScore > houseScore) && (humanScore < 22))
		cout << "You win!\n";
	else if ((humanScore < houseScore) && (houseScore < 22))
		cout << "Computer wins!\n";
	else if ((humanScore > 21) && (houseScore < 22))
		cout << "You both busted! No winner!\n";
	else if (houseScore > 21)
		cout << "Computer busted! You win!\n";
	else if (humanScore > 21)
		cout << "Player busted! Computer wins!\n";
	else
		cout << "It was a tie.\n";
}

int dealCards(int numberOfCards, string message)
{
//This function deals the cards
//The number of cards to be dealt is provided as an argument
//A message indicating which player is receiving the cards is also
//given as an argument
//The player message and the cards dealt is displayed to the screen
//the total value of the dealt cards  is returned

	int totalValue;
	int cardValue;
	cout << message;

	
	

	for (int i = 1; i <= numberOfCards; i++)
	{
		cardValue = Random(10, 1);
		cout << cardValue << " ";
		totalValue = totalValue + cardValue;
	}

	return totalValue;
}

void hit(int &playerScore)
{
//This function asks the human if they want another card -- 'a hit'
//the player's score total is accumulated as they take cards
//the player can continue taking cards until they wish to stop or they exceed 21
//After a card is taken (use the dealCards function) the user's current total is displayed
//If the user goes over 21 'busted' is displayed
	char* playerHit = "y";
	

		while ((playerHit == "y") || (playerHit == "Y"))
		{

			if (playerScore <= 21)
			

				cout << "Do you want to hit? (y/n) ";\
				cin >> playerHit;

				if (playerHit == "y" || playerHit == "Y")
				
					dealCards(1, "Hit: ");
				
				else
				
					cout << "\n";

			if (playerScore >21)

				cout << "You have busted!\n";

				playerHit = "n";
			
		}
}

int Random(int lowerLimit, int upperLimit)
{
//returns a random number within the given boundary
	return 1 + rand() % (upperLimit - lowerLimit) +1;
}
Topic archived. No new replies allowed.