我一直在Cpp从事Hangman游戏。
因此,我创建了一个名为SoFar的变量,该变量在开始时存储破折号,但逐渐被发现。

for(i = 0; i <= TheWord.length(); i++)
            SoFar[i] = '-';


因此,我(尝试)将SoFar初始化为在开头包含破折号,并且长度与TheWord相同

稍后,当我打印SoFar时,它就空了!

cout << "\nSo far, the word is : " << SoFar << endl;




任何和所有建议,不胜感激。这是我的完整程序,以供参考:

#include <iostream>
 #include <cstdlib>
 #include <string>
 #include <vector>
 #include <ctime>
 #include <cctype>

using namespace std;

 int main()
 {

    vector<string> words;
    words.push_back("SHAWARMA");
    words.push_back("PSUEDOCODE");
    words.push_back("BIRYANI");
    words.push_back("TROLLED");
    srand((unsigned)time(NULL));
    string TheWord = words[(rand() % words.size()) + 1];
    string SoFar;
    const int MAXTRIES = 8;
    string used = "";
    int tries, i;
    i = tries = 0;
    char guess;
        for(i = 0; i <= TheWord.length(); i++)
            SoFar[i] = '-';

    while(tries <= MAXTRIES && SoFar != TheWord)
    {
        /****************************************************************/
        /*                          I/0                                 */
        /****************************************************************/
        cout << "\nYou haz " << MAXTRIES - tries << " tries to go!\n" ;
        cout << "You've used the following letters : ";

        for(i = 0; i <= used.length(); i++)
            cout << used[i] << " : " ;

        cout << "\nSo far, the word is : " << SoFar << endl;
        cout << "\nEnter your guess : " ;
        cin >> guess;
        /****************************************************************/
        /*                          Processing input                    */
        /****************************************************************/
        if(used.find(guess) != string::npos)
            continue;
        guess = toupper(guess);
        if(TheWord.find(guess) != string::npos)
        {
            for(i = 0; i <= TheWord.length(); i++)
            {
                if(guess == TheWord[i])
                    SoFar[i] = guess;
            }

        }
        else
        {
            cout << "\nSorry, but the word doesn't have a letter like " << guess << " in it...\n";
            tries++;
        }
                used += guess;

    }

    if(tries == MAXTRIES)
        cout << "\nYep, you've been hanged...\n";
    else if(SoFar == TheWord)
        cout << "\nYou got it! Congratulations...\n(Now Im gonna add some psuedorandomly generated words just for you <3 :P)";

    cout << "\nThe word was : " << TheWord;

    return 0;
 }

最佳答案

您将SoFar定义为:

string SoFar;


...然后您尝试写:

    for(i = 0; i <= TheWord.length(); i++)
        SoFar[i] = '-';


写入时,SoFar的长度仍为0,因此,每次执行SoFar[i] = '-';时,都会得到未定义的行为。

尝试:

std::string SoFar(TheWord.length(), '-');


这定义SoFar已经包含正确的破折号。

这是一个快速演示:

#include <string>
#include <iostream>

int main(){
    std::string TheWord{"TROLLED"};

    std::string SoFar(TheWord.length(), '-');

    std::cout << TheWord << "\n";
    std::cout << SoFar << "\n";
}


至少对我来说,这似乎可以得出正确的结果长度:

TROLLED
-------

10-08 20:04