以下代码显示:格式'%d'期望类型为'int *'的参数,但是参数7的类型为float *。我不是专家,但是我无法区分错误。在scanf上存在此问题。除此问题外,还有3个相关的警告。在void edit()部分的第158行。我不断尝试并得到同样的东西。我问是否有人可以帮忙?
问题:
struct employee
{
char name[50];
char sex;
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
};
在这里构造:
#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 ext();
FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];
int main()
{
//FILE * fptr, *ft;
int choice;
//fptr = fopen("ems.txt","rb+");
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...");
ext(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);
fflush(stdin);
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");
ext(1);
break;
default:
puts("Choice is incorrect!!");
continue;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name);
printf("\nEnter the sex of the employee (M/m or F/f): ");
fgets(&e.sex);
printf("\nEnter the address of the employee: ");
fgets(e.adrs);
printf("\nEnter designation of the employee: ");
fgets(e.dsgn);
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);
fflush(stdin);
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while(next !='n');
fclose(fptr);
}
void list ()
{
/* what is going on here??? */
while(fread(&e,recsize,1,fptr)==1)
{
printf("\n%s %c %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID);
}
getche();
return ;
}
void edit ()
{
char next;
do
{
printf("Enter the employee name to be edited: ");
scanf("%s", empname);
while(fread(&e,recsize,1,fptr)==1)
{
if(strcmp(e.name,empname) == 0)
{
printf("\nEnter new name,sex,address,designation,age,salary,employee ID ");
scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
fseek(fptr,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fptr);
break;
}
}
printf("\nEdit another record(y/n)");
next = getche();
fflush(stdin);
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb");
while(fread(&e,recsize,1,fptr) == 1)
{
if(strcmp(e.name,empname) != 0)
{
fwrite(&e,recsize,1,ftemp);
}
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt");
rename("Temp.dat","ems.txt");
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
next = getche();
}while(next !='n');
}
完整代码:
items
答案 0 :(得分:0)
如果您向我们展示所有 您在那条线上遇到的错误,那将是一件好事:
x1.c: In function ‘edit’:
x1.c:170:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
^
x1.c:170:17: warning: unknown conversion type character ‘.’ in format [-Wformat=]
x1.c:170:17: warning: format ‘%d’ expects argument of type ‘int *’, but argument 7 has type ‘float *’ [-Wformat=]
x1.c:170:17: warning: too many arguments for format [-Wformat-extra-args]
第一个警告回到您先前的问题。 e.sex
的类型为char
(将提升为int
),但是您指定的%s
期望为char *
。要读取该字段,您想使用%c
格式说明符,该说明符读取单个字符而不是字符序列,并且您想要传递要读取的字段的地址,即{{1} }。
第二个警告是由于您使用&e.sex
作为格式说明符。与%.2f
不同,printf
并不精确。将其更改为scanf
。一旦这样做,第三个错误就会消失。
通常,总是从上到下解决编译器错误,因为代码中的早期问题可以逐步解决。