You didn't use the switch statement correctly, it's meant to be something like this:
Code:
switch(choice)
{
case 1: printf("You have entered choice 1\n");
break;
case 2: printf("You have entered choice 2\n");
break;
default: printf("Invalid choice!\n");
break;
}
And also don't use void main() use int main(void) or int main(int argc, char** argv) instead.
This might give you a better overview, you can add in error control if you need it.
Code:
#include <stdio.h>
void load_menu(void);
void sum(void);
void rest(void);
int main(int argc, char** argv)
{
load_menu();
return 0;
}
void load_menu(void)
{
int choice;
do
{
printf("Menu\n\n");
printf("1. Sum\n");
printf("2. Rest\n");
printf("3. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: sum();
break;
case 2: rest();
break;
case 3: printf("Quitting program!\n");
exit(0);
break;
default: printf("Invalid choice!\n");
break;
}
} while (choice != 3);
}
void sum(void)
{
int num1, num2;
int ch;
printf("Enter number 1: ");
scanf("%d",&num1);
printf("Enter number 2: ");
scanf("%d",&num2);
printf("\nThe sum of the numbers was: %d",num1+num2);
/* Flushes input buffer from the newline from scanf() */
while ( (ch = getchar()) != '\n' && ch != EOF) ;
printf("\n\nPress ENTER to continue.");
while ( (ch = getchar()) != '\n' && ch != EOF)
;
return;
}
void rest(void)
{
int ch;
printf("Sleepy sleepy... zZZzZzZz\n");
printf("You now feel awake again!\n");
/* Flushes input buffer */
while ((ch = getchar()) != '\n' && ch != EOF) ;
printf("\n\nPress ENTER to continue.");
while ((ch = getchar()) != '\n' && ch != EOF)
;
return;
}