我需要从用户那里获取给定范围内的输入整数。我需要得到他们的低点和高点。但循环并没有停止,我不知道为什么。我以为我的条件很好,但循环不会停止。
int obtainNumberBetween (const char* descriptionCPtr, int low, int high) {
char line[MAX_LINE];
int entry;
// #define MAX_LINE 256
// YOUR CODE HERE
do
{
printf("Please enter the lowest number in the range (%d-%d):\n " ,RANGE_LOWEST ,RANGE_HIGHEST);
fgets(line, 256, stdin);
low = atoi(line);
printf ("The value entered is %d\n", low);
}
while ( (entry < low) || (entry > high) );
return(entry);
}
应有的示例输出:
Please enter the lowest number in the range (0-32767): -6
Please enter the lowest number in the range (0-32767): 1
Please enter the highest number in the range (1-32767): 0
Please enter the highest number in the range (1-32767): 32768
Please enter the highest number in the range (1-32767): 512
最佳答案
您的entry
变量未初始化。所以这一行调用了一个未定义的行为:
while ( (entry < low) || (entry > high) )
也许您应该将用户输入分配给条目
,而不是low
。
关于c - 获取用户输入并在需要时停止循环 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41800077/