DD ArtCustom
DD ArtCustom

Reputation: 13

The following C code shows that : format '%d' expects argument of type 'int *', but argument 7 has type float *

The following code shows that : format '%d' expects argument of type 'int *', but argument 7 has type float *. I'm not an expert but I can't distinguish the error. This issue is present at the scanf. Along with this issue there are 3 more related warnings. It's at line 158 in the void edit () section. I keep trying and getting this very same thing. I ask if someone can please kindly assist?

Problem:

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 here:

struct employee
{
        char name[50];
        char sex;
        char adrs[50];
        char dsgn[25];
        int age,empID;
        float slry;
};

Entire code:

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

Upvotes: 0

Views: 626

Answers (1)

dbush
dbush

Reputation: 224112

It would have been good if you showed us all the errors you got on that line:

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]

The first warning goes back to your prior question. e.sex has type char (which gets promoted to int) but you specified %s which expects a char *. To read this field, you want to use the %c format specifier which reads a single character instead of a sequence of characters, and you want to pass the address of the field you want to read into, i.e. &e.sex.

The second warning is due to your use of %.2f as a format specifier. Unlike printf, scanf doesn't take a precision. Change this to %f. Once you do that, the third error goes away.

As a rule, always address compilers errors from top to bottom, as problems earlier in the code can cascade down.

Upvotes: 0

Related Questions