This has me really stumped. This is a hangman program I wrote for my C++ class. I'm trying to have it store guessed letters in "char guesses[50]" yet somehow it's pulling other variables values into there and not working at all.
You might have to run the code to see what I mean:
Much thanks to anyone who can figure this out!Code:#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <windows.h>
using namespace std;
//class for storing our words and definitions:
class word_list {
public:
char word[10];
char hint[255];
};
//declare functions:
void wordinfo(int length, char word[10]);
void printxy(int x, int y, char text[255]);
void dopicture(int num);
int location(char string[10], char character);
int main() {
int choice, chances = 0;
word_list wordlist[5];
char again;
char word[10] = "";
char guesses[50] = {""}; //********* <---
char guess;
//setup words (in uppercase) and hints:
strcpy(wordlist[0].word,"DONKEY");
strcpy(wordlist[0].hint,"This animal makes an ass of himself.");
strcpy(wordlist[1].word,"PHONE");
strcpy(wordlist[1].hint,"Ring! Ring!");
strcpy(wordlist[2].word,"CHECKBOOK");
strcpy(wordlist[2].hint,"It's easier to balance than a scale.");
strcpy(wordlist[3].word,"BOAT");
strcpy(wordlist[3].hint,"It floats. It carries. It");
strcpy(wordlist[4].word,"KEYBOARD");
strcpy(wordlist[4].hint,"Alphabetical/Numerical input device.");
do {
//setup our values:
chances = 0;
strcpy(word, "");
//strcpy(guesses, ""); //********* <---
choice = 0;
system("CLS");
//display welcome and menu:
cout << "Welcome to Hangman, Man!" << endl << endl;
cout << "Please choose a word (1) through (5): ";
cin >> choice;
//make sure acceptable value was entered:
while (choice < 1 || choice > 5) {
system("CLS");
cout << "Bogus, man! Your choice needs to be a number (1) through (5)"
<< endl << endl;
cout << "Please choose a word (1) through (5): ";
cin >> choice;
}
//start the game:
do {
system("CLS");
//Give our hints, etc:
cout << "Hangman, man!" << endl << endl;
cout << "Hint: " << wordlist[choice - 1].hint << endl;
cout << "Word: ";
wordinfo(strlen(wordlist[choice - 1].word), word);
cout << endl;
cout << "Guessed Letters: " << guesses << endl << endl; //********* <---
//dopicture(chances);
cout << "Enter guess: ";
cin >> guess;
//add letter to guessed letters (guesses)
strcpy(guesses, strupr(guesses + guess)); //********* <---
//check to see if letter exists in word
if (location(wordlist[choice - 1].word, guess) != -1) {
word[location(wordlist[choice - 1].word, guess)] = guess;
if (strcmp(word, wordlist[choice - 1].word) == 0) {
system("CLS");
cout << "You won with " << 6 - chances << " chances!";
cout << endl << endl;
break;
} else {
system("CLS");
cout << "Good Guess!" << endl << endl;
system("PAUSE");
system("CLS");
}
} else {
chances++;
if (chances == 6) {
//dopicture(chances);
cout << "Sorry, you didn't win this time..." << endl;
cout << "The word was " << wordlist[choice - 1].word << endl << endl;
} else {
system("CLS");
cout << "Whoa! Not in this word, buddy. Try again!" << endl;
cout << "You have " << 6 - chances << " left." << endl << endl;
system("PAUSE");
system("CLS");
}
}
} while (chances < 6);
cout << "Would you like to play again? (y) or (n): ";
cin >> again;
} while (again == 'y');
return 0;
}
int location(char string[10], char character) {
for (int i = 0; i < strlen(string); i++) {
if (string[i] == character) {
return i;
break;
}
}
return -1;
}
void wordinfo(int length, char word[10]) {
for (int i = 0; i < length; i++) {
if (word[i] == NULL) {
cout << " _ ";
} else {
cout << " " << word[i] << " ";
}
}
}
