C++ weirdness

Hello everyone, I'm making a game of hangman. I got the original code from err0r1212, and wanted to build upon it and change some things. However, I'm not familiar with the bool function. I'd like to remove the "play again" function, but if I delete it errors come up of course. I'm really new to c++, but I know the basics. If someone could help me remove the play again function, I'd really appreciate it :)

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>

using namespace std;

string THE_WORD; // word to guess
int wrong;
string soFar;
string used;

bool match(char letter, string word);
char askGuess(string usedLettersStr);
bool playAgain();

int hint()
{
if (THE_WORD == "DODGERS") { std::cout << "HINT: A baseball team"; return 0; }
if (THE_WORD == "COVFEFE") { std::cout << "HINT: Tweet mispelling"; return 0; }
if (THE_WORD == "MEMES") { std::cout << "HINT: Pepe is an example"; return 0; }
if (THE_WORD == "PURPLE") { std::cout << "HINT: A color"; return 0; }
if (THE_WORD == "GEOGRAPHER") { std::cout << "HINT: Studies the Earth, I.E. rocks, earthquakes"; return 0; }
if (THE_WORD == "SZECHUAN") { std::cout << "HINT: Mulan"; return 0; }
}

int main()
{
srand(time(0));

vector<string> words; // add more?
words.push_back("DODGERS");
words.push_back("COVFEFE");
words.push_back("MEMES");
words.push_back("PURPLE");
words.push_back("GEOGRAPHER");
words.push_back("SZECHUAN");

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);

cout << " _ _ " << endl;
cout << "| | | | " << endl;
cout << "| |_| | __ _ _ __ __ _ _ __ ___ __ _ _ __ " << endl;
cout << "| _ |/ _` | '_ \\ / _` | '_ ` _ \\ / _` | '_ \\ " << endl;
cout << "| | | | (_| | | | | (_| | | | | | | (_| | | | |" << endl;
cout << "|_| |_|\\__,_|_| |_|\\__, |_| |_| |_|\\__,_|_| |_|" << endl;
cout << " __/ | " << endl;
cout << " |___/ " << endl;

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout << "---------------------------------------------" << endl;
cout << "| ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
cout << "The theme is things that make Lohman mad.";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout << " |" << endl;
cout << "---------------------------------------------";

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);

bool done = false;
do
{
const int MAX_WRONG = 7;

random_shuffle(words.begin(), words.end());
THE_WORD = words[0];

soFar = string(THE_WORD.size(), '-');
used = "";


while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong) << " wrong guesses left.\n";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
hint();

used += askGuess(used);



}
if (wrong == MAX_WRONG)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout << "\nYou lose";
}

cout << "\nThe word was ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
cout << THE_WORD << endl;

} while (playAgain());

return 0;
}

inline bool match(char letter, string word)
{
return (word.find(letter) != string::npos);
}

char askGuess(string usedLettersStr)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
char guess;
cout << "\n\nWhat's your guess: ";
cin >> guess;
guess = toupper(guess);
while (match(guess, used))
{
cout << "\nYou've already guessed " << guess << " you dork." << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}



if (match(guess, THE_WORD))
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
cout << "That's right! " << guess << " is in the word.\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);


for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout << "Nope, " << guess << " isn't in the word.\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
++wrong;
}return guess;
}
bool playAgain() // function to play again while clearing system
{
char again;
cout << "\n\nWould you like to play again? <y/n>: ";
cin >> again;

cin.clear(); //clear and ignore cin
cin.ignore();

again = toupper(again);

system("cls");

return (again == 'Y');
}
It's not clear if you want the game to continue indefinitely, or to only play once.
The simplest solution is to change playAgain() to return a fixed value.
1
2
3
bool playAgain() // function to play again while clearing system
{  return false;   //  Assumes you don't want the game to play indefinitely
}

If you want the game to play indefinitely, then return true.

If you want to get rid of the function altogether, then you will have to get rid of the do/while loop that calls playAgain().

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Actually you will see where the function is used much more clearly if you write your code in a better layout (i.e. use indentation).

In fact you only use playAgain in a do...while construct, which reads

do
// play the game
while(playAgain()) // == true).

As stated before, you have to remove the whole cycle if you want to get rid of the function.
Topic archived. No new replies allowed.