gpt4 book ai didi

c - 调试 C 代码

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

有人可以告诉我我的代码有什么问题以及为什么会产生此输出。

代码:

int main(){
unsigned num;
char response;

do{
printf("Please enter a positive integer greater than 1 and less than 2000: ");
scanf("%d", &num);
if (num > 1 && num < 2000){
printf("All the prime factors of %d are given below: \n", num);
printPrimeFactors(num);
printf("\n\nThe distinct prime factors of %d are given below: \n", num);
printDistinctPrimeFactors(num);
}
else{
printf("\nSorry that number does not fall between 1 and 2000.\n");
}
printf("\n\nDo you want to try another number? Say Y(es) or N(o): ");
getchar();
response = getchar();
}
while(response == 'Y' || response == 'y'); // if response is Y or y then program runs again
printf("Thank you for using my program. Good Bye!\n\n"); //if not Y or y, program terminates
return 0;
}

输出:

Please enter a positive integer greater than 1 and less than 2000: 1600
All the prime factors of 1600 are given below:
2 2 2 2 2 2 5 5

The distinct prime factors of 1600 are given below:
2 5

Do you want to try another number? Say Y(es) or N(o): yes
Please enter a positive integer greater than 1 and less than 2000: All the prime factors of 1600 are given below:
2 2 2 2 2 2 5 5

The distinct prime factors of 1600 are given below:
2 5

Do you want to try another number? Say Y(es) or N(o): Thank you for using my program. Good Bye!

最佳答案

当您询问是否要重复时,您只读取y,而将es留在标准输入中排队。当您读取下一个数字时,scanf 尝试将其解析为数字,失败并返回而不更改 num。当您提示用户时,您需要烧掉整行。

关于c - 调试 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655427/

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