gpt4 book ai didi

c - 开关盒在 C 中无法正常工作

转载 作者:行者123 更新时间:2023-12-02 22:40:44 26 4
gpt4 key购买 nike

在我的程序中有一个小问题。

当我按 2 或 3 或 4 时,它会正确显示,但在那之后当我按 a 或 b 或 c 等,它会显示前一个结果而不是打印 Invalid 选项。

我该如何解决这个问题?

    #include <stdio.h>
#include <string.h>

typedef struct vehicle
{
char name[100];
char lice_no[25];
int vehicle_type;
char cmpny_name[100];
int menu_year;
}record;

int main(void)
{
int i,choice;
FILE *fp1,*fp2;
char oname[100];
record det,det1;
int recsize;
char c;

fp1 = fopen("record.dat" , "r+");
if(fp1 == NULL)
{
fp1 = fopen("record.dat" , "w+");
if(fp1 == NULL)
{
printf("error in opening file : \n");
return -1;
}
}
recsize = sizeof(det);

do
{
printf("\t\"enter the choice\"\n");

printf("1 : adding the record\n");
printf("2 : delete the record\n");
printf("3 : editing the record\n");
printf("4 : display the record\n");
printf("5 : exit the program\n");


fflush(stdin);
scanf("%d" , &choice);
scanf("%c" , &c);

switch(choice)
{
case 1 :
{
printf("In this add logic\n")
break;
}
case 2 :
{
printf("In this case delete logic\n");
break;
}
case 3 :
{
printf("In this case edit logic\n");
break;
}
case 4 :
{
printf("display logic\n");
break;
}
case 5 :
{
printf("exit logic\n");
break;
}
default :
{
printf("\"Invalid option\"\n");
break;
}
}
}
while(1);
return 0;
}

最佳答案

看起来您正在将数字输入 Choice,将字符输入 c。但是您只在开关中使用 Choice var,您永远不会检查 C var。

所以本质上,如果您点击一个字母,它会将其存储在 C 变量中,然后再次使用 Choice 中的旧值。

关于c - 开关盒在 C 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10814580/

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