英文:
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<stdio.h>
#include<stdlib.h>
int main()
{
int choice,num,i,fact;
while(1)
{
printf("\n 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;//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;
}
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 2、3、4 和 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<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;//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;
}
}
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;
}
break;
case 4:
exit(0);//terminates program execution included in the <stdlib.h>//
break;
default:
printf("the choice is invalid\a\n");//\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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论