Right now I am trying to get my scorecard to work for hangman. This is a game I can play as many times as I want and I want it to show my lowest score when I press 2 at the game menu. For some reason my logic isn't working. Can anyone help? I have attached the 3 sections of code necessary for this to work.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
int maxAttempts = 10; //max attempts possible
void poorStiff(int);
int wordFill(char, string, string&);
int main()
{
int intro;
int bodyPart = 0;
ifstream wordIn; //file stream read in
wordIn.open("WordList.txt"); //list of words used in this game
//if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); }
char letter; //letter guessed
int numWrongGuesses = 0; //counts the number of wrong guesses
string theWord;
string words[13];
int play = 0;
bool run = true;
int makeYourSelection = 0;
int bestScore = 11;
while (run == true)
{
int attemptsGame = 0;
// put a game menu to loop that asks if you want to keep playing or exit and go to scorecard
cout << " WELCOME TO THE GAME OF HANGMAN\n\n\n\n";
cout << " Press 1 to play the game, press 2 to exit to the scorecard \n\n";
cout << " You have 10 attempts to guess the words before you get hung \n\n";
cin >> play;
while (play == 1)//loops while user has entered 1, 0 exits the game
{
for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant
{
wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file
cout << words[i] << endl;
}
srand(time(NULL));//to get a random word
int n = rand() % 12;
theWord = words[n]; //pulls word from file
wordIn.close();
string mystery(theWord.length(), '*'); //replaces word letters with asterisks
while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses
{
cout << mystery << endl << endl;
cout << "You now have the length of the word represented by the *'s. \n\n";
cout << "Guess a letter \n\n";
cin >> letter;
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
bodyPart++;
poorStiff(bodyPart);
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
numWrongGuesses++;
attemptsGame++;
}
else
{
for (int i = 0; i < mystery.length(); i++)
{
if (theWord[i] == letter)
{
mystery[i] = letter;
}
}
cout << "You have found one of the letters. Congratulations! \n\n";
cout << "You have: " << maxAttempts - numWrongGuesses;
cout << " guesses left \n\n" << endl;
}
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
}
}
if (numWrongGuesses == maxAttempts) //when you run out of guesses
{
cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n";
cout << "The word you were trying to guess was " << theWord << endl;
poorStiff(bodyPart);
}
cin.ignore();
cin.get();
break;
}
while (play == 2)
{
cout << "Best Scores: \n\n";
cout << bestScore << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained
{
int i;
int hits = 0; //letter hits within the word
int many = theWordSecret.length();
for (i = 0; i < many; i++)
{
if (guess == guessWord[i])
return 0;
if (guess == theWordSecret[i])
{
guessWord[i] == guess;
hits++;
}
}
return hits;
}
void poorStiff(int bodyPart)
{
if (bodyPart == 1)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 2)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 3)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 4)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / " << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 5)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 6)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 7)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 8)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 9){
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / " << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 10)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / \." << endl;
cout << "_______________" << endl;
}
}
Did you try debugging? If you step through your code you will immediately find the problem, which is here:
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break; // problem here
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
// break should be here
}
You're breaking out of your loop before your checking logic. So move your break statement to after the check, because that code never gets executed.
For a start enable your compiler warnings, you'll get many from a quick look at your code.
Change this
guessWord[i] == guess;
to this
guessWord[i] = guess;
At this point
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
...
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
}
else
{
...
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
...
}
}
Here, when you are entering the first if, then you will enter the second if too, which doesn't make sense. That happens because theWord and mystery are both empty strings!
Also notice that break should be after this part of code:
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
because as it is, this part of code will never be executed.
I suggest taking at this answer (not relevant with your logical error)
System(“pause”); - Why is it wrong?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Link to Code: https://onlinegdb.com/B1DsFDa8D
Hello, my while loops aren't responding to their set conditions once the value is updated. the code just keeps going until it reaches the end. If you run the code you'll see what I'm talking about(in fact it is necessary that you do so). When the value of the parameter is updated the value does add up and changes. When I print out the value of it at the end of the code it does in fact register as the number that should be assigned to it throughout the loop but the loop just doesn't do anything when it should be stopped. Basically my problem might be an infinite loop. the only thing that counters it is a "return 0;" at the end of the loop.
My professor gave specific instructions: Create a hangman game. The game usually involves one player guessing letters to a secret word. Bad guesses cause the picture of a hangman to be drawn one segment at a time. Once there are 7 bad guesses, the hangman picture has been drawn and the player guessing loses the game. In your game, seven bad answers to any of 16 multiple choice questions will result in a lost game.
Program 1 Writes 16 questions to a file called “infile.txt”
Program 2 reads “infile.txt” and uses the questions for the hangman game
Use a boolean value-returning function called "is_hung". This function takes an integer parameter called "num_errors". This parameter is tested using an "if-else" statement to determine how much of the hangman to display to the screen based on the number of wrong answers.
The entire hangman can be displayed by the code segment below:
{
cout << "\t \t \t" << " O " << endl;
cout << "\t \t \t" << "/|\\" << endl;
cout << "\t \t \t" << " | " << endl;
cout << "\t \t \t" << "/ \\" << endl;
cout << " YOU ARE HUNG" endl;
return false;
}
1 incorrect answer displays the head
2 incorrect answers displays the left arm
3 incorrect answers displays the right arm
4 incorrect answers displays the top half of the body
5 incorrect answers displays the bottom half of the body
6 incorrect answers displays the left leg
7 incorrect answers displays the right leg
My Code:
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool is_hung(int, bool);
int main()
{
string question1;
string answer1;
string answer2;
string answer3;
string answer4;
string question2;
string answer5;
string answer6;
string answer7;
string answer8;
string question3;
string answer9;
string answer10;
string answer11;
string answer12;
string question4;
string answer13;
string answer14;
string answer15;
string answer16;
string question5;
string answer17;
string answer18;
string answer19;
string answer20;
string question6;
string answer21;
string answer22;
string answer23;
string answer24;
string question7;
string answer25;
string answer26;
string answer27;
string answer28;
string question8;
string answer29;
string answer30;
string answer31;
string answer32;
ifstream reader;
reader.open("infile.txt");
int num_right = 0;
int num_error = 0;
bool power = true;
cout << power << endl;
string user_answer1 = "";
string user_answer2 = "";
string user_answer3 = "";
string user_answer4 = "";
string user_answer5 = "";
string user_answer6 = "";
string user_answer7 = "";
string user_answer8 = "";
while (power == true) {
while (num_error < 7) //should stop the while loop when number of incorrect answers are equal to 7
{
//1
getline(reader, question1); //get line from infile.txt
getline(reader, answer1);
getline(reader, answer2);
getline(reader, answer3);
getline(reader, answer4);
cout << question1 << endl; //print out line from file
cout << answer1 << endl;
cout << answer2 << endl;
cout << answer3 << endl;
cout << answer4 << endl;
cout << "Enter Answer: ";
cin >> user_answer1; //have the user input answer
if (user_answer1 == "B") //if statement to determine correct answer
{
cout << "correct" << endl;
num_right++; //adds 1 to num_right
cout << "" << endl;
}
else //if user_answer is not equal answer/wrong answer
{
cout << "incorrect" << endl;
num_error++; //add 1 to num_error
power = is_hung(num_error, power); //get function
}
//2
getline(reader, question2);
getline(reader, answer5);
getline(reader, answer6);
getline(reader, answer7);
getline(reader, answer8);
cout << question2 << endl;
cout << answer5 << endl;
cout << answer6 << endl;
cout << answer7 << endl;
cout << answer8 << endl;
cout << "Enter Answer: ";
cin >> user_answer2;
if (user_answer2 == "B") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//3
getline(reader, question3);
getline(reader, answer9);
getline(reader, answer10);
getline(reader, answer11);
getline(reader, answer12);
cout << question3 << endl;
cout << answer9 << endl;
cout << answer10 << endl;
cout << answer11 << endl;
cout << answer12 << endl;
cout << "Enter Answer: ";
cin >> user_answer3;
if (user_answer3 == "C") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//4
getline(reader, question4);
getline(reader, answer13);
getline(reader, answer14);
getline(reader, answer15);
getline(reader, answer16);
cout << question4 << endl;
cout << answer13 << endl;
cout << answer14 << endl;
cout << answer15 << endl;
cout << answer16 << endl;
cout << "Enter Answer: ";
cin >> user_answer4;
if (user_answer4 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//5
getline(reader, question5);
getline(reader, answer17);
getline(reader, answer18);
getline(reader, answer19);
getline(reader, answer20);
cout << question5 << endl;
cout << answer17 << endl;
cout << answer18 << endl;
cout << answer19 << endl;
cout << answer20 << endl;
cout << "Enter Answer: ";
cin >> user_answer5;
if (user_answer5 == "A") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//6
getline(reader, question6);
getline(reader, answer21);
getline(reader, answer22);
getline(reader, answer23);
getline(reader, answer24);
cout << question6 << endl;
cout << answer21 << endl;
cout << answer22 << endl;
cout << answer23 << endl;
cout << answer24 << endl;
cout << "Enter Answer: ";
cin >> user_answer6;
if (user_answer6 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
//7
getline(reader, question7);
getline(reader, answer25);
getline(reader, answer26);
getline(reader, answer27);
getline(reader, answer28);
cout << question7 << endl;
cout << answer25 << endl;
cout << answer26 << endl;
cout << answer27 << endl;
cout << answer28 << endl;
cout << "Enter Answer: ";
cin >> user_answer7;
if (user_answer7 == "A") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
cout << power << endl;
//8
getline(reader, question8);
getline(reader, answer29);
getline(reader, answer30);
getline(reader, answer31);
getline(reader, answer32);
cout << question8 << endl;
cout << answer29 << endl;
cout << answer30 << endl;
cout << answer31 << endl;
cout << answer32 << endl;
cout << "Enter Answer: ";
cin >> user_answer8;
if (user_answer8 == "D") {
cout << "correct" << endl;
num_right++;
cout << "" << endl;
}
else {
cout << "incorrect" << endl;
num_error++;
power = is_hung(num_error, power);
}
cout << "" << endl;
if (num_error > 7) {
cout << "You got " << num_right << " out of 8. You lose." << endl;
}
else if (num_error < 7) {
cout << "You got " << num_right << " out of 8. You Win." << endl;
}
reader.close();
return 0;
}
}
reader.close();
cout << "" << endl;
if (num_error > 7) {
cout << "You got " << num_right << " out of 16. You lose." << endl;
}
else if (num_error < 7) {
cout << "You got " << num_right << " out of 16. You Win." << endl;
}
return 0;
}
bool is_hung(int num_errors2, bool power2)
{
if (num_errors2 == 1) {
cout << "\t \t \t"
<< " O " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 2) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 3) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/ \\" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 4) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 5) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 6) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "\t \t \t"
<< "/ " << endl;
cout << "" << endl;
power2 = true;
return power2;
}
else if (num_errors2 == 7) {
cout << "\t \t \t"
<< " O " << endl;
cout << "\t \t \t"
<< "/|\\" << endl;
cout << "\t \t \t"
<< " | " << endl;
cout << "\t \t \t"
<< "/ \\" << endl;
cout << " YOU ARE HUNG" << endl;
cout << "" << endl;
power2 = false;
return power2;
}
}
There is a ridiculous amount of wasteful, repetitive code, which makes it extremely difficult to read and debug, and significantly increases the risk of making mistakes.
This code can be greatly reduced by making use of fewer variables, better loops, and a little arithmetic, eg:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool is_hung(int);
int main()
{
string question;
string answer;
string user_answer;
const string correct_answers = “BBCDADADBDABCABB”;
int num_right = 0;
int num_error = 0;
ifstream reader("infile.txt");
for(int i = 0; i < 16; ++i)
{
getline(reader, question); // get line from infile.txt
cout << question << endl; // print out line from file
for (int j = 0; j < 4; ++j)
{
getline(reader, answer);
cout << answer << endl;
}
cout << "Enter Answer: ";
cin >> user_answer; // have the user input answer
if (user_answer == correct_answers[i]) // if statement to determine correct answer
{
cout << "correct" << endl;
num_right++; // adds 1 to num_right
cout << endl;
}
else // if user_answer is not equal answer/wrong answer
{
cout << "incorrect" << endl;
num_error++; // add 1 to num_error
if (is_hung(num_error)) // get function
break;
}
}
reader.close();
cout << endl;
if (num_error == 7)
{
cout << "You got " << num_right << " out of 16. You lose." << endl;
}
else
{
cout << "You got " << num_right << " out of 16. You Win." << endl;
}
return 0;
}
bool is_hung(int num_errors)
{
cout << "\t \t \t" << " O ";
if (num_errors >= 2)
{
cout << endl;
cout << "\t \t \t" << "/";
}
if (num_errors >= 3)
{
cout << (num_errors >= 4 ? ‘|’ : ‘ ‘) << “\\”;
}
if (num_errors >= 5)
{
cout << endl;
cout << "\t \t \t" << " | ";
}
if (num_errors >= 6)
{
cout << endl;
cout << "\t \t \t" << "/";
}
if (num_errors >= 7)
{
cout << " \\" << endl;
cout << " YOU ARE HUNG";
}
cout << endl;
return (num_errors < 7);
}
Don’t you think that is much easier to work with?
I am writing in C++ and the errors I am having are:
E0120 return value type does not match the function type
C4700 uninitialized local variable 'name' used
C2562 interact 'void function returning a value
And a warning error of C4447.
My code is down below:
#include <limits>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
void readFile(char sudokuBoard[][9]);
void writeFile(char sudokuBoard[][9]);
void display(char sudokuBoard[][9]);
void interact();
void getOption(char sudokuBoard[][9]);
void editSquare(char sudokuBoard[][9]);
void showValues();
void openingMessage();
void openingQuestion();
//opening message to the game and pause
void openingMessage()
{
std::cout << "Welcome to Sudoku"
<< endl << "Press any key to continue.";
cin.ignore();
}
//Questions to determine whether the user will play the game
void openingQuestion()
{
char response; // users input to the question below
cout << "Would you like to play Sudoku ? " << endl << "1 for yes, 2 for no";
cin >> response;
if (response == 1) //if there answer is yes
{
//return main();
}
else if (response == 2) //if answer is no
{
cout << "are you sure you do not want to play ?" << endl << "1 for yes and 2 for no";
//if user answers no
char eResponse; //response to the next question
if (eResponse == 2) // 2 equals no
{
return main();
}
// if user answers yes
if (eResponse == 1) // 1 equals yes
{
//return exit();
}
}
}
//Makes other functions function
int main()
{
//Declaring array
char sudokuBoard[9][9];
//calling the other functions
readFile(sudokuBoard);
interact();
display(sudokuBoard);
return 0;
}
//Asks the user for a fulename to read a gamebaord
//in from that file name and then places it in an array
void readFile(char sudokuBoard[][9])
{
//Declare filename
char sourceFile[256];
//Declaring file input
ifstream fin;
//Getting the filename from the user
cout << "Where is your board located? ";
cin >> sourceFile;
//Open file error checking
fin.open(sourceFile);
if (fin.fail())
{
cout << "Input file opening failed. " << endl;
exit(1);
}
//Read file into array
for (int col = 0; col < 9; col++)
{
for (int row = 0; row < 9; row++)
{
fin >> sudokuBoard[row][col];
if (sudokuBoard[row][col] == '0')
{
sudokuBoard[row][col] = ' ';
}
}
}
//close the file
fin.close();
}
//displays the result to the screen
void display(char sudokuBoard[][9])
{
//Declare variables
char option;
//Display Column Title
cout << " A| B| C| D| E| F| G| H| I" << endl;
//Row 1
cout << "1 "
<< sudokuBoard[0][0]
<< " " << sudokuBoard[1][0]
<< " " << sudokuBoard[2][0]
<< "|"
<< sudokuBoard[3][0]
<< " " << sudokuBoard[4][0]
<< " " << sudokuBoard[5][0]
<< "|"
<< sudokuBoard[6][0]
<< " " << sudokuBoard[7][0]
<< " " << sudokuBoard[8][0]
<< endl;
//Row 2
cout << "2 "
<< sudokuBoard[0][0]
<< " " << sudokuBoard[1][1]
<< " " << sudokuBoard[2][1]
<< "|"
<< sudokuBoard[3][1]
<< " " << sudokuBoard[4][1]
<< " " << sudokuBoard[5][1]
<< "|"
<< sudokuBoard[6][1]
<< " " << sudokuBoard[7][1]
<< " " << sudokuBoard[8][1]
<< endl;
//Row 3
cout << "3 "
<< sudokuBoard[0][2]
<< " " << sudokuBoard[1][2]
<< " " << sudokuBoard[2][2]
<< "|"
<< sudokuBoard[3][2]
<< " " << sudokuBoard[4][2]
<< " " << sudokuBoard[5][2]
<< "|"
<< sudokuBoard[6][2]
<< " " << sudokuBoard[7][2]
<< " " << sudokuBoard[8][2]
<< endl;
//Separator
cout << " -----_-----_-----" << endl;
//Row 4
cout << "4 "
<< sudokuBoard[0][3]
<< " " << sudokuBoard[1][3]
<< " " << sudokuBoard[2][3]
<< "|"
<< sudokuBoard[3][3]
<< " " << sudokuBoard[4][3]
<< " " << sudokuBoard[5][3]
<< "|"
<< sudokuBoard[6][3]
<< " " << sudokuBoard[7][3]
<< " " << sudokuBoard[8][3]
<< endl;
//Row 5
cout << "5 "
<< sudokuBoard[0][4]
<< " " << sudokuBoard[1][4]
<< " " << sudokuBoard[2][4]
<< "|"
<< sudokuBoard[3][4]
<< " " << sudokuBoard[4][4]
<< " " << sudokuBoard[5][4]
<< "|"
<< sudokuBoard[6][4]
<< " " << sudokuBoard[7][4]
<< " " << sudokuBoard[8][4]
<< endl;
//Row 6
cout << "6 "
<< sudokuBoard[0][5]
<< " " << sudokuBoard[1][5]
<< " " << sudokuBoard[2][5]
<< "|"
<< sudokuBoard[3][5]
<< " " << sudokuBoard[4][5]
<< " " << sudokuBoard[5][5]
<< "|"
<< sudokuBoard[6][5]
<< " " << sudokuBoard[7][5]
<< " " << sudokuBoard[8][5]
<< endl;
//Separator
cout << " -----_-----_-----" << endl;
//Row 7
cout << "7 "
<< sudokuBoard[0][6]
<< " " << sudokuBoard[1][6]
<< " " << sudokuBoard[2][6]
<< "|"
<< sudokuBoard[3][6]
<< " " << sudokuBoard[4][6]
<< " " << sudokuBoard[5][6]
<< "|"
<< sudokuBoard[6][6]
<< " " << sudokuBoard[7][6]
<< " " << sudokuBoard[8][6]
<< endl;
//Row 8
cout << "8 "
<< sudokuBoard[0][7]
<< " " << sudokuBoard[1][7]
<< " " << sudokuBoard[2][7]
<< "|"
<< sudokuBoard[3][7]
<< " " << sudokuBoard[4][7]
<< " " << sudokuBoard[5][7]
<< "|"
<< sudokuBoard[6][7]
<< " " << sudokuBoard[7][7]
<< " " << sudokuBoard[8][7]
<< endl;
cout << "9 "
<< sudokuBoard[0][8]
<< " " << sudokuBoard[1][8]
<< " " << sudokuBoard[2][8]
<< "|"
<< sudokuBoard[3][8]
<< " " << sudokuBoard[4][8]
<< " " << sudokuBoard[5][8]
<< "|"
<< sudokuBoard[6][8]
<< " " << sudokuBoard[7][8]
<< " " << sudokuBoard[8][8]
<< endl
<< endl;
getOption(sudokuBoard);
}
//Allows the user to interact and manipulate the game board
void interact()
{
cout << "Options:" << endl
<< " ? Show these instructions" << endl
<< " D Display the board" << endl
<< " X Edit one square" << endl
<< " H Help show the possible values for one of the squares"
<< endl
<< " Q Save and Quit" << endl
<< endl;
return getOption;
}
//gets the user's input
void getOption(char sudokuBoard[][9])
{
char option;
cout << "> ";
cin >> option;
if (option == 'x' || option == 'X')
editSquare(sudokuBoard);
else if (option == '?')
interact();
else if (option == 'd' || option == 'D')
display(sudokuBoard);
else if (option == 'h' || option == 'H')
showValues();
else if (option == 'q' || option == 'Q')
writeFile(sudokuBoard);
else
cout << "ERROR: Invalid command";
return;
}
//edits one square with coordinates entered by the user
void editSquare(char sudokuBoard[][9])
{
//Declare variables
char letter;
int number;
int value = 0;
//Get letter and number coordinates
cout << "What are the coordinates of the square: ";
cin >> letter >> number;
//Converts letter to uppercase
letter = toupper(letter);
//if square is full, display "read only" message
if (sudokuBoard[letter - 65][number - 1] != ' ')
{
cout << "ERROR: square \'" << letter
<< number << "\' is a read-only" << endl;
getOption(sudokuBoard);
}
else
{
//get value to place in the coordinates
cout << "what is the value at \'" << letter
<< number << "\': ";
cin >> value;
//makes sure value is within the right range
if (value < 1 || value > 9)
{
cout << "Try Again: Value |'" << value << "| in square |'"
<< letter << number << "|' is invalud" << endl;
cout << endl;
getOption(sudokuBoard);
}
// Check for duplicate in column
for (int row = 0; row < 9; ++row)
if (sudokuBoard[row][number - 1] == value)
{
cout << "ERROR: square \'" << letter
<< number << "\' you typed a duplicate number" << endl
<< "Please try again" << endl;
}
// Check for duplicate in row
for (int col = 0; col < 9; ++col)
if (sudokuBoard[letter - 65][col] == value)
{
cout << "ERROR: square \'" << letter
<< number << "\' you typed a duplicate number" << endl
<< "Please try again" << endl;
}
cout << endl;
sudokuBoard[letter - 65][number - 1] = value;
getOption(sudokuBoard);
}
return;
}
//writes the content of the board to a file to be picked up later
void writeFile(char sudokuBoard[][9])
{
//File output
ofstream fout;
char destinationFile[256];
//user input
cout << "What file would you like to write your board to: ";
cin >> destinationFile;
//Open destination file & error checking
fout.open(destinationFile);
if (fout.fail())
{
cout << "Output file opening failed" << endl;
exit(1);
}
else
cout << "Board written successfully";
//Write board to file
for (int col = 0; col < 9; col++)
{
for (int row = 0; row < 9; row++)
{
if (sudokuBoard[row][col] == ' ')
{
sudokuBoard[row][col] = '0';
}
fout << sudokuBoard[row][col];
//Makes sure it's a 9x9 grid
if (row % 9 == 0)
{
fout << endl;
}
}
}
//close file
fout.close();
}
//Show all the possible values for a given coordinates
void showValues()
{
//variables
char letter;
int number;
//letter/number coordinates
cout << "What are the coordinates of the square: ";
cin >> letter >> number;
//Coinverts letter to uppercase
letter = toupper(letter);
return;
}
I am using Visual Studio 2019 for this.
A couple notes to point out
first of all, you can not return the main function
in your line
if (eResponse == 2) // 2 equals no
{
return main();
}
next
you have a lot of void function with return statements
because the function is void you can not return anything
instead of
void getOption(char sudokuBoard[][9])
{
char option;
cout << "> ";
cin >> option;
if (option == 'x' || option == 'X')
editSquare(sudokuBoard);
else if (option == '?')
interact();
else if (option == 'd' || option == 'D')
display(sudokuBoard);
else if (option == 'h' || option == 'H')
showValues();
else if (option == 'q' || option == 'Q')
writeFile(sudokuBoard);
else
cout << "ERROR: Invalid command";
return;
}
use
char getOption(char sudokuBoard[][9])
{
char option;
cout << "> ";
cin >> option;
if (option == 'x' || option == 'X')
editSquare(sudokuBoard);
else if (option == '?')
interact();
else if (option == 'd' || option == 'D')
display(sudokuBoard);
else if (option == 'h' || option == 'H')
showValues();
else if (option == 'q' || option == 'Q')
writeFile(sudokuBoard);
else
cout << "ERROR: Invalid command";
return option;
}
go through your functions and ask yourself, do i need to return a value? if yes then change from void to what ever type you are using and if no then remove the return statement.
for your warning issue
char sudokuBoard[9][9];
is not being initialised.
you can do something like
char sudokuBoard[9][9] = {};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi so i have started c++ 3 days ago, read some tutorials etc. I wanted to make my own hangman game since it seemed as an easy task for a begginer but i stumbled upon an issue. Everything is working well ecept i cant seem to find a way to make 1 letter strings swap with underscores until underscores change to the missing word. So basically when you compile it you can guess the whole word only.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess;
int wrong=0;
int main (){
string copy = word;
cout << "----------------------Hello! Welcome to the HANGMAN game!---------- ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;
cin >> word;
//space
for (int x=0; x<30; x++){
cout << endl;
}
//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('_');}
cout << underscore << endl;
//MAIN WHILE
while(wrong<12){
cin >> guess;
//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << usedguess << endl;
wrong ++;
}
}
if(underscore == word){
cout << "You win!" << endl;
break;
}
if(wrong==1){
cout << "I" << endl;
}
else if(wrong==2){
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==3){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==4){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==5){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==6){
cout << "I===" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==7){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==8){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I |" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==9){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==10){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==11){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I /" << endl;
cout << "I" << endl;
}
else if(wrong==12){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I / /"<< endl;
cout << "I YOU ARE DEAD" << endl;
cout << "Game over bro! The word was: " << word <<endl;
break;
}
}
}
To compare the strings of word and guess, you can iterate over the characters in a for-loop, and check if there is a match
string word = "hangman";
string guess = "mansomething";
string underscore = string(word.size(), '_'); // init a string with underscores equal to the length of 'word'
// iterate over the characters in word and guess
for (size_t i = 0, iend = min(word.size(), guess.size()); i < iend; i++) {
if (word[i] == guess[i])
underscore[i] = word[i]; // if the characters match at position i, update the underscore.
}
cout << underscore << endl;
Afterwards, underscore contains the following
_an____
I am trying to do a hangman project, but my code isn't working. Whenever I put in the proper letter, the code tells me it is wrong (even though it is right). Not really sure why - the code worked at some point but I changed some things and now I don't know why it doesn't work. So it is probably a simple fix, but I am just not seeing it.
Any help would be very appreciated!
#include <iostream>
using namespace std;
int letterFill (char, string, string&);
int main()
{
string name;
int maxAttempts = 5;
int wrongGuesses;
char letter;
srand(time(0));
const string wordList[15] = { "hanukkah", "sparklers", "mistletoe", "menorah", "presents", "reindeer",
"kwanzaa", "snowman", "eggnog", "celebration", "yuletide", "resolution", "nutcracker", "ornaments", "gingerbread" };
string correctWord = wordList[rand() % 15];
string unknown(correctWord.length(),'*');
cout << correctWord << endl;
cout << "Welcome to a fun game of winter holiday hangman! What is your name? " << endl;
cin >> name;
cout << name <<", there are some simple things you should know about this game before you start playing!" << endl;
cout << "You will be trying to guess a randomly selected word by typing in ONE letter at a time " << endl;
cout << "You will have " << maxAttempts << " tries before losing the game " << endl;
cout << "And remember, all of the words are winter holiday related. Good luck " << name <<"!" << endl;
cout << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" <<endl;
while (wrongGuesses == 0)
{
cout << "Guess a letter" << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 1)
{
cout << "You have 4 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 2)
{
cout << "You have 3 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 3)
{
cout << "You have 2 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 4)
{
cout << "You have 1 guess left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 5)
{
cout << "Sorry " << name << " you have made 5 wrong guesses!" << endl;
cout << "Game over. Click any key to exit. Play again soon :) " << endl;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
system("pause");
return 0;
}
int letterFill (char guessLetter, string mysteryWord, string& guessWord)
{
int x;
int matches=0;
int lengthWord=mysteryWord.length();
for (x = 0; x< lengthWord; x++)
{
if (guessLetter == mysteryWord[x])
return 0;
if (guessLetter == mysteryWord[x])
{
guessWord[x] = guessLetter;
matches++;
}
}
return matches;
}
You aren't updating the string guessWord in your int letterFill() function. As soon as you see a letter that matches you return without entering that second if statement.
I assume what you want is only to return after fully updating the guessWord, based on that what you want to do is iterate through the string, updating guessWord as you find matches and after your loop do a check
if(matches == 0) return 0;
else return matches;
I have started with C++ and I am in the middle of creating a hangman game, My code worked fine up until I chose to make three different levels of difficulty, My game asks the user for the difficulty level they would like to play, then instead of actually playing the game, it skips straight to the end where it says the user has guessed the word correctly. Any help appreciated!
The code is as follows :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
void ClearScreen();
void DisplayMan0();
void DisplayMan1();
void DisplayMan2();
void DisplayMan3();
void DisplayMan4();
void DisplayMan5();
void DisplayMan6();
void DisplayMan7();
int main()
{
const int MAX_WRONG = 7; // incorrect guesses allowed
void (*pfnaDisplayMan[])() = {DisplayMan0, DisplayMan1, DisplayMan2, DisplayMan3, DisplayMan4, DisplayMan5, DisplayMan6, DisplayMan7};
vector<string> words; // Level 1
words.push_back("GREEN");
words.push_back("BANANA");
words.push_back("LAPTOP");
words.push_back("GIRAFFE");
words.push_back("PENCIL");
vector<string> wordsD1; // Level 2
wordsD1.push_back("DELICIOUS");
wordsD1.push_back("COMPUTING");
wordsD1.push_back("SOFTWARE");
wordsD1.push_back("HARDWARE");
wordsD1.push_back("TELEPHONE");
vector<string> wordsD2; // Level 3
wordsD2.push_back("BAMBOOZLED");
wordsD2.push_back("DAYDREAMER");
wordsD2.push_back("CANNIBALISM");
wordsD2.push_back("NERVOUSLY");
wordsD2.push_back("APPROACHING");
srand((unsigned int)time(0));
string THE_WORD;
string soFar;
int wordLength;
string used; // letters already guessed
cout << "\t\t HANGMAN\n";
cout << "Please enter a difficulty level [1-3] ";
int dif = 0;
while(dif < 1 || dif > 3)
{
cin >> dif;
}
cout << "You have chosen difficulty level : "<< dif << endl;
if(dif == 1)
{
random_shuffle(words.begin(), words.end());
int incorrectGuesses = 0; // number of incorrect guesses
string const THE_WORD = words[0]; // word to guess
string soFar(THE_WORD.size(), '*'); // word guessed so far
// count length of randomly chosen string and display it
wordLength = THE_WORD.length();
}
if(dif == 2)
{
random_shuffle(wordsD1.begin(), wordsD1.end());
int incorrectGuesses = 0; // number of incorrect guesses
string const THE_WORD = wordsD1[0];
string soFar(THE_WORD.size(), '*');
wordLength = THE_WORD.length();
}
if(dif == 3)
{
random_shuffle(wordsD2.begin(), wordsD2.end());
int incorrectGuesses = 0; // number of incorrect guesses
string const THE_WORD = wordsD2[0];
string soFar(THE_WORD.size(), '*');
wordLength = THE_WORD.length();
}
// main loop
while ((incorrectGuesses < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n- There are : "<< wordLength <<" letters in the word :\t" << soFar << endl;
cout << "\n- You have guessed " <<incorrectGuesses << " times wrong out of "<< MAX_WRONG << " allowed wrong guesses.\n";
cout << "\nLetters used : " << used << endl;
cout << "=====================================================";
char guess;
cout << "\n\t\tEnter a letter : ";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word in uppercase
while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed the letter " << guess << endl;
cout << "Enter another letter / word: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << "=====================================================\n";
cout << "- Correct, The letter " << guess << " is in the word.\n";
// update soFar to include newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++incorrectGuesses;
pfnaDisplayMan[incorrectGuesses]();
}
}
// shut down
if (incorrectGuesses == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";
cout << "\nThe word was " << THE_WORD << endl;
return 0;
}
void DisplayMan0()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan1()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan2()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan3()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan4()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan5()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan6()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| / \\" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan7()
{
using namespace std;
cout << "\t\t_______" << endl;
cout << "\t\t|DONT" << endl;
cout << "\t\t|HANG" << endl;
cout << "\t\t|THE" << endl;
cout << "\t\t|MAN" << endl;
cout << "\t\t| O" << endl;
cout << "\t\t| _______ /XL" << endl;
cout << "\t\t__|_____| / \\" << endl;
}
Put incorrectGuesses out of those scopes. Because out of those scopes this variable is not declared.
if(dif == 1)
{
int incorrectGuesses = 0;
...
}
if(dif == 2)
{
int incorrectGuesses = 0;
...
}
if(dif == 3)
{
int incorrectGuesses = 0;
...
}
Should be
int incorrectGuesses = 0;
if(dif == 1)
{
...
}
if(dif == 2)
{
...
}
if(dif == 3)
{
...
}
Same issues for soFar, THE_WORD and wordLength. That part of code should be like this:
string THE_WORD;
string soFar;
int wordLength;
string used;
// cout ... cin ....
int incorrectGuesses = 0;
if(dif == 1)
{
random_shuffle(words.begin(), words.end());
THE_WORD = words[0]; // word to guess
wordLength = THE_WORD.length();
}
if(dif == 2)
{
random_shuffle(wordsD1.begin(), wordsD1.end());
THE_WORD = wordsD1[0];
wordLength = THE_WORD.length();
}
if(dif == 3)
{
random_shuffle(wordsD2.begin(), wordsD2.end());
THE_WORD = wordsD2[0];
wordLength = THE_WORD.length();
}
soFar.assign(THE_WORD.size(), '*');
M M. is correct. Your redeclaring the variables.
Just a small remark. I would use a Switch Case instead of a set of if statements. Changing:
if(dif==1){}
if(dif==2){}
if(dif==3){}
into
switch(dif){
case(1):
break;
case(2):
break;
case(3):
break;
}
Not for necessarily for readability but more to indicate that the value of dif isn't edited depending upon its value. For example:
Option 1:
dif = 1;
if(dif==1){ dif = 3; }
if(dif==2){}
if(dif==3){ dif = 7; }
Versus:
Option 2
dif = 1;
switch(dif){
case(1):
dif = 3;
break;
case(2):
break;
case(3):
dif = 7;
break;
}
Option 1 output: 7
Option 2 output: 3
You declare incorrectGuesses out of scope. It is NEVER declared or assigned a value. Declare it at the beginning of your function and assign it value in the other scopes.