1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

error with code?

Discussion in 'C++' started by zendet, Apr 2, 2010.

  1. zendet

    zendet New Member

    Joined:
    Mar 27, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    It works good if you get the word right, but when it starts if i get all my guesses wrong and then type y to restart it does not, it just stays put..

    Code:
     
     
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cctype>
    
    using namespace std;
    string THE_WORD; // guess word
    int wrong;
    string soFar; 
    string used;
    bool match(char letter, string word);
    char askGuess(string usedLettersStr); 
    bool playAgain();
    int main()  //main
    {
     srand(time(0)); //gets the random word
     vector<string> words; // list of words to get
     words.push_back("BROWN");
     words.push_back("GOLD");
     words.push_back("GREY");
     words.push_back("SILVER");
     words.push_back("BLACK");
     words.push_back("WHITE");
     words.push_back("PURPLE");
     words.push_back("PINK");
     words.push_back("YELLOW");
     words.push_back("GREEN");
     words.push_back("RED");
     words.push_back("BLUE");
     
    //title
    cout<<"\n\n";
    cout<< "\t##  ##   ####    ##   ##   ###     ##      ##   ####    ##   ##\n";
    cout<< "\t##  ##  ##  ###  ###  ##  ##       ###    ###  ##  ###  ###  ##\n";
    cout<< "\t######  #######  ## # ##  ##  ###  ## #  # ##  #######  ## # ##\n";
    cout<< "\t##  ##  ##   ##  ##  ###  ##   #   ##  ##  ##  ##   ##  ##  ###\n";
    cout<< "\t##  ##  ##   ##  ##   ##   ####    ##  ##  ##  ##   ##  ##   ##\n\n";
    
     cout << "\t\t\t   Hangman 2010 - Wayne Daly\n";
     
     cout<< "\n\n\nHINT: The word is a colour\n";
     // loop starts here
     bool done = false;
     do
     {
      const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed before death, change your total lives here
      random_shuffle(words.begin(), words.end());
      THE_WORD = words[0]; // word to guess
      soFar = string(THE_WORD.size(), '*'); // word guessed so far
      used = ""; // letters already guessed
      // loop for current word
      while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
      {
       cout << "\n\nYou have " << (MAX_WRONG - wrong)
        << " Lives left.\n";
       cout << "\nYou've picked the following correct letters:\n" << used
        << endl;
       cout << "\nSo far, the word is:\n\n" << soFar << endl;
       used += askGuess(used);
       {
       } // end of while 
       
    
      }
      if (wrong == MAX_WRONG) //when all your lives are gone
      {
       cout<<"\n\n";
       cout<<"   +----+     \n";
       cout<<"   |    |     \n";
       cout<<"   |    O     \n";
       cout<<"   |   /|\\   \n";
       cout<<"   |   / \\   \n";
       cout<<"   |Your Dead \n";
       cout<<"  ============\n\n";
       
       
      }
      cout << "\nThe word was " << THE_WORD << endl;
     } while (playAgain());
     return 0;
    }
    inline bool match(char letter, string word)
    {
     return (word.find(letter) != string::npos);
    }
    char askGuess(string usedLettersStr)
    {
     char guess;
     cout << "\n\nEnter your guess: ";
     cin >> guess;
     guess = toupper(guess); //make uppercase since secret word in uppercase
     while (used.find(guess) != string::npos)
      while (match(guess, used))
      {
       cout << "\nYou have already guessed " << guess << endl;
       cout << "Enter your guess: ";
       cin >> guess;
       guess = toupper(guess);
      }
     
     if (match(guess, THE_WORD))
     {
      cout << "That's right! " << guess << " is in the word.\n";
     }
     else
     {
      cout << "Sorry, " << guess << " isn't in the word.\n";
      ++wrong;
     }
     // update soFar to include new guessed letter
     for (unsigned int i = 0; i < THE_WORD.length(); ++i)
      if (THE_WORD[i] == guess)
       soFar[i] = guess;
     return 0;
    }
    bool playAgain() //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');
    }
     
     
     
     
     
    
     
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    common mistake when you use global variables.

    Code:
    .................
    const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed before death, change your total lives here
        [COLOR=Red]wrong=0;//add this.[/COLOR]
        random_shuffle(words.begin(), words.end());
    .................
    
     

Share This Page