Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Hang man by using functions

$
0
0
I have to make a hangman program using functions. I've started but I have a logic error or something in my code. When I run it and it asks me for the letter guess, I enter it and it says That's right <<guess<<is in the word. Or if its not in the word is says Sorry <<guess<< is not in the word. It displays either of these twice, then there is and error and stops working because I can not enter anything and have to quit the program. I got the getguess function to work but I think that the problem maybe in the string function.

// Hangman
// The classic game of hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;

char getGuess(string &old); //Prototype Function Declaration
string checkguess(string THE_WORD, char guess,string soFar, int wrong); //Prototype Function Declaration

using namespace std;

int main()
{
// set-up
const int MAX_WRONG = 10;	 // maximum number of incorrect guesses allowed

vector<string> words;	 // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");

srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong = 0; // number of incorrect guesses
string soFar(THE_WORD.size(), '-'); // word guessed so far
string old = ""; // letters already guessed

cout << "Welcome to Hangman. Good luck!\n";

// main loop
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong);
cout << " incorrect guesses left.\n";
cout << "\nYou've used the following letters:\n" << old << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;


char guess = getGuess(old);

string a;
a= checkguess( THE_WORD, guess, soFar, wrong);
checkguess( THE_WORD, guess, soFar, wrong);
}

// shut down
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";

cout << "\nThe word was " << THE_WORD << endl;

return 0;
}

//----getGuess Function Return starts here---

char getGuess(string &old)
{
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);	 //make uppercase since secret word in uppercase
while (old.find(guess) != string::npos);	//if guess IS in the string, ask for guess again in while loop
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
old += guess;
return guess;
}


string checkguess(string THE_WORD,char guess, string soFar, int wrong)
{
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word.\n";


for (unsigned 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";
++wrong;
}	
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>