数组和循环的逻辑错误
Logic error with array and for loop
如果我在scanf("%d", &numPlayers)
中输入一定数量的5
播放器(score[]
数组中的5
元素),for循环将播放器一直循环到播放器5(score[]
的元素4),然后跳到winMsg函数。尽管我在第一个for循环中将所有score[]
元素设置为0
,但该元素的分数是一个很大的值。如果我输入6
或更多元素,则第二个for循环永远不会执行。程序使用4
或score[]
中更少的元素运行时没有问题。我在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);
不会达到您认为的效果。
-
具有不是常量的静态数组大小不是标准C++,如果您想要这种行为,则应该使用std::vector。
-
即使这对你有效,你也应该先询问玩家的数量,然后创建数组。即
scanf("%d", &numPlayers);//first
....
int score[numPlayers];//then
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 方法内部但循环仍得到预期的不合格id错误C++
- C++ - 循环访问指针数组会导致错误
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- For 循环 C++ 中的值错误
- 为什么我的 for 循环给出错误:X 没有命名类型
- 类方法 - 数据结构中 For 循环的运行时错误
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如何向用户显示特定错误,要求他/她使用循环再次提供输入?
- 循环 c++ 中的分段错误
- 虽然循环即使应用了错误条件也不会退出
- 错误 C2512 视觉C++(并且不是循环依赖项)
- 使用基于范围的指针循环和向量时出现C++错误
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 在指针迭代中使用for循环时出现分段错误
- 向量循环抛出2个错误
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- C++,查找最低值,函数错误:循环最多运行一次(循环增量从未执行)