-
Case: Problem...
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.
-
you need to "break" out of your case statement at the end of each cases code block or it will just keep executing subsequent lines.
Code:
case 2:
menu();
break;
-
Code:
default:
printf("Not a valid entry\n");
system("pause");
main();
Do not call main recursively. Since all your other functions seem to call the menu function and that just calls main recursively, you are just asking for trouble. The structure of your program should be closer to the following:
Code:
int main(void)
{
int choice, yesno = 2; // Initialize yesno to NO
do
{
choice = menu();
switch( choice )
{
case 1:
add();
break;
case 2:
substract();
break;
case 3:
multiply();
break;
case 4:
devide();
break;
case 5:
yesno = quit();
break;
default:
printf("Not a valid entry\n");
system("pause");
}
} while ( yesno == 2 );
return 0;
}
Rely less on recursion to accomplish things and more on looping. Your menu function should simply display the choices and get the users input and return it to the calling function. Each of the other functions should not call the menu function but simply return to the calling function naturally. Your quit function should ask if the user really wants to quit and then get the input from the user and return that choice back to the calling function.
-
It's not completely working. The choice = menu(); part and the yesno = quit(); part aren't working. It says: "void value not ignored as it ought to be"
Jst.
-
Of course it doesn't work. You have your functions all declared void and then you try to get them to return something. How could that possibly work? If you want them to return a value, don't make them void functions.
Quzah.
-
I was just demonstrating how the program should be structured, you have to actually make more changes to the code to get it working.
-
Although you did help me with improving my C program, it still has the original problem with inputting letters at the scanf("%d", &choice); part..
Jst.
-
That's because you're not supposed to input letters when you tell scanf to expect numbers. :rolleyes: Read the FAQ if you want a better way to read numbers.
Quzah.