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

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

problem with a menu driven program consisting of switch()

问题

Here is the translated code portion without any additional information:

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

int main() {
    int choice, num, i, fact;
    while (1) {
        printf("\n1-factorial\n");
        printf("2-prime\n");
        printf("3.odd/even\n");
        printf("4-exit\n");
        printf("your choice?\n");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("enter a number");
                scanf("%d", &num);
                fact = 1;
                for (i = 1; i <= num; i++) {
                    fact = fact * i; // factorial of a number
                }
                printf("factorial value is %d\n", fact);
                break; // ends the loop here

            case 2:
                printf("enter a number");
                scanf("%d", &num);
                for (i = 2; i < num; i++) {
                    if (num % i == 0) {
                        printf("the number is not a prime number\n");
                        break;
                    }
                    if (i == num) {
                        printf("the number is prime\n");
                        break;
                    }
                }
            case 3:
                printf("enter a number");
                scanf("%d", &num);
                if (num % 2 == 0) {
                    printf("the number is even\n");
                } else {
                    printf("the number is odd\n");
                    break;
                }
            case 4:
                exit(0); // terminates program execution included in the <stdlib.h>//
            default:
                printf("the choice is invalid\a\n"); // \a means alarm
        }
    }
    return 0;
}

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

英文:
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
int main()
{
int choice,num,i,fact;
while(1)
{
printf(&quot;\n 1-factorial\n&quot;);
printf(&quot;\n2-prime\n&quot;);
printf(&quot;\n3.odd/even\n&quot;);
printf(&quot;\n4-exit\n&quot;);
printf(&quot;your choice?\n&quot;);
scanf(&quot;%d&quot;,&amp;choice);
switch(choice)
{
case 1:
printf(&quot;enter a number&quot;);
scanf(&quot;%d&quot;,&amp;num);
fact=1;
for(i=1;i&lt;=num;i++)
{
fact=fact*i;//factorial of a number//
}
printf(&quot;factorial value  is %d\n&quot;,fact);
break;//ends the loop here//
case 2:
printf(&quot;enter a number&quot;);
scanf(&quot;%d&quot;,&amp;num);
for(i=2;i&lt;num;i++)
{
if(num%i==0)
{
printf(&quot;the number is not a prime number\n&quot;);
break;
}
if(i==num)
{
printf(&quot;the number is prime\n&quot;);
break;
}
}
case 3:
printf(&quot;enter a number&quot;);
scanf(&quot;%d&quot;,&amp;num);
if(num%2==0)
{
printf(&quot;the number is even\n&quot;);
}
else
{
printf(&quot;the number is odd\n&quot;);
break;
}
case 4:
exit(0);//terminates program execution included in the &lt;stdlib.h&gt;//
default:
printf(&quot;the choice is invalid\a\n&quot;);//\a means alarm//
}
}
return 0;
}

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:

我没有完全理解你的问题,但我在你的代码中看到了一个语法错误。所有的 switch case 后都必须跟着一个 break 语句。在这里,你只有一个 break 语句,而且是在第一个 case 后面。尝试在 case 234  default 的代码块后面加上 *break;* 看看是否可以解决你的问题。

尝试像这样做:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int choice, num, i, fact;
    while (1)
    {
        printf("1-factorial\n");
        printf("\n2-prime\n");
        printf("\n3.odd/even\n");
        printf("\n4-exit\n");
        printf("your choice?\n");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            printf("enter a number");
            scanf("%d", &num);
            fact = 1;
            for (i = 1; i <= num; i++)
            {
                fact = fact * i; // 阶乘
            }
            printf("阶乘值是 %d\n", fact);
            break; // 在此结束循环

        case 2:
            printf("enter a number");
            scanf("%d", &num);
            for (i = 2; i < num; i++)
            {
                if (num % i == 0)
                {
                    printf("这个数字不是质数\n");
                    break;
                }
                if (i == num)
                {
                    printf("这个数字是质数\n");
                    break;
                }
            }
            break;

        case 3:
            printf("enter a number");
            scanf("%d", &num);
            if (num % 2 == 0)
            {
                printf("这个数字是偶数\n");
            }
            else
            {
                printf("这个数字是奇数\n");
                break;
            }
            break;

        case 4:
            exit(0); // 终止程序执行,需要包含在 <stdlib.h> 中
            break;
        default:
            printf("选择无效\a\n"); // \a 表示警告
            break;
        }
    }
    return 0;
}

还要注意,阶乘的情况在一定范围的整数后将无法正常工作,因为阶乘值会超过 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:

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
int main()
{
int choice,num,i,fact;
while(1){
printf(&quot;1-factorial\n&quot;);
printf(&quot;\n2-prime\n&quot;);
printf(&quot;\n3.odd/even\n&quot;);
printf(&quot;\n4-exit\n&quot;);
printf(&quot;your choice?\n&quot;);
scanf(&quot;%d&quot;,&amp;choice);
switch(choice)
{
case 1:
printf(&quot;enter a number&quot;);
scanf(&quot;%d&quot;,&amp;num);
fact=1;
for(i=1;i&lt;=num;i++)
{
fact=fact*i;//factorial of a number//
}
printf(&quot;factorial value  is %d\n&quot;,fact);
break;//ends the loop here//
case 2:
printf(&quot;enter a number&quot;);
scanf(&quot;%d&quot;,&amp;num);
for(i=2;i&lt;num;i++)
{
if(num%i==0)
{
printf(&quot;the number is not a prime number\n&quot;);
break;
}
if(i==num)
{
printf(&quot;the number is prime\n&quot;);
break;
}
}
break;
case 3:
printf(&quot;enter a number&quot;);
scanf(&quot;%d&quot;,&amp;num);
if(num%2==0)
{
printf(&quot;the number is even\n&quot;);
}
else
{
printf(&quot;the number is odd\n&quot;);
break;
}
break;
case 4:
exit(0);//terminates program execution included in the &lt;stdlib.h&gt;//
break;
default:
printf(&quot;the choice is invalid\a\n&quot;);//\a means alarm//
break;
}
}
return 0;
}

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:

确定