gpt4 book ai didi

c - while循环导致程序挂起

转载 作者:行者123 更新时间:2023-11-30 14:24:06 26 4
gpt4 key购买 nike

我正在做一项类作业(不评分),并且不清楚为什么这段代码会导致我的程序“挂起”而不是循环运行。

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

int main()
{
int nbStars = 0; // User defines the number of stars to display
int nbLines = 0; // User defines the number of lines on which to print

// Obtain the number of Stars to display
printf("Enter the number of Stars to display (1-3): ");
scanf("%d", &nbStars);
getchar();

// Limit the values entered to between 1 and 3
do {
printf("Enter the number of Stars to display (1-3): ");
scanf("%d", &nbStars);

if (nbStars < 1 || nbStars > 3) puts("\tENTRY ERROR: Please limit responses to between 1 and 3.\n");
} while (nbStars < 1 || nbStars > 3);
}

最佳答案

肯定还有其他问题发生,因为您的代码既可以在带有 GCC 的 Linux 上运行,也可以在带有 GCC 的 Windows 7 cygwin 上运行。您能否提供有关您正在使用的输入和环境的更多详细信息?

尝试此代码,看看是否会得到不同的行为:

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

int main()
{
int nbStars = 0; // User defines the number of stars to display
int nbLines = 0; // User defines the number of lines on which to print

// Obtain the number of Stars to display
do
{
printf("Enter the number of Stars to display (1-3): ");
scanf("%d", &nbStars);

if (nbStars < 1 || nbStars > 3)
{
puts("\tENTRY ERROR: Please limit responses to between 1 and 3.\n");
}
}while (nbStars < 1 || nbStars > 3);

printf("You entered %d\n", nbStars);
return( 0 );
}

关于c - while循环导致程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com