School Project: Wheel of Fortune

My partner and I are having a little trouble working around a (what seems to be) minor problem.

In wheel of fortune we have a text file that contains a category and phrase (for example: school jacksonville_state_university)that are on different lines. When we read in the phrase, it includes the underscores. I included underscores because when I just did spaces, it only read in the last word. I want to print a "board" something like what hangman would print (for example for the word school,
cout "------"). If there is more than one word, I would like to cout a space for the underscores. Any help would be great. Also, if there is a different way to do this that would be more efficient, that would be great too. Here is the section of code for that part that I have come up with. I creates a compiler error saying that C++ cannot compare integers and pointers:

1
2
3
4
5
6
7
8
9
10

for(int i = 0; i < phrase_length; i++){
	char y = phrase[i];
	if(y == "_"){
	        cout << " " << endl;
		}
	if(y != "_"){
		cout << "-" << endl;
		}
in both if statements compare char y to the char constant '_' not the string literal "_"
Wow! Thanks a lot! That made it work perfectly!!!!!
Well, I've run into another small problem. Now it prints out the letter in the correct spot, but it will only show one letter at a time. For example, if the word is: dog and the user guesses "d" it will show d--. If the user guesses "g" on the next guess, it will show --g. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// COUT BLANKS FOR THE PHRASE AND REPLACE DASHES WITH LETTERS
		for(int i = 0; i < phrase_length; i++){
			char y = phrase[i];	
			
			if(y == '_'){
				cout << " ";
				}
			if(y != '_' && player_choice != y && guessed_letters[i] != y){
				cout << "-";
				}
			if(y != '_' && player_choice == y && guessed_letters[i] != y){
				cout << player_choice;
				}
				
			if(y != '_' && player_choice == y && guessed_letters[i] == y){
				cout << guessed_letters[i];
				}
			}
Instead of using cout a bunch of times, what if you made another char array, of the same size as the phrase, and initalized it with all '-' characters, then, if the user's guess matched any of the original phrase's letters, replace the copy's '-' in that spot with the correct letter? Then you can print out the copy array. Try that and post back.
BOOM! Perfect! Thank you so much! My partner and I were sooo close to doing that, but what we did is left our initializer in the while loop and it initialized every time, so it didn't look like it was updating. that way was so much shorter too. When I finish, I will post all of my code.

THANKS AGAIN davidcox95 & trojansdestroy!!
Ok here is my code. We only had to write code for one player because of time constraints and I could do more tomorrow, but I am going to bed. I still have to make more than one category, but that isn't the biggest part of it. It works pretty well though, but there could obviously be some improvements made.

So, it's not 100% finished, but it is mostly done aside from a few little tweaks here and there.

Thanks again everyone for helping us.
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
// Wheel of Fortune

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>

using namespace std;

struct Player {
	string name;
	int money;
	};
	
int main(){

	//PLAYER INFORMATION AND WELCOME
	Player one;
	one.money = 0;
	cout << "Welcome to 'Wheel of Fortune'! What is your name?: ";
	cin >> one.name;
	cout << "Hello, " << one.name << ". You start the game with $" << one.money << "." << endl;
	
	// READ FROM FILE
	ifstream infile;
	string phrase;
	string category;
	int phrase_length;
	infile.open("input.txt");
	
	while(infile){
		infile >> phrase >> category;
		}
	phrase_length = phrase.length();
		
	cout << "The category is " << category << endl << endl;
	
	
	// WHEEL
	int wheel[24];
	wheel[0] = 0;
	wheel[1] = 0; 
	wheel[2] = 800;
	wheel[3] = 350;
	wheel[4] = 450;
	wheel[5] = 700;
	wheel[6] = 300;
	wheel[7] = 600;
	wheel[8] = 5000;
	wheel[9] = 600;
	wheel[10] = 500;
	wheel[11] = 300;
	wheel[12] = 500;
	wheel[13] = 800;
	wheel[14] = 550;
	wheel[15] = 400;
	wheel[16] = 300;
	wheel[17] = 900;
	wheel[18] = 500;
	wheel[19] = 300;
	wheel[20] = 900;
	wheel[21] = 600;
	wheel[22] = 400;
	wheel[23] = 300;
	
	int player_menu;
	char player_choice;
	bool spin_wheel;
	long seed = time(NULL); // gets time
	srand(seed);
	int spin;
	bool playing = true;
	int player_spins = 0;
	string player_solve;
	bool player_win = false;
	
	char copy_phrase[phrase_length];
		for(int i = 0; i < phrase_length; i++){
			copy_phrase[i] = '-';
			}
			
	while(playing == true && player_win == false){
	
	if(player_spins < 1){ // Check to see if the player has spun the wheel yet.
		cout << "Spin the wheel to start..." << endl;
		player_menu = 2;
		char copy_phrase[phrase_length];
		for(int i = 0; i < phrase_length; i++){
			copy_phrase[i] = '-';
			}
		}
	else{
		cout << "\n\nWhat would you like to do?: " << endl << "1. Guess a letter" << endl << "2. Spin the wheel" << endl << "3. Buy a vowel (- $250)" << endl << "4. Solve the puzzle" << endl;
		cin >> player_menu;
		}
		player_choice = '*';
		// EVALUATE THE PLAYERS MENU CHOICE
		if(player_menu == 1){
			cout << "Guess a consonant: ";
			cin >> player_choice;
			
			}
		if(player_menu == 2){
			player_spins++;
			// RANDOM NUMBER GENERATOR
			spin = rand() % 25;
			cout << endl << "The wheel landed on: $" << wheel[spin] << "." << endl;
			if(spin == 0 || spin == 1){
				one.money == 0;
				cout << "OH NO! Bankrupt!" << endl;
				}
			}
		if(player_menu == 3){
			one.money -= 250;
			cout << "Here is your money now: $" << one.money << ". " << "Guess a vowel: ";
			cin >> player_choice;
			}
		if(player_menu == 4){
			cout << "You want to solve (no caps please and use underscores for spaces):"; // Underscores because of time constraints.
			cin >> player_solve;
			if(player_solve == phrase){
				cout << "You win!" << endl;
				player_win = true;
				playing = false;
				}
			}
			
		for(int i = 0; i < phrase_length; i++){
			if(phrase[i] == '_'){
				copy_phrase[i] = ' ';
				}
			if(phrase[i] == player_choice){
				copy_phrase[i] = player_choice;
				one.money += wheel[spin];
				cout << "Your money: $" << one.money << endl;
				}				
			}
		cout << endl;
		if (player_win == false){
			for(int i = 0; i < phrase_length; i++){
				cout << copy_phrase[i];
				}
			}
		}
	if(player_win == true){
		cout << "Thanks for playing Wheel of Fortune!" << endl << "Your earnings today were $" << one.money << " in cash and prizes." << endl;
		}
	return 0;
	}
	
Topic archived. No new replies allowed.