使用局部变量超出循环范围

时间:2014-04-07 13:46:05

标签: c variables for-loop counter local-variables

我有一个计算任何给定牌的总价值的功能,它将所有当前牌的总和存储到得分中。 它还计算甲板上的A数。 循环后我无法链接ace和得分的值。 我已经在我的实际文件中实现了print语句,它正确计算,但得分和ace显然在for循环后填充了随机数。 我不知道如何在for循环之外使用这些值。

    void calculateScore(Player * current_player)
    {
        int i, ace;
        unsigned score;
        ace = 0;
            score = 0;

        for (i=0; i<current_player->curnumcards; i++){
            if (current_player->hand[i].c_face == 0){
                ace++;
            } else if (current_player->hand[i].c_face == 10){
                score += 10;
            } else if (current_player->hand[i].c_face == 11){
                score += 10;
            } else if (current_player->hand[i].c_face == 12){
                score += 10;
            } else {
                score += ++current_player->hand[i].c_face;
            }//end if to check ace/jack/queen/king  

        }//end for loop

        if (ace>0){
            for (;ace!=1;ace--){

                if (score>11){
                    score += 1;
                } else {
                    score += 11;
                }//end if > 10 ace
            }//end for loop to calculate ace's
        }// end if ace loop 

        printf("Current score: %d\n", &score);

    }

2 个答案:

答案 0 :(得分:1)

你应该printf(&#34;当前得分%u \ n&#34;,得分);您正在打印内存地址和分数,您只需要得分。它没有签名所以%u不是%i。

答案 1 :(得分:-1)

ace, score = 0;

ace的值,对它不执行任何操作,并将{0}分配给score。您可能希望为两者分配0:

ace = score = 0;
相关问题