变量即使在定义后也重新定义为垃圾值

时间:2019-02-17 00:39:04

标签: c++ recursion

在某些输入之后,变量pscore被重新定义为垃圾值。 (我知道全局变量是不好的做法,但是范围有限。)这似乎是随机的,但仅在设置tscore = 0的某些情况下才会发生。

结果应遵循定义的逻辑,而不是垃圾值。

我经历了一个调试器,试图查看何时将该值更改为垃圾内容,但是没有运气。我已经有一些人在看它了,他们什么也没看到。


    int pscore = 0;
    int tscore = 0;
    int cscore = 0;
    bool again = false;

    int rollDie(){
        return random() % 6 + 1;
    }

    int pTurn(){
        if (again == true){
             print("Do you want to roll again (Y/N)?:"); 
        } else {
             print("Do you want to roll a dice (Y/N)?:"); //asks player if 
    they want to pass the turn   
        }
         again = true;
         string no;
         getline(cin, no); //reads from console and sets it to a string 
    called 'yes'
         if (no.compare("n") == 0){ //checks if the player inputs yes or 
     variants
             return tscore; //ends the turn
        } else {
             int i = rollDie(); //rtd
             switch(i){
                 case 1:
                     print("You rolled a 1");
                     tscore++;
                     print("Your turn total is ", tscore);
                     pTurn();
                     break;
                case 2:
                    print("You rolled a 2");
                    print("Your turn total is 0");
                    tscore = 0;
                    return tscore;
                    break;
                 case 3:
                    print("You rolled a 3");
                    tscore = tscore + 3;
                    print("Your turn total is ", tscore);
                    pTurn();
                    break;
                case 4:
                    print("You rolled a 4");
                    print("Your turn total is 15");
                    tscore = 15;
                    return 15;
                    break;
                case 5:
                    print("You rolled a 5");
                    print("Your turn total is 0");
                    tscore = 0;
                    return tscore;
                    break;
                case 6:
                    print("You rolled a 6");
                    tscore = tscore + 6;
                    print("Your turn total is ", tscore);
                    pTurn();
                    break;
                default:
                    return tscore;
                    break;

             }
        }
    }

void ggame(){
    if (pscore <= 80){
        print("It is now human's turn");
        again = false;
        print("");
        pscore = (pscore + pTurn()); //adds player score to the total score calculated that turn
        print("computer: ", cscore);
        cout << "human: " << pscore << endl;
        //print("human: ", pscore);
        print("");
        tscore = 0; //resets turn score
        if (pscore >= 80){cout << "human: " << pscore << endl;
            print("Congratulations! human won this round of Jeopardy Dice!");
            return; //breaks out of the recursion loop
        }
    }
    if (cscore <= 80){
        print("It is now computer's turn");
        print("");
        cscore = (cscore + cTurn()); //adds computer score to the total score calculated that turn
        print("computer: ", cscore);
        cout << "human: " << pscore << endl;
        //print("human: ", pscore);
        print("");
        tscore = 0; //resets turn score
        if (cscore >= 80){
            print("Congratulations! computer won this round of Jeopardy Dice!");
            return; //breaks out of the recursion loop
        } 
    }
   ggame(); //recursion
}

2 个答案:

答案 0 :(得分:1)

以下情况

        case 1: //adds 1 to the score and allows the player to keep playing on the same turn
            print("Computer rolled a 1");
            tscore++;
            print("Computer turn total is ", tscore);
            cTurn();
            break;

和其他类似的内容,丢弃对cTurn的递归调用的结果,并且不返回任何内容。

所以

cscore = (cscore + cTurn());

在程序认为合适的地方执行算​​术运算。换句话说,垃圾进,垃圾出。

解决方案:

return cTurn();

不是

cturn();

许多现代的编译器都会对此发出警告。编译器警告表示,虽然代码在语法上正确无误并生成了程序,但该程序可能无法满足您的要求。不要忽略编译器警告。它们是防范逻辑错误的第一道防线。

如果您的编译器没有警告您,请使用更好的编译器或咨询编译器文档以了解如何提高警告消息的详细程度。

答案 1 :(得分:0)

pturn()中有一些情况下,您退出而没有返回值。这会将堆栈上的所有垃圾添加到pscore