c++ - 字符串未正确打印/初始化

标签 c++ string

我一直在用 Cpp 开发 Hangman 游戏... 所以我创建了一个名为 SoFar 的变量,它在开始时存储破折号,但逐渐发现。

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

所以,我(尝试)初始化 SoFar 以在开头包含破折号,并且与 TheWord 的长度相同

稍后,当我打印 SoFar 时,它只是空的!

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

Picture showing output

感谢任何和所有建议。这是我的完整程序,供引用:

#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
-------

关于c++ - 字符串未正确打印/初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842518/

相关文章:

.net - 字符串类型 .NET 与 char 数组

c++ - OutputDebugString 错误

c++ - 使用 LibAV : avcodec_decode_video2? 将压缩帧解码到内存

C++ 模板特化思考

Objective-C,如何获取 UITextField 的值?

bash 在文件的每一行中搜索字符串

c++ - 最佳虚拟机/字节码解释器循环

c++ - 如何使用线性代数的C++模板库Eigen?

c - 从 C 中的文件读取后,在字符串后面放置一个空值?

Ruby:将 String 转换为 Integer 或在必要时将其保留为 String 的最佳方法?