Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

pointers - The following C code shows that : format '%s' expects argument of type 'char *', but argument 3 has type int

The following code shows that format '%s' expects argument of type 'char *', but line 139 has type 'int'. I don't see any mistakes in variable types. This issue is present at the scanf. Along with this issue there are 3 more related ones. I keep trying and getting this very same error. I ask if someone can please kindly assist?

This is the struct:

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

This is the 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... 
");

        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("*******************************
");
        printf("
Employee management system");
        printf("
1. Insert employee information");
        printf("
2. List all employee information");
        printf("
3. Edit employee information");
        printf("
4. Delete employee information");
        printf("
5. Exit");
        printf("

*****************************
");
        printf("

 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("********************************************************** 
");
        printf("
Enter the name of the employee: ");
        gets(e.name);
        printf("
Enter the sex of the employee (M/m or F/f): ");
        gets(&e.sex);
        printf("
Enter the address of the employee: ");
        gets(e.adrs);
        printf("
Enter designation of the employee: ");
        gets(e.dsgn);
        printf("
Enter age of the employee: ");
        scanf("%d", &e.age);
        printf("
Enter basic salary of the employee: ");
        scanf("%f", &e.slry);
        printf("
Enter 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 
%f 
%d 
", e.age, e.slry, e.empID);
       // fwrite(&e,recsize,1,fptr);
        fflush(stdin);
        printf("
Do you want to input more? (y/n): ");
        next = getche();
        printf("
");


    }
    while(next !='n');

    fclose(fptr);
}

void list ()
{
    /* what is going on here??? */
    while(fread(&e,recsize,1,fptr)==1)
    {
        printf("
%s %s %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("
Enter 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("
Edit another record(y/n)");
        next = getche();
        fflush(stdin);

    }
    while(next != 'n');


    return ;
}

void del()
{
    char next;
    do
    {
        printf("
Enter 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');
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not following the format properly, ie: while printing a character you are using %s, like: here printf(" %s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID); you are using %s for e.sex which is merely a character. And used %s for taking char inputs like e.sex via scanf several times, like this: scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);, use %c instead. Fix these formatting both while taking input and printing output in a formatted way and see.

You can take help from printf man page and scanf man page for details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...