简体   繁体   中英

yes or no to exit a switch statement

Is it possible to create yes or no function to be called to exit a switch statement. if (y)es is hit it would exit. If (n)o is hit it would loop back to the switch's options. if so how would this be done. This is what I have so far.

I hope this helps to clarify what I am trying to do

int yes_no(void) {

    scanf("%d", &option);

    while (option == 'y' || option == 'Y' || option == 'n' || option == 'N') {
        if (option == 'y' || option == 'Y') {
            return 1;
        } else
        if (option == 'n' || option == 'N') {
            return 0;
        } else {
            printf("Only (Y)es or (N)o are acceptable");
        }
    }
}

int main(void) {
    int select;

    do {
        printf("0 exit");
        printf("1 hello");

        scanf("%d", &select);

        switch (select) {
          case 0:
            printf("Exit the program? (Y)es or (N)o :");
            yes_no(); // if (n)o is hit it will loop back to the menu
            break;
          case 1:
            printf("hello how r u doing");
            break;
          default:
            printf("not accepted number");
        }
    } while (select != 0);
}

Consider the following yes_no function.

int yes_no() {
  char option;

  while (1) {
    printf("(y/n): ");
    if (scanf(" %c", &option) != 1) {
      printf("Error occurred while reading option.\n");
      continue;
    }

    if (option == 'y' || option == 'Y') {
      return 1;
    } else if (option == 'n' || option == 'N') {
      return 0;
    } else {
      printf("Only (y)es or (n)o are acceptable.\n");
    }
  }
}

You have a return value of 1 if given a yes input, and 0 in case of no . With this in mind, in the case 0 code block of the switch statement, the return value of this function can be captured, which can then be used to either break the loop or do nothing. For example:

int main(void) {
  int select;
  int continue_loop = 1;

  printf("Press 0 to exit\n");
  printf("Press 1 for hello\n\n");

  while (continue_loop) {
    printf("(0/1): ");
    if (scanf(" %d", &select) != 1) {
      printf("Error occurred while reading number.\n");
      continue;
    }

    switch (select) {
      case 0:
        printf("Exit the program? (y)es or (n)o;\n");
        int make_sure = yes_no();  // yes_no() returns 1 if 'yes', and 0 if 'no'
        // If 'yes', break while loop by setting continue_loop to 0.
        // Do nothing in case of 'no'
        if (make_sure == 1) {
          continue_loop = 0;
        }
        break;
      case 1:
        printf("Hello, how are you doing?\n");
        break;
      default:
        printf("Number not accepted.\n");
    }
  }

  return 0;
}

This should do the job, if I get your question correctly:

int yes_or_no()
{
  do
  {
    char option = 0;
    scanf(" %c", &option);
    if (option == 'y' || option == 'Y')
      return 1;
    if (option == 'n' || option == 'N')
      return 0;
    printf("Only (Y)es or (N)o are acceptable");
  }
  while( 1 );
}

with switch you can perform your code better:

int yes_no(void) {
scanf("%d", &option);
while (1){
switch(option){
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf("invalid input.try again");
scanf("%d", &option);
}
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM