Reputation: 13
The following code shows that format '%c' expects argument of type 'int', but argument 3 has type 'int'. I don't see any mistakes in variable types. This issue is present at the printf, line 155 at the void list() section. I have no idea why it's expecting it as type int. Isn't it suppose to be char? This seems different from the other warnings. I ask if someone can please kindly assist?
This is the struct:
struct employee
{
char name[50];
char sex[7];
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
};
Problem:
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();
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 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);
}
Upvotes: 0
Views: 320
Reputation: 23832
The main problem with the code is:
scanf("%c",e.sex);
e.sex
is a char array, but by using the %c
specifier you should provide the address of a single char
variable, i.e. replacing char sex[7];
with char sex;
.
The same with:
scanf("%s%c%s%s%d%f%d", e.name, e.sex, e.adrs, e.dsgn, &e.age, &e.slry, &e.empID);
^^ ^^^^^
Since you are using this variable in a switch
statement you may want to consider making struct employee
member variable sex
a single char
.
Or if it's really a char array you need:
scanf("%6s",e.sex); //6 char limit for a 7 char container
The same goes for the code that produces the error you describe.
printf("\n\n%s \t\t%c \t%s ... e.sex, e.adrs, e.dsgn, e.age, e.slry, e.empID);
^^ ^^^^^
In this case a simple %s
specifier is enough.
But you cannot then use e.sex
or any char array, for that matter, in a switch
.
That leads you dereference e.sex
in switch(*e.sex)
, which means that you are using the first character of e.sex
char array, if it is intentional, ok, it's legal, though not usual, but be aware of this.
Upvotes: 1