gpt4 book ai didi

c - 如何用C创建菜单?

转载 作者:行者123 更新时间:2023-11-30 18:44:59 24 4
gpt4 key购买 nike

我用 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);

最佳答案

how do I fix it?

can you explain a little more explanatory? –

看看那个有 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

关于c - 如何用C创建菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56008442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com