I'm having a problem understanding how to get my while
loop to simply output a message saying "Invalid Input" and asking for a new question from the user unless he chooses number 1
or 2
in the list. What happens if you for example input : asdas
instead of a integer the program never stops looping.
What I would like to happen is for the program to tell the user to enter a new number from 1-2
instead of simply stopping running which i can achieve by setting the default in the switch to exit(0);
or runSystem = false;
For example:
CMD Says enter 1-2
the user enters : asdaf
(never stops looping) as in current situation.
What I want is: asdf
and then it says "enter a new choice" and waits for a correct answer.
What bothers me is the fact that the program will do as i want it to if you enter an invalid number for example: 12312312
and ask for a new entry but it doesn't work with string input.
Code:
#include <stdio.h>
#include <stdbool.h>
int main(int argc, char **argv) {
int userinput;
int runSystem = true;
void options() {
printf("<========Welcome to the program, please make a choice========> \n\n");
printf("1: Say Hello\n");
printf("2: Say GoodBye\n");
printf("Please enter a choice:");
scanf("%d", &userinput);
}
while (runSystem) {
options();
switch(userinput) {
case 1: printf("Hello!\n");
break;
case 2: printf("GoodBye!\n");
break;
case 3: printf("Invalid, try again\n");
break;
default:
break;
}
}
return 0;
}