Reputation: 355
I want to return to the start of the loop and make the user enter another input.
This is what I have but I keep getting the error message repeating over and over again.
How would I return so the user can enter a argument
printf("Enter a option\n");
scanf("%d", &option);
while (option != 1 || option != 2 || option != 3 || option != 4)
{
if (option == 1)
{
option1(...);
break;
}
else if (option == 2)
{
option2(...);
break;
}
else if (option == 3)
{
option3(...);
break;
}
else if (option == 4)
{
option4(...);
break;
}
else
{
printf("\nPlease enter a correct option\n");
continue;
}
}
Upvotes: 1
Views: 17761
Reputation: 23
you should Change your loop to do while.
do
{
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
option1(...);
}
else if(option == 2){
option2(...);
}
else if(option == 3){
option3(...);
}
else if(option == 4){
option4(...);
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}while(option != 1 || option != 2 || option != 3 || option != 4);
Upvotes: 0
Reputation: 5318
int option;
while(1)
{
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
printf("option1\n");
break;
}
else if(option == 2){
printf("option2\n");
break;
}
else if(option == 3){
printf("option3\n");
break;
}
else if(option == 4){
printf("option4\n");
break;
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}
Upvotes: 1
Reputation: 2181
use a do..while with printf/scanf inside.
do {
printf("Enter a option\n");
scanf("%d",&option);
your if/else statements here...
} while(true);
Upvotes: 0
Reputation: 11954
Just rearrange the logic, for example like:
do {
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
option1(...);
break;
}
else if(option == 2){
option2(...);
break;
}
else if(option == 3){
option3(...);
break;
}
else if(option == 4){
option4(...);
break;
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}
while(true);
Now your code does the scanf
only once and then iterates over the same result, instead you must read the value each time you begin the loop.
Upvotes: 4