以下代码显示格式'%c'期望类型为'int'的参数,但是参数3的类型为'int'。我看不到变量类型有任何错误。在void list()部分的printf行155中存在此问题。我不知道为什么它期望它为int类型。是不是应该成为char?这似乎与其他警告不同。我问是否有人可以帮忙?
这是结构:
struct employee
{
char name[50];
char sex[7];
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
};
问题:
while(fread(&e,recsize,1,fptr)==1)///read the file and fetch the record one record per fetch
{
printf("\n\n%s \t\t%c \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex,
e.adrs, e.dsgn, e.age, e.slry, e.empID);
}
getch();
完整代码:
#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("%c",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("%d", &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);
// 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%c \t%s \t%s \t%d \t%.2f \t%d",e.name, e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID);
}
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);
}
答案 0 :(得分:1)
代码的主要问题是:
scanf("%c",e.sex);
e.sex
是一个字符数组,但是通过使用%c
说明符,您应该提供单个char
变量的地址,即将char sex[7];
替换为{{1} }。
与以下相同:
char sex;
由于您在 scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, &e.empID);
^^ ^^^^^
语句中使用了此变量,因此您可能需要考虑将switch
成员变量struct employee
设为单个sex
。
或者如果它确实是一个char数组,则需要:
char
产生您描述的错误的代码也是如此。
scanf("%6s",e.sex); //6 char limit for a 7 char container
在这种情况下,一个简单的printf("\n\n%s \t\t%c \t%s ... e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID);
^^ ^^^^^
说明符就足够了。
但是您不能在%s
中使用e.sex
或任何char数组。
这会导致您在switch
中取消引用e.sex
,这意味着您正在使用switch(*e.sex)
char数组的第一个字符,如果有意的话,可以,这是合法的,尽管并不常见,但请注意这一点。