调试code用C [英] Debugging code in C
本文介绍了调试code用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人能告诉我什么是错我的code和为什么它是产生此输出。
code:
INT的main(){
无符号NUM;
焦炭反应;做{
的printf(请输入一个正整数大于1且小于2000:);
scanf函数(%D,试验#);
如果(NUM→1&放大器;试验#&下; 2000){
的printf(所有%D的主要因素,下面给出:\\ n,NUM);
printPrimeFactors(NUM);
的printf(\\ n \\ n此%d个鲜明的首要因素,下面给出:\\ n,NUM);
printDistinctPrimeFactors(NUM);
}
其他{
的printf(\\ nSorry这个数字不1和2000年\\ n之间落);
}
的printf(\\ n \\ n您想尝试另一个号码说Y(ES)或N(O):);
的getchar();
响应=的getchar();
}
而(响应=='Y'||响应=='Y'); //如果响应是Y或y,则程序再次运行
输出(感谢您使用我的节目再见\\ n \\ n!); //如果不是Y或Y,程序终止
返回0;
}
输出:
请输入一个正整数大于1并且小于2000时:1600
所有的1600的素因数给出如下:
2 2 2 2 2 2 5 51600独特的首要因素,给出如下:
2 5你想尝试另一个号码?说Y(ES)或N(O):是
请输入大于1比2000的一个正整数,更大的少:所有1600的主要因素,给出如下:
2 2 2 2 2 2 5 51600独特的首要因素,给出如下:
2 5你想尝试另一个号码?说Y(ES)或N(O):感谢您使用我的程序。再见!
解决方案
在问你是否要重复,你只读过是
,留下 ES
在标准输入排队。当你去阅读下一个数字, scanf函数
试图解析为一个数字,失败,并返回不改变 NUM
。当您提示用户,你需要刻录全线飘红。
Can someone tell me what is wrong with my code and why it is producing this output.
Code:
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;
}
Output:
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!
解决方案
When you ask whether you want to repeat, you only read the y
, leaving the es
queued up in stdin. When you go to read the next number, scanf
tries to parse that as a number, fails, and returns without altering num
. When you prompt the user, you need to burn that whole line.
这篇关于调试code用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文