-
menu driven program help
hi, could someone please help me,
i am trying to write a menu driven c program using switch case.
its a program which should have menu as 1.add, 2.sub, 3.mul, 4.div
i wrote this but cannot get output... someone please correct me...
[code]
#include <stdio.h>
void main()
{
int opt,a,b,sum,sub,mul,div;
printf("\n1.add");
printf("\n2.sub");
printf("\n3.mul");
printf("\n4.div");
printf("\nenter your choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
case add:
printf("enter a & b values");
scanf("%d%d",&a,&b);
sum=a+b;
printf("%d",sum);
break;
case 2:
case sub:
printf("enter a & b values");
scanf("%d%d",&a,&b);
sub=a-b;
printf("%d",sub);
break;
default:
printf("enter correct option");
}
}
[\code]
please help
-
I won't fix it for you but I will suggest you take a very close look at the case statements in your switch.
-
Welcome to the forum, vallamrao! ;)
A few suggestions, if I may:
1) Use code tags (highlight your code and click on the # icon in the editing window). It's almost impossible to enjoy studying code that's all smashed up as html text.
2) Instead of repeating scanf() for a and b in your cases, why not do it before the switch case starts, and then inside the case statement, just do the actual operation on a and b.
3) Look at them cases! Well, well. :p :p