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

时间:2014-02-18 01:16:30

标签: 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;
 }

3 个答案:

答案 0 :(得分:2)

您正在尝试修改不存在的对象:

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] = '-';

由于SoFar为空,因此尝试修改SoFar[i]是未定义的行为。

答案 1 :(得分:2)

您将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
-------

答案 2 :(得分:1)

您已将soFar指定为字符串,因此请尝试使用双引号作为值

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