C ++邪恶的Hangman实现问题

时间:2014-04-02 17:55:31

标签: c++ if-statement vector crash

我正在尝试实施一个版本的Evil Hangman,游戏根据最难猜的内容改变单词。但是我收到了一个错误,我似乎无法发现它。这可能是愚蠢的事情,但感觉我已经盯着它好几个小时了。我在崩溃之前通过打印语句将其缩小到这个范围:

vector<string> newWords;
    bool inword=false;
    int place=0;
    cout<<"here 1";
    if(!remove){//remove true so only keep words that dont have letter in them.
        cout<<" in if(remove) \n";
        for(int i=0;i<wordsLeft.size();i++){
            cout<<" in first loop \n";
            for(int n=0;n<(wordsLeft[i].length())&&!inword;n++){
                cout<<"in second loop n: "<<n<<" i: "<<i<<" inword: "<<inword<<" length: "<<wordsLeft[i].length()<<endl;
                if(wordsLeft[i].at(n)==letter){
                    inword=true;
                }
            }
            if(!inword){//if false then add word meaning words not containing letter array
                    newWords[place]=wordsLeft[i];
                    place+=1;
            }
            else{
                inword=false;
            }
        }
        cout<<"here 2";
    }
    else{//remove false so only keep words that have letter in them.
        cout<<"here 3";
        for(int i=0;i<wordsLeft.size();i++){
            for(int n=0;n<(wordsLeft[i].length())&&!inword;n++){
                if(wordsLeft[i].at(n)==letter){
                    inword=true;
                }
            }
            if(inword){//if true then add word meaning words containing letter array
                    newWords[place]=wordsLeft[i];
                    place+=1;
                    inword=false;
            }
        }
        cout<<"here 3.5";
    }
    cout<<"here 4";
    index=place;
    wordsLeft=newWords;
}

1 个答案:

答案 0 :(得分:1)

我还无法添加评论,但根据您的评论it's saying that wordsLeft wasn't declared in the scope,这意味着您的第一个for循环中的wordsLeft无法访问。你可能已经在另一个函数的其他地方定义了该变量,或者它可能是私有的。

发布WordsLeft和下次的帖子:发布错误消息/代码