This is pretty much my first attempt at anything in C++. I started off writing code in HTML then PHP now I've moved on to C++.
Specifically in this, I am having a problem with the menu function. When you compile and run the code and hit x + enter or any other letter, it loops -- endlessly. I'm having trouble figuring it out, any help would be appreciated. Thanks.
Code:// Hangman #include <iostream> #include <string> using namespace std; void menu(); string make_display_word(string s) { string t = ""; for (int i = 0; i < s.size(); i++) t += '_'; return t; } bool check_entries(string entry) { for(int i=0; i<entry.size(); i++) { if(isalpha(entry[i])) { } else { return false; } } return true; } void play(string word, string guesses, string correct) { char guess; cout << "Please guess a letter, the word is " << word.size() << " letters long\n\n"; cin >> guess; if(!(isalpha(guess))) { cout << "You must enter only letters\n\n"; play(word, guesses, correct); } // check if guessed char is only a letter. if(int(guesses.find(guess))<0) { if(int(word.find(guess))<0) { guesses += guess; cout << "Wrong Guesses: " << guesses << "\n\n"; } else { for(int j=0; j < word.size(); j++) { if(word[j]==guess) { correct[j]=guess; } } if(word==correct) { cout << "\nCongratulations you have the right word! " << correct << "\n\n\n\n\n\n\n\n\n\n"; exit(0); } else { cout << "\"" << guess << "\" is a correct guess!\n" << "What you have so far: " << correct << endl << endl; } } } else { cout << "You already guessed that letter!\n\n"; play(word, guesses, correct); } play(word, guesses, correct); } void begin_play() { string word; string guesses = ""; cout << "Please enter the word for the game "; cin >> word; if(check_entries(word)==false) { cout << "You must enter only letters.\n"; begin_play(); } play(word, guesses, make_display_word(word)); } void menu() { int menuchoice; // Input for menu selection cout << "1. Play!\n2. Instructions\n3. Rules\n\n"; cin >> menuchoice; switch(menuchoice) { case 1: begin_play(); break; case 2: cout << "Instructions to follow soon.\n\n\n\n"; menu(); break; case 3: cout << "Rules to follow soon.\n\n\n\n"; menu(); break; default: cout << "Please select a menu choice.\n\n\n\n\n\n"; menu(); } } int main() { cout << "Welcome to Hangman!\n\n"; menu(); // calls menu function return 0; }



LinkBack URL
About LinkBacks




