当我在程序中选择某个选项时,我会一直无限循环

时间:2013-12-30 13:54:40

标签: c

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

然而,当我尝试修改名称时,它不会修改,当我尝试修改性别时,它会导致无限循环,任何人都可以告诉我什么是错的?

我认为我在循环中做出的断言有问题,提前谢谢

#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

3 个答案:

答案 0 :(得分:1)

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

1。 避免使用休息和继续,他们是流量破坏者,臭虫来源,坏和邪恶,它是相同的去,他们在这里特定的其他方式的情况。

您可以执行以下操作:

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。 每当你使用一个函数,测试它的返回时,就会发生很多因为思考而发生的错误&#34;它是一个stdio函数,它是防错的&#34;。例如,你的emp.dat的fopen。它可能会失败(事实上在某些时候会失败)。

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

在这个版本中,当用户输入无效内容时,他只需要再试一次。

答案 1 :(得分:1)

除了DrakaSAN的答案之外,我还要补充一点,当你在整数之后进行字符/字符串输入时,总是刷新输入缓冲区输入

刷新输入缓冲区的一种方法是使用getchar()

while ((ch = getchar()) != '\n');

但是如果用户将输入提供为“123 abc \ n”(如注释中 chux 所述),假设123转到整数变量而“abc”转到字符数组,那么是解决这个问题的方法:

//can be modified according to programmer's requirements
int a;
char arr[10],ch;

scanf("%d",&a);

while((ch=getchar())==' ' || ch=='\t' || ch=='\n') //loop until non-whitespace character
{
    if (ch=='\n')
    {
        ch=getchar();
        break;
    }
}

if (ch!='\n') //ch contains the first character of the character array
{
    arr[0]=ch;
    gets(arr+1);
}
else //if two consecutive new lines after integer, string contains nothing
    arr[0]='\0';

答案 2 :(得分:0)

您需要在choice == 2之前choice == 3continue之前写出结果,例如

fseek(fptr, -sizeof(customer), SEEK_CUR);
fwrite(&customer,sizeof(customer), 1, fptr);