Code:
#include <stdio.h>
void menu();
void add();
void substract();
void multiply();
void devide();
void quit();
int main(void)
{
menu();
}
void menu()
{
int choice;
printf("\n############################################\n");
printf("\n\t\tCalculator\n");
printf("\n############################################\n\n");
printf("\n [1] Add\n [2] Substract\n [3] Multiply\n [4] Devide\n [5] Quit\n");
printf("\n\n\nChoice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add();
case 2:
substract();
case 3:
multiply();
case 4:
devide();
case 5:
quit();
default:
printf("Not a valid entry\n");
system("pause");
main();
}
}
void add()
{
int n1, n2, ans;
printf("Enter the first number: ");
scanf("%d", &n1);
printf("Enter the second number: ");
scanf("%d", &n2);
ans=n1+n2;
printf("%d + %d = %d\n", n1, n2, ans);
system("pause");
menu();
}
void substract()
{
int n1, n2, ans;
printf("Enter the first number: ");
scanf("%d", &n1);
printf("Enter the second number: ");
scanf("%d", &n2);
ans=n1-n2;
printf("%d - %d = %d\n", n1, n2, ans);
system("pause");
menu();
}
void multiply()
{
int n1, n2, ans;
printf("Enter the first number: ");
scanf("%d", &n1);
printf("Enter the second number: ");
scanf("%d", &n2);
ans=n1*n2;
printf("%d x %d = %d\n", n1, n2, ans);
system("pause");
menu();
}
void devide()
{
int n1, n2, ans;
printf("Enter the first number: ");
scanf("%d", &n1);
printf("Enter the second number: ");
scanf("%d", &n2);
ans=n1/n2;
printf("%d : %d = %d\n", n1, n2, ans);
system("pause");
menu();
}
void quit()
{
int yesno;
printf("\nAre u sure you want to exit?\n");
printf("\n[1] Yes\n[2] No\n");
printf("\n");
printf("\nChoice: ");
scanf("%d", &yesno);
switch(yesno) {
case 1:
printf("\nGoodbye..\n");
system("pause");
exit(0);
case 2:
menu();
default:
printf("Not a valid entry\n");
system("pause");
quit();
}
}
The quit and menu function both have the switch { case 1: } system, when I want to choose an option, in both quit and menu and I input a letter instead of a number it sais Not a valid entry and does the system("pause") thing after that it returns to the function that I defined and it keeps saying the same thing, whatever button I press, it just keeps saying the same thing...
Btw: Any tips and improvements of my code are welcome .
Tnx
Jst.