0

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.

4

5 回答 5

3

use this may help you.

for(i=1;i<10;i++)
{
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");
}
}

you can put any number depanding upon your requirment..I have used 10 in for loop. You can use as per your requirment

于 2013-03-09T13:37:35.407 回答
0

Put the following in a loop

while(1)
{
    printf("Please choose a function:\n>>");
    scanf("%d",&function);
    printf("\n");
    if(function==1)
    {
        system("cls");
        printf("Nice");
        break;
    }
    else if(function==2)
    {
        printf("You chose to exit.\n\n");
        return 0;
    }
    else if(function>2)
    {
        printf("Error! Please try again.\n\n");
    }
}
于 2013-03-09T13:33:44.353 回答
0

Try it with a while loop and a switch statement.

Something like so (not sure if it complies, just to show you what I mean):

function = 0;
do
{
    printf("Please choose a function:\n>>");
    scanf("%d", &function);
    switch(function)
    {
        case 1:
            /* do your stuff */
            break;
        default:
            fprintf(stderr, "Error! Please try again.\n\n");
            break;
    }
} while(function != 2)
于 2013-03-09T13:36:08.500 回答
0

Assuming you're allowed more than one of while/for/if, how about a mixture of do ... while and if

int function;
do {
    while (scanf("Please choose a function:\n>>%d", &function) != 1)
        ;
    printf("\n");
    if(function==1) {
        system("cls");
        printf("Nice");
    }
    else if(function>2) {
        printf("Error! Please try again.\n\n");
    }
} while (function != 2);
printf("You chose to exit.\n\n");
return 0;
于 2013-03-09T13:38:19.863 回答
0

use can use a do while loop

int main()

int function;
do {
  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  {
      printf("Error! Please try again.\n\n");
  }
} while (function > 2)
return 0;

}

于 2013-03-09T13:45:01.390 回答