数组和for循环的逻辑错误

时间:2016-04-16 19:22:42

标签: c++ arrays

如果我在5 5数组中输入score[]个玩家(scanf("%d", &numPlayers)元素)的数量,那么玩家将一直向玩家循环播放5(score[]的元素4),然后跳转到winMsg函数。即使我在第一个for循环中将所有score[]元素设置为0,该元素的分数也是一个很大的值。如果我输入6个或更多元素,则第二个for循环永远不会执行。对于4score[]或更少的元素,程序运行没有问题。我在Ubuntu中使用gedit和终端。有任何想法吗?相当新的编程。我感谢任何帮助。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

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

void winMsg(int winRoll, int player, int winScore){
printf("Congratulations Player %d! Your winning roll was a %d, putting your score at %d!\n\nGame Over\n\n", player + 1, winRoll, winScore);
return;
}


int main(){

srand(time(NULL));
int numPlayers = 0;
int roll = 0;
int i = 0;
int score[numPlayers];
char choice[2];

printf("Welcome to the \"Roll to Win\" game. Each roll adds to the current player's score, according to the die's number. A roll of 1 will cause the player to recieve no points that round, and then be skipped to the next player. First player to reach 100 or over wins! Please enter number of players: \n\n");
scanf("%d", &numPlayers);
printf("\n");

while (numPlayers >= 100 || numPlayers <= 0){
    printf("Please enter a number of players less than 100, greater than 0.\n\n");
    scanf("%d", &numPlayers);
    printf("\n");
}

for (i = 0; i < numPlayers; ++i){
    score[i] = 0;
    printf("Set Player %d score to %d.\n", i + 1, score[i]);
}   

printf("Starting with Player 1.\n\n");

for (i = 0; i < numPlayers; ++i){

    roll = rollDie();

    if (roll == 1){
        printf("Player %d rolled a 1. Skipping turn. Current score: %d.\n\n", i + 1, score[i]);
    }
    else{
        do{
            score[i] += roll;
            if (score[i] >= 100){
            winMsg(roll, i, score[i]);
            exit(0);
            }
            printf("Player %d rolled a %d, continue rolling (enter r to roll, or sr to stop rolling)? Current score: %d.\n\n", i + 1, roll, score[i]);
            scanf("%s", choice);
            printf("\n");

            while ((strcmp ("r",choice) != 0) & (strcmp ("sr",choice) != 0)){
                printf("Please enter a correct selection (enter r to roll, or sr to stop rolling).\n\n");
                scanf("%s", choice);
                printf("\n");
            }

            if (strcmp ("sr",choice) == 0){
                printf("Player %d decided to stop rolling. Continuing to next player.\n\n", i + 1);
                break;
            }

            roll = rollDie();
            if (roll == 1){
                printf("Player %d rolled a 1. Skipping turn. Current score: %d.\n\n", i + 1, score[i]);
                break;
            }

        } while (strcmp (choice,"r") == 0);
    }

    if (i == numPlayers - 1){
        i = -1;
    }
}



}

2 个答案:

答案 0 :(得分:0)

请注意,您在数组初始化之前设置了大小,因此最终会产生垃圾。

答案 1 :(得分:0)

执行int score[numPlayers];及以后scanf("%d", &numPlayers);将无法完成您的预期。

  1. 如果你想要这个行为你应该使用std :: vector,那么静态数组的大小不是标准的C ++。

  2. 即使这对您有用,您也应首先询问玩家数量,然后创建阵列。即

    scanf("%d", &numPlayers);//first

    ....

    int score[numPlayers];//then

相关问题