c++ - how to compare string word to chat guess? -
c++ - how to compare string word to chat guess? -
i working on programme (hangman) , having problems comparing user input (char) word guessed (string) determine if guessed letter in word or not.
#include <iostream> #include <fstream> // ifstream , ofstream #include <iomanip> // input/output manipulation #include <cctype> // toupper() function #include <cstring> #include <string> // strings using namespace std; #include "myfuncts.h" // programmer defined includes //ascii fine art from: http://ascii.co.uk/art/hangman manus o'donnell int main() { ifstream infile; // used read file string stage0; string stage1; string stage2; string stage3; string stage4; string stage5; string stage6; string word; // convert array downwards road char guess; int numwrong = 0; bool found = true; //stage0 stage0 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||\n| |/\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; //stage1 stage1 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; //stage2 stage2 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /y . . y\\\n| | | |\n| | | . |\n| | | |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; //stage3 stage3 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /y . . y\\\n| | // | |\n| | // | . |\n| | ') | |\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; //stage4 stage4 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /y . . y\\\n| | // | | \\\\\n| | // | . | \\\\\n| | ') | | (`\n| |\n| |\n| |\n| |\n| |\n\"\"\"\"\"\"\"\"\"\"|_ |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; //stage5 stage5 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /y . . y\\\n| | // | | \\\\\n| | // | . | \\\\\n| | ') | | (`\n| | ||'\n| | ||\n| | ||\n| | ||\n| | / |\n\"\"\"\"\"\"\"\"\"\"|_`-' |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; //stage6 - game on stage6 = " ___________.._______\n| .__________))______|\n| | / / ||\n| |/ / ||\n| | / ||.-''.\n| |/ |/ _ \\\n| | || `/,|\n| | (\\\\`_.'\n| | .-`--'.\n| | /y . . y\\\n| | // | | \\\\\n| | // | . | \\\\\n| | ') | | (`\n| | ||'||\n| | || ||\n| | || ||\n| | || ||\n| | / | | \\\n\"\"\"\"\"\"\"\"\"\"|_`-' `-' |\"\"\"|\n|\"|\"\"\"\"\"\"\"\ \ '\"|\"|\n| | \\ \\ | |\n: : \\ \\ : :\n. . `' . .\n\n\n"; infile.open("hangman.dat"); if (!infile) { cout << "error opening file reading\n"; system("pause"); homecoming 1; }// end if infile >> word; // convert these array while (!infile.fail()) // .fail improve .eof catches more issues { cout << "word: " << word << endl;; infile >> word; } infile.close(); word = casechanger(word, true); //input cout << "word guess: " << word << endl << endl; //process & output while (numwrong <= 6) { if (numwrong == 0) cout << stage0 << endl; else if (numwrong == 1) cout << stage1 << endl; else if (numwrong == 2) cout << stage2 << endl; else if (numwrong == 3) cout << stage3 << endl; else if (numwrong == 4) cout << stage4 << endl; else if (numwrong == 5) cout << stage5 << endl; else cout << stage6 << endl; cout << "please come in letter guess: "; cin >> guess; guess = toupper(guess); cout << "you entered: " << guess << endl; for(int = 0; < word.length(); i++) { if(word[i] == guess) found = true; else found = false; } if (found) { cout << guess << " in word guess." << endl; } else { cout << guess << " not in word guess." << endl; numwrong = numwrong++; } } cout << "\n\n"; system("pause"); homecoming 0; } for reason, when come in letter part of word, still says not, , increments number of wrong guesses (numwrong).
could utilize afresh set of eyes, have no clue why not working.
thanks!
the problem in loop determine if letter correct. if finds right letter, loop doesn't end, , continues going through letters, changes false.
found = false; //move here set false default before loop starts for(int = 0; < word.length(); i++) { if(word[i] == guess) { found = true; break; //add here exit loop if letter found } } c++
Comments
Post a Comment