程序拒绝进展的奇怪故障(刽子手任务)

问题描述 投票:-2回答:1

该程序应该如何工作,它将要求用户猜测随机选择的单词。用户将通过放置可能在该单词内的字母来完成此操作。代码将随机选择3个单词,并且当用户猜到其中一个单词时,它将继续移动到下一个单词。如果用户正确地获得了所有3个单词,那么程序会祝贺他们,如果他们弄错了,那么程序就会结束。该程序将检查用户是否正在尝试输入他们已经使用过的信件,并将阻止他们这样做。该程序还将检查该字母是对还是错,如果错误,将给用户一个警告并增加他们的尝试次数。

我已经编写了几乎所有需要的东西,但我遇到了一个错误,在第一个单词之后,如果用户要输入第二个单词中的一个字母,程序就会说出这个单词错了。例如,让我们说第二个词是CIA。我会在控制台输入字母I和A,它会接受它,但是当我输入C时它会告诉我C不在所选单词中并且会增加我,如果我要尝试输入在C再次它将阻止我这样做。尝试调试但我无法明确指出问题。

#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <string>
#include<vector>
#include<ctime>
#include<algorithm>
#include<cctype>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    string user;
    // Display Title of program to user
    cout << "Keywords" << endl;
    // Ask the recruit to login using thier name
    cout << "Log in using your name" << endl;
    // Hold the recruit's name in a var, and address them by it throughout the simulation.
    cin >> user;
    // Display an overview of what Keywords II is to the recruit 
    cout << " Hello " << user << ". This program is to test out your code breaking skills." << endl;
    // Display an directions to the recruit on how to use Keywords
    cout << "The program will load a random word, and it is your job to guess what that word is." << endl;
    cout << "In order to guess a word, you must guess the letters that are in the word. Use your keyboard to type in the letter that you think is in the word" << endl;
    cout << "Every time you guess a right letter, the program will congratulate you. Every time you guess wrong, however, you will lose a turn. When you lose too many turns, the program will end" << endl;
    cout << "Remember, the aim is to guess enough right letters to make up the word that was randomly chosen for you." << endl;
        // Create a collection of 10 words you had wrote down manually
    vector<string>words;
    words.push_back("PRESIDENT");
    words.push_back("FILES");
    words.push_back("INFORMATION");
    words.push_back("COUNTRY");
    words.push_back("SECURITY");
    words.push_back("CIA");
    words.push_back("SECRETS");
    words.push_back("DATA");
    words.push_back("SERVICE");
    words.push_back("WANTED");

    // Create an int var to count the number of simulations being run starting at 1
    int tries = 3;
        // Display the simulation # is staring to the recruit. 
    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(words.begin(), words.end());
        // Pick new 3 random words from your collection as the secret code word the recruit has to guess. 
    const string word1 = words[0];
    const string word2 = words[1];
    const string word3 = words[2];
    string soFar1(word1.size(), '-');
    string soFar2(word2.size(), '-');
    string soFar3(word3.size(), '-');
    string used = "";
    int game = 1;
        // While recruit hasn’t made too many incorrect guesses and hasn’t guessed the secret word
    //Start of first word
    while ((tries != 0) && (soFar1 != word1) && (game == 1)) {
        //     Tell recruit how many incorrect guesses he or she has left
        cout << "You have " << tries;
        cout << " more tries until the program ends. Keep on at it!" << endl;
        //     Show recruit the letters he or she has guessed
        cout << "The following letters were used:\n" << used << endl;
        //     Show player how much of the secret word he or she has guessed
        cout << "Here is the progress you are making with this word:" << soFar1 << endl;
        //     Get recruit's next guess
        char guess;
        cout << "Enter the letter that you want to use:";
        cin >> guess;
        guess = toupper(guess);
        //While recruit has entered a letter that he or she has already guessed
        while (used.find(guess) != string::npos) {
            cout << "The letter" << guess << "was already used. Use another letter." << endl;
            //Get recruit ’s guess
            cin >> guess;
            guess = toupper(guess);
        }
            //Add the new guess to the group of used letters
        used += guess;
            //If the guess is in the secret word
        if (word1.find(guess) != string::npos) {
            //Tell the recruit the guess is correct
            cout << "The letter you used is in this word ";
            // Update the word guessed so far with the new letter
            for (int i = 0; i < word1.length(); ++i) {
                if (word1[i] == guess) {
                    soFar1[i] = guess;
                }
            }
        }
        //Otherwise
        else {
            //Tell the recruit the guess is incorrect
            cout << "You guessed the wrong letter. " << guess << " is not in the word.\n";
            // Increment the number of incorrect guesses the recruit has made
            --tries;
        }
        //End of first word
    }
    if (soFar1 == word1) {
        cout << "Congrats, you got that code right. Here comes the next one." << endl;
        used = "";
        ++game;

    }
    //Start of second word
    while ((tries != 0) && (soFar2 != word2)&&( game == 2)) {
        //     Tell recruit how many incorrect guesses he or she has left
        cout << "You have " << tries;
        cout << " more tries until the program ends. Keep on at it!" << endl;
        //     Show recruit the letters he or she has guessed
        cout << "The following letters were used:\n" << used << endl;
        //     Show player how much of the secret word he or she has guessed
        cout << "Here is the progress you are making with this word:" << soFar2 << endl;
        //     Get recruit's next guess
        char guess;
        cout << "Enter the letter that you want to use:";
        cin >> guess;
        guess = toupper(guess);
        //While recruit has entered a letter that he or she has already guessed
        while (used.find(guess) != string::npos) {
            cout << "The letter" << guess << "was already used. Use another letter." << endl;
            //Get recruit ’s guess
            cin >> guess;
            guess = toupper(guess);
        }
        //Add the new guess to the group of used letters
        used += guess;
        //If the guess is in the secret word
        if (word1.find(guess) != string::npos) {
            //Tell the recruit the guess is correct
            cout << "The letter you used is in this word ";
            // Update the word guessed so far with the new letter
            for (int i = 0; i < word2.length(); ++i) {
                if (word2[i] == guess) {
                    soFar2[i] = guess;
                }
            }
        }
        //Otherwise
        else {
            //Tell the recruit the guess is incorrect
            cout << "You guessed the wrong letter. " << guess << " is not in the word.\n";
            // Increment the number of incorrect guesses the recruit has made
            --tries;
        }
        //End of second word
    }
    if (soFar2 == word2) {
        cout << "Congrats, you got that code right. Here comes the next one." << endl;
        used = "";
        ++game;

    }
    //Start of first word
    while ((tries != 0) && (soFar3 != word3) && (game == 3)) {
        //     Tell recruit how many incorrect guesses he or she has left
        cout << "You have " << tries;
        cout << " more tries until the program ends. Keep on at it!" << endl;
        //     Show recruit the letters he or she has guessed
        cout << "The following letters were used:\n" << used << endl;
        //     Show player how much of the secret word he or she has guessed
        cout << "Here is the progress you are making with this word:" << soFar3 << endl;
        //     Get recruit's next guess
        char guess;
        cout << "Enter the letter that you want to use:";
        cin >> guess;
        guess = toupper(guess);
        //While recruit has entered a letter that he or she has already guessed
        while (used.find(guess) != string::npos) {
            cout << "The letter" << guess << "was already used. Use another letter." << endl;
            //Get recruit ’s guess
            cin >> guess;
            guess = toupper(guess);
        }
        //Add the new guess to the group of used letters
        used += guess;
        //If the guess is in the secret word
        if (word1.find(guess) != string::npos) {
            //Tell the recruit the guess is correct
            cout << "The letter you used is in this word ";
            // Update the word guessed so far with the new letter
            for (int i = 0; i < word1.length(); ++i) {
                if (word3[i] == guess) {
                    soFar3[i] = guess;
                }
            }
        }
        //Otherwise
        else {
            //Tell the recruit the guess is incorrect
            cout << "You guessed the wrong letter. " << guess << " is not in the word.\n";
            // Increment the number of incorrect guesses the recruit has made
            --tries;
        }
        //End of second word
    }
    // If the recruit has made too many incorrect guesses
    if (tries == 0) {
    //     Tell the recruit that he or she has failed the Keywords II course.
        cout << "You have failed. Your director shall hear about this" << endl;
    }
    // Otherwise
    else {
    // Congratulate the recruit on guessing the secret words
        cout << "You guessed all words right. Congrats!" << endl;
    }
    cout << "\n The words were: " << word1 << "\n" << word2 << "\n" << word3 << endl;


        // Ask the recruit if they would like to run the simulation again

        // If the recruit wants to run the simulation again

        //     Increment the number of simiulations ran counter

        //     Move program execution back up to // Display the simulation # is staring to the recruit. 

        // Otherwise 

        //     Display End of Simulations to the recruit

        //     Pause the Simulation with press any key to continue

    cin >> user;
    return 0;
}
c++
1个回答
1
投票

你的问题似乎是所有三款游戏都使用word1.find(guess)。我还建议创建一个PlayGame(const string& word)函数,以减少重复和bug的可能性。

© www.soinside.com 2019 - 2024. All rights reserved.