【问题标题】:The following C code shows that : format '%d' expects argument of type 'int *', but argument 7 has type float *以下 C 代码显示:格式 '%d' 需要类型为 'int *' 的参数,但参数 7 的类型为 float *
【发布时间】:2020-10-18 23:13:21
【问题描述】:

以下代码显示:格式“%d”需要“int *”类型的参数,但参数 7 的类型为 float *。我不是专家,但我无法区分错误。这个问题出现在 scanf 中。除此问题外,还有 3 个相关警告。它位于 void edit () 部分的第 158 行。我一直在尝试并得到同样的东西。请问有人可以帮忙吗?

问题:

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

结构在这里:

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

【问题讨论】:

标签: c pointers types arguments


【解决方案1】:

如果您向我们展示所有您在该行遇到的错误,那就太好了:

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 格式说明符,它读取单个字符而不是字符序列,并且您要传递要读取的字段的地址,即&amp;e.sex

第二个警告是由于您使用%.2f 作为格式说明符。与printf 不同,scanf 不需要精度。将此更改为%f。一旦你这样做了,第三个错误就会消失。

作为一项规则,总是从上到下解决编译器错误,因为代码中较早的问题可能会级联。

【讨论】:

  • 谢谢。我不再有与类型相关的警告。但是现在 fgets 错误
  • @DDArtCustom 看看你应该如何调用函数。
  • 谢谢,fgets函数有三个参数。
猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
相关资源
最近更新 更多