数组和循环的逻辑错误

Logic error with array and for loop

本文关键字:错误 循环 数组      更新时间:2023-10-16

如果我在scanf("%d", &numPlayers)中输入一定数量的5播放器(score[]数组中的5元素),for循环将播放器一直循环到播放器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!nnGame Overnn", 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: nn");
scanf("%d", &numPlayers);
printf("n");
while (numPlayers >= 100 || numPlayers <= 0){
    printf("Please enter a number of players less than 100, greater than 0.nn");
    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.nn");
for (i = 0; i < numPlayers; ++i){
    roll = rollDie();
    if (roll == 1){
        printf("Player %d rolled a 1. Skipping turn. Current score: %d.nn", 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.nn", 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).nn");
                scanf("%s", choice);
                printf("n");
            }
            if (strcmp ("sr",choice) == 0){
                printf("Player %d decided to stop rolling. Continuing to next player.nn", i + 1);
                break;
            }
            roll = rollDie();
            if (roll == 1){
                printf("Player %d rolled a 1. Skipping turn. Current score: %d.nn", i + 1, score[i]);
                break;
            }
        } while (strcmp (choice,"r") == 0);
    }
    if (i == numPlayers - 1){
        i = -1;
    }
}

}

请注意,在已知数组之前初始化数组时设置了大小,因此最终会出现垃圾。

执行int score[numPlayers];和更高版本的scanf("%d", &numPlayers);不会达到您认为的效果。

  1. 具有不是常量的静态数组大小不是标准C++,如果您想要这种行为,则应该使用std::vector。

  2. 即使这对你有效,你也应该先询问玩家的数量,然后创建数组。即

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

    ....

    int score[numPlayers];//then