I am currently working my way through the C tutorials and have a question about adding a loop to the switch..case tutorial code.
I have tried to add a loop so that if the selection is less than or equal to 4, the switch case is offered, else- default option prints error message.
Will this loop back to the top of the program and run it again?
Anyway I get an error "expected expression before 'else'", am I close with my code or not?
Code:
#include <stdio.h>
int playgame();
int loadgame();
int multiplayer();
int main()
{
int input;
printf("1. Playgame\n");
printf("2. Loadgame\n");
printf("3. Multiplayer\n");
printf("4. Exit\n");
printf("Selection: ");
scanf ("%d", &input);
if (input <= 4){
switch (input){
case 1:
playgame();
break;
case 2:
loadgame();
break;
case 3:
multiplayer();
break;
case 4:
printf("Thank you for playing\n");
break;
}
else{
default:
printf ("Bad input, make another selection: \n");
scanf("%d", &input)
break;
}
}
getchar();
}
int playgame(){
printf("you are playing\n");
return 0;
}
int loadgame(){
printf("game is loading\n");
}
int multiplayer(){
printf("Find a friend to play with\n");
}