首页 > 解决方案 > 如何用 C 创建菜单?

问题描述

我用 Switch 创建了一个菜单,但是如果情况 1 的操作错误,我想按 N 返回到情况 1 的开头。我想使用 IF-Else 来执行此操作,我想在按 N 后按 1 并返回开始。

编辑:当我按 N 键时,我需要写 N 两次才能使该过程发生,我该如何解决?

    switch (menu){

case 1:

do {
    redo = 0;

    printf("\n\nPlease write Worker ID Number : ");
    scanf("%s", &file_name);
    strcpy(out3, file_name);
    strcat(out3, filex);
    fout = fopen(out3, "wb");


    printf("Please Enter The Worker's First Name : ");
    scanf("%s", &user.firstName);

    printf("Please Enter The Worker's Last Name : ");
    scanf("%s", &user.lastName);

    printf("Please Enter Worker's Identification Number Again : ");
    scanf("%s", &user.UserID);

    printf("Please Enter Worker's Gender : ");
    scanf("%s", &user.gender);      

    printf("Please Enter The Worker's Birthday Date (dd/mm/yyyy) Format : ");
    scanf("%d / %d / %d", &user.day, &user.month, &user.year);

    printf("Please Enter The Nationality Of The Worker's : ");
    scanf("%s", &user.nation);  

    printf("Please Enter The Worker's Start Date (dd/mm/yyyy) Format : ");
    scanf("%d / %d / %d", &user.Sday, &user.Smonth, &user.Syear);

    printf("\n\n\tWorker Information is :\nFirst Name : %s\nLast Name : %s \nIdentification Number : %s \nGender : %s \nBirthday Date : %d/%d/%d\nNationality : %s\nDate of Start : %d/%d/%d", user.firstName, user.lastName, user.UserID, user.gender, user.day, user.month, user.year, user.nation, user.Sday, user.Smonth, user.Syear);


    for (;;) {

    printf("\n\n\tDo You Approve Your Information ? Yes or No Enter Y or N\n");
    scanf("%s", &answer);       
    if (scanf(" %c", &answer)  != 1)
    return -1;



    if(answer == 'Y' || answer == 'y'){

        printf("\nWorker's information has been saved!\n");

        fprintf(fout, "Worker's ID : %s\nWorker's Name : %s %s\nWorker's Birthday Date :  %d/%d/%d\nWorker's Gender : %s\nWorker's Nationality : %s\nWorker's Start Date : %d/%d/%d", user.UserID, user.firstName, user.lastName, user.day, user.month, user.year, user.gender, user.nation, user.Sday, user.Smonth, user.Syear);
        fclose(fout);               


        break;

    }


    else if(answer == 'N' || answer == 'n'){

        printf("Press 1 to re-enter your information : ");
        scanf("%d",&answer1);

        redo = 1;
        break;  

        }


    }
    } while (redo);

标签: cswitch-statementcase

解决方案


我如何解决它?

你能解释一下吗?–

看那个有2个循环的例子

  • 允许重新输入工人信息的“do while”
  • 在答案不是 y Y n 或 N 的情况下再次询问的“for”

我使用两种循环让你看到区别

#include <stdio.h>

typedef struct User {
  char firstName[32];
} User;

int main()
{
  User user;
  char answer;
  int redo;

  do {
    redo = 0;

    printf("Please Enter The Worker's First Name : ");
    if (scanf("%31s", user.firstName) != 1)
      /* EOF */
      return -1;

    printf("\n\n\tWorker Information is :\nFirst Name : %s\n", user.firstName);

    for (;;) {
      printf("\n\n\tDo You Approve Your Information ? Yes or No Enter Y or N\n");
      if (scanf(" %c", &answer)  != 1)
        /* EOF */
        return -1;

      if(answer == 'Y' || answer == 'y'){
        printf("\nWorker's information has been saved!\n");
        break;
      }
      else if(answer == 'N' || answer == 'n') {
        redo = 1;
        break;
      }
      puts("invalid answer");
    }
  } while (redo);

  puts("done");
  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
Please Enter The Worker's First Name : aze


    Worker Information is :
First Name : aze


    Do You Approve Your Information ? Yes or No Enter Y or N
Y

Worker's information has been saved!
done
pi@raspberrypi:/tmp $ ./a.out
Please Enter The Worker's First Name : aze


    Worker Information is :
First Name : aze


    Do You Approve Your Information ? Yes or No Enter Y or N
N
Please Enter The Worker's First Name : aze


    Worker Information is :
First Name : aze


    Do You Approve Your Information ? Yes or No Enter Y or N
a
invalid answer


    Do You Approve Your Information ? Yes or No Enter Y or N
Y

Worker's information has been saved!
done
pi@raspberrypi:/tmp $

其中:

  • 为什么你在你的 中明确给出字符串的地址scanf("%s"..)?接收者必须是一个字符数组,在这种情况下“&”是无用的,否则你的代码是无效的
  • 我鼓励您限制scanf将写入的字符数,以避免在它从接收器中写入时出现未定义的行为,这就是为什么我使用scanf("%31s", user.firstName)知道firstNamechar[32]
  • 我还鼓励您检查所有scanf的结果,以确保输入正确
  • scanf(" %c", &answer)用来获取一个字符,前面的空格%c允许绕过分隔符,包括仍然缓冲的分隔符,如在前一个输入末尾输入的换行符

在您的版本之后:

警告现在你有

 scanf("%s", &answer);
 if (scanf(" %c", &answer) != 1)

必须删除第一个scanf 。

接近尾声

scanf("%d",&answer1);

但你不使用 answer1


推荐阅读