问题出现在一个包含switch()的菜单驱动程序中。

huangapple go评论110阅读模式
英文:

problem with a menu driven program consisting of switch()

问题

Here is the translated code portion without any additional information:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int choice, num, i, fact;
  5. while (1) {
  6. printf("\n1-factorial\n");
  7. printf("2-prime\n");
  8. printf("3.odd/even\n");
  9. printf("4-exit\n");
  10. printf("your choice?\n");
  11. scanf("%d", &choice);
  12. switch (choice) {
  13. case 1:
  14. printf("enter a number");
  15. scanf("%d", &num);
  16. fact = 1;
  17. for (i = 1; i <= num; i++) {
  18. fact = fact * i; // factorial of a number
  19. }
  20. printf("factorial value is %d\n", fact);
  21. break; // ends the loop here
  22. case 2:
  23. printf("enter a number");
  24. scanf("%d", &num);
  25. for (i = 2; i < num; i++) {
  26. if (num % i == 0) {
  27. printf("the number is not a prime number\n");
  28. break;
  29. }
  30. if (i == num) {
  31. printf("the number is prime\n");
  32. break;
  33. }
  34. }
  35. case 3:
  36. printf("enter a number");
  37. scanf("%d", &num);
  38. if (num % 2 == 0) {
  39. printf("the number is even\n");
  40. } else {
  41. printf("the number is odd\n");
  42. break;
  43. }
  44. case 4:
  45. exit(0); // terminates program execution included in the <stdlib.h>//
  46. default:
  47. printf("the choice is invalid\a\n"); // \a means alarm
  48. }
  49. }
  50. return 0;
  51. }

If you have any specific questions about the code or need further assistance, please feel free to ask.

英文:
  1. #include&lt;stdio.h&gt;
  2. #include&lt;stdlib.h&gt;
  3. int main()
  4. {
  5. int choice,num,i,fact;
  6. while(1)
  7. {
  8. printf(&quot;\n 1-factorial\n&quot;);
  9. printf(&quot;\n2-prime\n&quot;);
  10. printf(&quot;\n3.odd/even\n&quot;);
  11. printf(&quot;\n4-exit\n&quot;);
  12. printf(&quot;your choice?\n&quot;);
  13. scanf(&quot;%d&quot;,&amp;choice);
  14. switch(choice)
  15. {
  16. case 1:
  17. printf(&quot;enter a number&quot;);
  18. scanf(&quot;%d&quot;,&amp;num);
  19. fact=1;
  20. for(i=1;i&lt;=num;i++)
  21. {
  22. fact=fact*i;//factorial of a number//
  23. }
  24. printf(&quot;factorial value is %d\n&quot;,fact);
  25. break;//ends the loop here//
  26. case 2:
  27. printf(&quot;enter a number&quot;);
  28. scanf(&quot;%d&quot;,&amp;num);
  29. for(i=2;i&lt;num;i++)
  30. {
  31. if(num%i==0)
  32. {
  33. printf(&quot;the number is not a prime number\n&quot;);
  34. break;
  35. }
  36. if(i==num)
  37. {
  38. printf(&quot;the number is prime\n&quot;);
  39. break;
  40. }
  41. }
  42. case 3:
  43. printf(&quot;enter a number&quot;);
  44. scanf(&quot;%d&quot;,&amp;num);
  45. if(num%2==0)
  46. {
  47. printf(&quot;the number is even\n&quot;);
  48. }
  49. else
  50. {
  51. printf(&quot;the number is odd\n&quot;);
  52. break;
  53. }
  54. case 4:
  55. exit(0);//terminates program execution included in the &lt;stdlib.h&gt;//
  56. default:
  57. printf(&quot;the choice is invalid\a\n&quot;);//\a means alarm//
  58. }
  59. }
  60. return 0;
  61. }

i m not sure about the while loop like i have tried for(;;) too but i dont think thats the issue when i input 1 the factorial part works fine
but when i press 2 or 3
it says - enter a number and when i enter one
it again prompts me to enter a number
and then breaks
what maybe the issue here

i tried changing some logics but that didn't help i,googled it and it said something about buffer of \n but i couldn't understand it

答案1

得分: 1

I didn't quite understand your request, but here's the translated code:

  1. 我没有完全理解你的问题,但我在你的代码中看到了一个语法错误。所有的 switch case 后都必须跟着一个 break 语句。在这里,你只有一个 break 语句,而且是在第一个 case 后面。尝试在 case 234 default 的代码块后面加上 *break;* 看看是否可以解决你的问题。
  2. 尝试像这样做:
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. int main()
  6. {
  7. int choice, num, i, fact;
  8. while (1)
  9. {
  10. printf("1-factorial\n");
  11. printf("\n2-prime\n");
  12. printf("\n3.odd/even\n");
  13. printf("\n4-exit\n");
  14. printf("your choice?\n");
  15. scanf("%d", &choice);
  16. switch (choice)
  17. {
  18. case 1:
  19. printf("enter a number");
  20. scanf("%d", &num);
  21. fact = 1;
  22. for (i = 1; i <= num; i++)
  23. {
  24. fact = fact * i; // 阶乘
  25. }
  26. printf("阶乘值是 %d\n", fact);
  27. break; // 在此结束循环
  28. case 2:
  29. printf("enter a number");
  30. scanf("%d", &num);
  31. for (i = 2; i < num; i++)
  32. {
  33. if (num % i == 0)
  34. {
  35. printf("这个数字不是质数\n");
  36. break;
  37. }
  38. if (i == num)
  39. {
  40. printf("这个数字是质数\n");
  41. break;
  42. }
  43. }
  44. break;
  45. case 3:
  46. printf("enter a number");
  47. scanf("%d", &num);
  48. if (num % 2 == 0)
  49. {
  50. printf("这个数字是偶数\n");
  51. }
  52. else
  53. {
  54. printf("这个数字是奇数\n");
  55. break;
  56. }
  57. break;
  58. case 4:
  59. exit(0); // 终止程序执行,需要包含在 <stdlib.h> 中
  60. break;
  61. default:
  62. printf("选择无效\a\n"); // \a 表示警告
  63. break;
  64. }
  65. }
  66. return 0;
  67. }
  68. 还要注意,阶乘的情况在一定范围的整数后将无法正常工作,因为阶乘值会超过 int 类型的大小。
英文:

I didn't quite understand your question, but I see a syntax error in your code. All switch cases must be followed by a break statement. Here as you can see there's only 1 break statement, that's after the first case. Try putting break; after the block of codes of case 2,3,4 and default and see if that resolves your problem

Try something like this:

  1. #include&lt;stdio.h&gt;
  2. #include&lt;stdlib.h&gt;
  3. int main()
  4. {
  5. int choice,num,i,fact;
  6. while(1){
  7. printf(&quot;1-factorial\n&quot;);
  8. printf(&quot;\n2-prime\n&quot;);
  9. printf(&quot;\n3.odd/even\n&quot;);
  10. printf(&quot;\n4-exit\n&quot;);
  11. printf(&quot;your choice?\n&quot;);
  12. scanf(&quot;%d&quot;,&amp;choice);
  13. switch(choice)
  14. {
  15. case 1:
  16. printf(&quot;enter a number&quot;);
  17. scanf(&quot;%d&quot;,&amp;num);
  18. fact=1;
  19. for(i=1;i&lt;=num;i++)
  20. {
  21. fact=fact*i;//factorial of a number//
  22. }
  23. printf(&quot;factorial value is %d\n&quot;,fact);
  24. break;//ends the loop here//
  25. case 2:
  26. printf(&quot;enter a number&quot;);
  27. scanf(&quot;%d&quot;,&amp;num);
  28. for(i=2;i&lt;num;i++)
  29. {
  30. if(num%i==0)
  31. {
  32. printf(&quot;the number is not a prime number\n&quot;);
  33. break;
  34. }
  35. if(i==num)
  36. {
  37. printf(&quot;the number is prime\n&quot;);
  38. break;
  39. }
  40. }
  41. break;
  42. case 3:
  43. printf(&quot;enter a number&quot;);
  44. scanf(&quot;%d&quot;,&amp;num);
  45. if(num%2==0)
  46. {
  47. printf(&quot;the number is even\n&quot;);
  48. }
  49. else
  50. {
  51. printf(&quot;the number is odd\n&quot;);
  52. break;
  53. }
  54. break;
  55. case 4:
  56. exit(0);//terminates program execution included in the &lt;stdlib.h&gt;//
  57. break;
  58. default:
  59. printf(&quot;the choice is invalid\a\n&quot;);//\a means alarm//
  60. break;
  61. }
  62. }
  63. return 0;
  64. }

Also note the factorial case won't work after a certain set of integers since the factorial value exceeds the size of int

huangapple
  • 本文由 发表于 2023年6月12日 01:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定