用C代码调试(Debugging code in C)

2019-08-16 16:54发布

谁能告诉我什么是错我的代码以及为何产生这种输出。

码:

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!

Answer 1:

当你问你是否要重复一遍,你只读过y ,使es在标准输入排队。 当你去阅读下一个号码, scanf试图解析为一个数字,失败,并且不改变返回num 。 当您提示用户,你需要刻录全线飘红。



文章来源: Debugging code in C