I am doing an assignment in C language programming concerning either a WHILE loop, a FOR loop or an IF statement. I have tried all kinds of ways to do it, but failed. I need someone to help me solve this problem. I want to repeat a certain expression until I get a number which fulfills either one of my top two conditions. The code goes like this:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int function;
printf("Please choose a function:\n>>");
scanf("%d",&function);
printf("\n");
if(function==1)
{
system("cls");
printf("Nice");
}
else if(function==2)
{
printf("You chose to exit.\n\n");
return 0;
}
else if(function>2)
{
printf("Error! Please try again.\n\n");
}
else;
return 0;
}
The part that I want to repeat is this until I get either 1 or 2 as my number.
else if(function>2)
{
printf("Error! Please try again.\n\n");
}
Anybody has a way of solving this? It doesn't matter if I have to change this IF statement into a FOR loop or WHILE loop, as long as the results come out as what I mentioned above.