string - C++ hangman game -


i wrote code hang man game read in words vector dictionary file. code keeps crashing , cannot life of me see cause of might be. advice can offer great help!

#include <iostream> #include <fstream> #include <vector> #include <cstdlib> #include <ctime> #include <string>  using namespace std;  const int max_tries=5;  int letterfill (char guess, string secretword, string &guessword) {     int i;     int matches=0;     int len=secretword.length();     (i = 0; i< len; i++)     {     // did match letter in previous guess?     if (guess == guessword[i])     return 0;      // guess in secret word?         if (guess == secretword[i])         {             guessword[i] = guess;             matches++;         }     } return matches; }   int main () {     string name;     char letter;     int num_of_wrong_guesses=0;     //input file    ifstream infile;    infile.open("dictionary2.txt");     //vector elements preallocated     //to increase push_back speed    vector<string> dict;      //temporary string     string temp;      //grab words file vector     while(infile >> temp)     {       dict.push_back( temp );     }       //close input file     infile.close();      //seed random number generator     srand(time(null));      //generate random index     int index = rand() % dict.size();      //word use hangman     string hangmanword = dict[index];     cout << hangmanword << endl;          // initialize secret word * character.     string unknown(hangmanword.length(),'*');      // welcome user     cout << "\nwelcome hangman";     cout << "\neach letter represented underscore.";     cout << "\nyou have type 1 letter in 1 try";     cout << "\nyou have " << max_tries << " attempts left";     cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";      // loop until guesses used     while (num_of_wrong_guesses < max_tries)     {         cout << "\n" << unknown;         cout << "\n guess letter: ";         cin >> letter;         // fill secret word letter if guess correct,         // otherwise increment number of wrong guesses.         if (letterfill(letter, hangmanword, unknown)==0)         {             cout << endl << "whoops! letter isn't in there!" << endl;             num_of_wrong_guesses++;         }         else         {             cout << endl << "you found letter! isn't exciting!" << endl;         }         // tell user how many guesses has left.         cout << "you have " << max_tries - num_of_wrong_guesses;         cout << " guesses left." << endl;         // check if user guessed word.         if (hangmanword==unknown)         {             cout << hangmanword << endl;             cout << "yeah! got it!";             break;         }     }     if(num_of_wrong_guesses == max_tries)     {         cout << "\nsorry, lose...you've been hanged." << endl;         cout << "the word : " << hangmanword << endl;     }     cin.ignore();     cin.get();     return 0; } 

since don't know how use debugger, places in code suspicious these:

int len=secretword.length(); (i = 0; i< len; i++) {   // did match letter in previous guess?    if (guess == guessword[i]) 

and this:

 //generate random index int index = rand() % dict.size();  //word use hangman string hangmanword = dict[index]; 

for first item, use length of secretword determine how many iterations loop (and use "i" loop counter). however, use "i" in guessword string. how know if position "i" in guessword out of bounds?

according how call function, secret word may "ladder" , guessword "lad". when index 3 of secret word, there no guessword[3], causing erroneous access. have 2 separate strings, , you're assuming same size when may not same size. fix making sure loop goes

[ 0, min(guessword.size(), secretword.size()) ).

in other words, loop should loop starting 0 smaller in length of 2 strings, less one.

for second item, value of index may or may not in bounds of dict vector. please check printing out values of index , dict.size(). if index out of bounds of dict.size(), know problem -- dictionary either didn't read words thought did, or index calculation wrong.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -