我需要从给定范围内的用户获取输入整数。我需要得到他们的低和高。但循环不停,我不知道为什么。我认为我的条件很好,但循环不会停止。它应该是什么获取用户输入并在需要时停止循环C
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
什么入口在干什么?你在里面比较什么价值? –
那么,用户输入需要低于“低”,或大于“高”,然后用户被要求另一个号码。用户最终输入一个合法的号码后,该函数返回该号码。 – yeny314
low = atoi(line);你低覆盖,你是否打算写入?因为你没有做任何事情进入,但你的修改很低,因此你失去了最低的价值。 –