C 以下代码在错误的行中显示信息

C 以下代码在错误的行中显示信息,c,C,代码的功能有问题。如下面的屏幕截图所示,它将信息存储在错误的部分。更具体地说是员工的性别。它会打印“m”或“f”以及员工的地址。我认为这与性功能有关。有人能帮忙吗?这是它如何显示的屏幕截图: 这是结构: struct employee { char name[50]; char sex[7]; char adrs[50]; char dsgn[25]; int age; int empID[9];

代码的功能有问题。如下面的屏幕截图所示,它将信息存储在错误的部分。更具体地说是员工的性别。它会打印“m”或“f”以及员工的地址。我认为这与性功能有关。有人能帮忙吗?这是它如何显示的屏幕截图:

这是结构:

struct employee
{
        char name[50];
        char sex[7];
        char adrs[50];
        char dsgn[25];
        int age;
        int empID[9];
        float slry;
};
具体代码:

        printf("\nEnter the sex of the employee (M/m or F/f): ");
        scanf("%6s",e.sex);

        switch(*e.sex)
        {
            case 'M':
            case 'm':
                printf("\nMale.\n");
                break;
            case 'F':
            case 'f':
                printf("\nFemale.\n  ");
                break;
            default:
                printf("Unspecified Sex.");
        }
地址代码:

printf("\nEnter the address of the employee: ");

fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP

fgets(e.adrs, sizeof(e.adrs), stdin); // this
完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"

void insert();
void list();
void edit();
void del();
void exit();
int tolower();

FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];

int main()
{
    int choice;
    fptr = fopen("ems.txt", "r+");


    if (fptr == NULL)
    {
        printf("Can't find file! Attempting to create file... \n");

        fptr = fopen("ems.txt","w+");
        if(fptr == NULL)
        {
            printf("Can't create file. Exiting...");
         exit(1);
        }
    }

    //Explain the reason for this?
    //recsize = (long int) sizeof(e);//


    while(1)
    {
        printf("*******************************\n");
        printf("\nEmployee management system");
        printf("\n1. Insert employee information");
        printf("\n2. List all employee information");
        printf("\n3. Edit employee information");
        printf("\n4. Delete employee information");
        printf("\n5. Exit");
        printf("\n\n*****************************\n");
        printf("\n\n Enter your choice: ");
        scanf("%d", &choice);
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}

        switch(choice)
        {
            case 1:
                puts("Insert was chosen");
                insert();

                break;
            case 2:
                puts("List was chosen");
                list();
                break;
            case 3:
                puts("Edit was chosen");
                edit();
                break;
            case 4:
                puts("Delete was chosen");
                del();
                break;
            case 5:
                puts("Exit was chosen");
                exit(1);
                break;
            default:
                puts("Choice is incorrect!!");
                break;
        }
    }

    return 0;
}


void insert()
{
    char next;

    do
    {
        printf("********************************************************** \n");
        printf("\nEnter the name of the employee: ");
        fgets(e.name, sizeof(e.name), stdin);
        printf("\nEnter the sex of the employee (M/m or F/f): ");
        scanf("%6s",e.sex);

        switch(*e.sex)
        {
            case 'M':
            case 'm':
                printf("\nMale.\n");
                break;
            case 'F':
            case 'f':
                printf("\nFemale.\n  ");
                break;
            default:
                printf("Unspecified Sex.");
        }

        printf("\nEnter the address of the employee: ");

        fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP

        fgets(e.adrs, sizeof(e.adrs), stdin); // this
        printf("\nEnter designation of the employee: ");
        fgets(e.dsgn, sizeof(e.dsgn), stdin); // this

        printf("\nEnter age of the employee: ");
        scanf("%d", &e.age);
        printf("\nEnter basic salary of the employee: ");
        scanf("%f", &e.slry);
        printf("\nEnter the employee's ID: ");
        scanf("%8d", e.empID);
        fputs(e.name, fptr);
        fputs(e.sex, fptr);
        fputs(e.adrs, fptr);
        fputs(e.dsgn, fptr);
        fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID[9]);
       // fwrite(&e,recsize,1,fptr);
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
        //fflush(stdin);//
        printf("\nDo you want to input more? (y/n): ");
        next = getche();
        printf("\n");
    }
    while( tolower(next) != 'n' );

    fclose(fptr);
}

void list ()
{
     printf("-------------------------------");
     printf("\nEmployee Details: \n---------------------------------\n");
     rewind(fptr);///moves file to start of the file
     while(fread(&e, recsize, 1, fptr)==1)///read the file and fetch the record one record per fetch
     {
         printf("\n\n%s \t\t%6s \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID[9]);
     }
     getch();

     /*printf("Name        : %s\n",e.name);
     printf("Address     : %s\n",e.adrs);
     printf("Sex         : %c\n",e.sex);
     printf("Designation : %s\n",e.dsgn);
     printf("Age         : %d\n",e.age);
     printf("Salary      : %.2f\n",e.slry);
     printf("Employee-ID : %d\n",e.empID);*/
}

void edit ()
{
    char next;
    do
    {
        printf("Enter the employee's name to be edited: ");
        scanf("%49[^\n]", empname);
        rewind(fptr);
        while(fread(&e, recsize, 1, fptr)==1)///fetch all records from file
        {
            if(strcmp(e.name,empname) == 0) ///if entered name matches with that in file
                printf("\nEnter new name, sex, address, designation, age, salary and employee ID: ");
                scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, e.empID);
                fseek(fptr, -recsize, SEEK_CUR);/// move cursor 1 step back from current position
                fwrite(&e, recsize,1,fptr); ///override the record
                break;
        }

        printf("\nEdit another record(y/n)");
        next = getche();
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}

    }
    while(next != 'n');

    return ;
}

void del()
{
    char next;
    do
    {
        printf("\nEnter name of employee to delete: ");
        scanf("%s",empname);
        ftemp = fopen("Temp.dat","wb"); ///create a intermediate file for temporary storage
        rewind(fptr); ///move record to starting of file
        while(fread(&e,recsize,1,fptr) == 1)  ///read all records from file
        {
            if(strcmp(e.name,empname) != 0)  ///if the entered record match
            {
                fwrite(&e,recsize,1,ftemp); ///move all records except the one which is to be deleted to temp file
            }
        }

        fclose(fptr);
        fclose(ftemp);
        remove("ems.txt"); ///remove original file
        rename("Temp.dat","ems.txt"); ///rename temp file to original file name
        fptr = fopen("ems.txt", "rb+");
        printf("Delete another record(y/n)");
        int ch;  while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
        next = getche();


    }while( tolower(next) != 'n' );
    fclose(fptr);
    exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“struct.h”
无效插入();
作废清单();
作废编辑();
void del();
无效退出();
int-tolower();
文件*fptr,*ftemp;
结构工程;
长整型;
字符名称[50];
int main()
{
智力选择;
fptr=fopen(“ems.txt”,“r+”);
如果(fptr==NULL)
{
printf(“找不到文件!正在尝试创建文件…\n”);
fptr=fopen(“ems.txt”,“w+”);
如果(fptr==NULL)
{
printf(“无法创建文件。正在退出…”);
出口(1);
}
}
//解释原因?
//recsize=(长整型)sizeof(e)//
而(1)
{
printf(“***************************************\n”);
printf(“\n员工管理系统”);
printf(“\n1.插入员工信息”);
printf(“\n2.列出所有员工信息”);
printf(“\n3.编辑员工信息”);
printf(“\n4.删除员工信息”);
printf(“\n5.退出”);
printf(“\n\n************************************\n”);
printf(“\n\n输入您的选择:”);
scanf(“%d”,选择(&C);
int ch;while((ch=getchar())!=EOF&&ch!='\n'){;}
开关(选择)
{
案例1:
看跌期权(“选择插入”);
插入();
打破
案例2:
看跌期权(“选择名单”);
list();
打破
案例3:
puts(“已选择编辑”);
编辑();
打破
案例4:
看跌期权(“选择删除”);
del();
打破
案例5:
看跌期权(“选择退出”);
出口(1);
打破
违约:
puts(“选择不正确!!”;
打破
}
}
返回0;
}
空白插入()
{
下一步;
做
{
printf(“******************************************************************************************************************\n”);
printf(“\n输入员工姓名:”);
fgets(e.name、sizeof(e.name)、stdin);
printf(“\n输入员工的性别(男/女或女):”;
scanf(“%6s”,例如性别);
开关(*e.sex)
{
案例“M”:
案例“m”:
printf(“\n可能。\n”);
打破
案例“F”:
案例“f”:
printf(“\n电子邮件。\n”);
打破
违约:
printf(“未指明性别”);
}
printf(“\n输入员工的地址:”);
fseek(stdin,0,SEEK_END);//添加此选项以避免跳过
fgets(e.adrs,sizeof(e.adrs),stdin);//这个
printf(“\n输入员工名称:”);
fgets(e.dsgn,sizeof(e.dsgn),stdin);//这个
printf(“\n输入员工的年龄:”);
scanf(“%d”和“e.age”);
printf(“\n输入员工的基本工资:”);
scanf(“%f”和e.slry);
printf(“\n输入员工ID:”);
scanf(“%8d”,e.empID);
FPUT(如名称、fptr);
FPUT(如性别、fptr);
FPUT(如ADR、fptr);
FPUT(如dsgn、fptr);
fprintf(fptr,“%d\n%f\n%d\n”,e.age,e.slry,e.empID[9]);
//fwrite&e,recsize,1,fptr);
int ch;while((ch=getchar())!=EOF&&ch!='\n'){;}
//fflush(stdin)//
printf(“\n是否要输入更多?(y/n):”;
next=getche();
printf(“\n”);
}
while(tolower(next)!=“n”);
fclose(fptr);
}
作废清单()
{
printf(“------------------------------------”;
printf(“\n员工详细信息:\n-----------------------------------\n”);
倒带(fptr);///将文件移动到文件的开头
while(fread&e,recsize,1,fptr)==1)///读取文件并获取记录,每次获取一条记录
{
printf(“\n\n%s\t\t%6s\t%s\t%s\t%d\t%.2f\t%d”,e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID[9]);
}
getch();
/*printf(“名称:%s\n”,e.Name);
printf(“地址:%s\n”,例如adrs);
printf(“性别:%c\n”,e.Sex);
printf(“名称:%s\n”,例如dsgn);
printf(“年龄:%d\n”,即年龄);
printf(“工资:%.2f\n”,e.slry);
printf(“员工ID:%d\n”,e.empID)*/
}
无效编辑()
{
下一步;
做
{
printf(“输入要编辑的员工姓名:”);
scanf(“%49[^\n]”,empname);
倒带(fptr);
而(fread&e,recsize,1,fptr)==1)///从文件中获取所有记录
{
if(strcmp(e.name,empname)==0)///如果输入的名称与文件中的名称匹配
printf(“\n输入新姓名、性别、地址、职务、年龄、工资和员工ID:”);
scanf(“%s%c%s%s%d%f%d”,e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,e.empID);
fseek(fptr,-recsize,SEEK_CUR);///将光标从当前位置向后移动1步
fwrite(&e,recsize,1,fptr);///重写记录
打破
}
printf(“\n编辑另一条记录(是/否)”);
next=getche();
int ch;while((ch=getchar())!=EOF&&ch!='\n'){;}
}
while(next!=“n”);
返回;
}
void del()
{
下一步;
做
{
printf(“\n输入要删除的员工姓名:”);
scanf(“%s”,empname);
ftemp=fopen(“Temp.dat”、“wb”);///为临时存储创建一个中间文件
倒带(fptr);///将记录移动到st
fgets(data_array, sizeof data_array, stdin);
data_array[strcspn(data_array, "\n\r")] = '\0';  // add
        fputs(e.name, fptr);
        fputs(e.sex, fptr);
        fputs(e.adrs, fptr);
        fputs(e.dsgn, fptr);
     while(fread(&e, recsize, 1, fptr)==1)///read the file and fetch the record one record per fetch
fwrite(&e, sizeof(e), 1, fptr);