gpt4 book ai didi

c - 当我在我的程序中选择某个选项时,我不断得到一个无限循环

转载 作者:太空宇宙 更新时间:2023-11-04 08:48:08 25 4
gpt4 key购买 nike

我有这个程序要求用户输入信息并将其存储在文件中,并允许您通过将总工资设置为 0 来编辑条目或添加新条目或删除条目。

但是,当我尝试修改姓名时,它不会修改,而当我尝试修改性别时,它会导致无限循环,任何人都可以告诉我哪里出了问题吗?

而且我认为我在循环中所做的 break 语句有问题,在此先感谢

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int employee_number;
char employee_name[20];
char employee_sex;
int employee_gross_salary;

}information;
int main()
{//open main function
information customer;
int i;
int choice;
int number;
int choice2;
int number2;
FILE *fptr = fopen("emp.dat", "wb+");

//asking user to enter atleast 5 customers into datarecords
for(i = 0; i<1;i++)
{//open for
printf("enter employee's number\n");
scanf("%d",&customer.employee_number);
getchar();
printf("enter the employee's name\n");
scanf("%s", customer.employee_name);
getchar();
printf("enter employee's gender\n");
scanf("%d",&customer.employee_sex);
getchar();
printf("enter employee's salary\n");
scanf("%d",&customer.employee_gross_salary);
getchar();
fwrite(&customer,sizeof(customer),1,fptr);
}//close for

for(;;)
{//open for
printf("\n what would you like to do\n1]Add entry\n 2]Delete entry \n3]Modify entry\n4]view entries\n5]exit\n");
scanf("%d", &choice);
if(choice == 5)
{break;}
else if(choice == 1)
{//open else if
fseek(fptr,0, SEEK_END);// check the parameters here
printf("enter new employee's number\n");
scanf("%d",&customer.employee_number);
getchar();
printf("enter the new employee's name\n");
scanf("%s", customer.employee_name);
getchar();
printf("enter new employee's gender\n");
scanf("%d",&customer.employee_sex);
getchar();
printf("enter new employee's salary\n");
scanf("%d",&customer.employee_gross_salary);
getchar();
fwrite(&customer,sizeof(customer),1,fptr);
continue;

}//close else if
else if( choice == 2)
{//open else if
printf("enter the employee number of person\n");
scanf("%d",&number);
fseek(fptr,0,SEEK_SET);
while((fread(&customer,sizeof(customer), 1,fptr))!=NULL)
{//open while

if(customer.employee_number == number)
{//open if
customer.employee_gross_salary = 0;
}//close if

}//close while
continue;
}//clsoe else if
else if(choice == 3)
{//open else if
printf("enter the employee number of the employee you would like to modify\n");
scanf("%d",&number2);
printf("what would you like to modify\n");
scanf("%d", &choice2);
fseek(fptr,0,SEEK_SET);
while((fread(&customer, sizeof(customer),1,fptr))!= NULL)
{//open while within else if
//1 to midify name, 2 to modify gender 3 for salary
if(customer.employee_number == number2)
{//open if

if(choice2 == 1)
{

printf("enter new name\n");
scanf("%s",customer.employee_name );
break;
}
else if(choice2 == 2)
{
printf("enter new gender");
scanf("%d", &customer.employee_sex);
break;
}
else if(choice2 == 3)
{
printf("enter new gross salary\n");
scanf("%d", &customer.employee_gross_salary);
break;
}

}//close if

}//close while within else if
continue;
}//close else if
else if(choice == 4)
{
fseek(fptr,0,SEEK_SET);
while((fread(&customer,sizeof(customer),1,fptr))!= NULL)
printf("\n%d\t%s\t%c\t%d\n", customer.employee_number,customer.employee_name,customer.employee_sex,customer.employee_gro ss_salary);
continue;
}
}//close for
return 0;
}//close main function

最佳答案

这不是调试的答案,只是重构代码和 future 写作的一些建议:

1.避免使用 break 和 continue,它们是 flow breaker、bug sources、bad 和 evil,go to 是一样的,它们在这里用于特定的 no-other-way 情况。

你可以这样做:

int end = 0,
choice = 0;

do
{
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1){}
if(choice == 1)
{
//Do stuff
}
else if (choice == 2)
{
//Do other stuff
}
else if (choice == 3)
{
//Do another stuff
}
else
{
end = 1;
}
}while(end == 0);
return 0;

没有继续,没有中断,更容易修改,更容易编写,更容易阅读,更短,两个词:更好

2。总是用英文写,你有一个完整的键盘,不用按字母付费,输入整个单词几乎一样快,并帮助很多其他人理解。此外,它将帮助您在编写文本或代码时减少错误。

3。如果它们属于同一类型,您可以一次声明多个变量:

int var1;
int var2;
int var3;
...

又长又重复​​,你可以这样写:

int var1,
var2,
var3;

一个好习惯可以是总是初始化变量,它有助于防止一些错误:

int var1=0,
var2=0,
var3=0;

4.每当你使用一个函数,测试它的返回值时,会因为认为“它是一个 stdio 函数,它是防错的”而产生很多错误。例如,您对 emp.dat 的打开。它可能会失败(实际上会在某个时候失败)。

FILE *fptr = fopen("emp.dat", "wb+");
if (fptr == NULL)
{
fprintf(stderr, "Error while opening emp.dat\n");
return -1;
}

5.如果你是初学者(这并不可耻,每个人都从某个时候开始,我们可以说即使在编码 10 多年之后每个人仍然在开始),先写你的算法,然后编码。示例:

//Get user's choice
//If user choice is do stuff
//Do stuff
//If it is do other stuff
//Do other stuff
//If it is do another stuff
//Do another stuff
//Else if he want to quit
//Quit

然后变成

int choice=0, //User's choice
end=0; //End of program

do
{
//Get user's choice
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1){}
//If user choice is do stuff
if(choice == 1)
{
//Do stuff
}
//If it is do other stuff
else if(choice == 1)
{
//Do other stuff
}
//If it is do another stuff
else if(choice == 1)
{
//Do another stuff
}
//Else if he want to quit
else
{
//Quit
end = 1;
}
}while (end == 0);
return 0;

它还可以防止您在数周后对您的代码进行注释,因为您不再知道为什么要这样做或那样做。

6.记录、记录、记录,尤其是在调试时!如果你愿意,你可以把它放在 stderr 上,这样你就可以把它从你的输出中分离出来。示例:

int end = 0,
choice = 0;

fprintf(stderr, "Start\n");

do
{
fprintf(stderr, "\tBegin loop\n");
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1){}
fprintf(stderr, "\tChoice is: %d\n", choice);
if(choice == 1)
{
fprintf(stderr, "\t\tStarting do stuff\n");
//Do stuff
fprintf(stderr, "\t\tEnding do stuff\n");
}
else if (choice == 2)
{
fprintf(stderr, "\t\tStarting do other stuff\n");
//Do other stuff
fprintf(stderr, "\t\tEnding do other stuff\n");
}
else if (choice == 3)
{
fprintf(stderr, "\t\tStarting do another stuff\n");
//Do another stuff
fprintf(stderr, "\t\tEnding do another stuff\n");
}
else
{
fprintf(stderr, "\t\tEnd order\n");
end = 1;
}
fprintf(stderr, "\tEnd of loop\n");
}while(end == 0);
fprintf(stderr, "End\n");
return 0;

所以你知道你的程序现在何时何地,这对调试有很大的帮助!

目前就想到这么多,希望对你有帮助。

此外,欢迎来到 Stack Overflow。

编辑:

感谢chunk,还有一个重点:

7.始终检查 scanf 以获取有效的用户输入。用户的输入可以而且将是几乎所有的东西,并且在某些时候会不是你想的那样,总是测试它。 (它不仅对 (f)scanf 有效,而且对您从其他来源获取数据但您自己的源代码以外的所有方式都有效)

int check = 0;
fprintf(stderr, "\tBegin loop\n");
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
check = fscanf(stdin, "%d", &choice);
if(check != 1)
{
fprintf(stderr, "Bad input\n");
return -1;
}
fprintf(stderr, "\tValid choice is: %d\n", choice);

这样,除十进制数以外的任何其他输入都将被丢弃并关闭程序,当然您可以做得更好。

int check = 0;
fprintf(stderr, "\tBegin loop\n");
fprintf(stdout, "1:Do stuff\n2:Do other stuff\n3: Do another stuff\nX: end\n");
while(fscanf(stdin, "%d", &choice) != 1)
{
fprintf(stderr, "Bad input!\n");
}
fprintf(stderr, "\tValid choice is: %d\n", choice);

在此版本中,当用户键入无效内容时,他只需重试。

关于c - 当我在我的程序中选择某个选项时,我不断得到一个无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20840959/

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