This is my first question on here and im still learning c, and i was writing this code to enter details of a user bank account to file and reading all records back from file, when the error, error:invalid type argument of unary '*" (have 'int') appeared on all lines that had pointer to struct *ptr pointed to integer values (eg line 40)
struct usr_act
{
char username[24], address[24], status;
int id, prebal, cp, newbal, pdate;
}a[3];
int j=0;
struct usr_act *ptr;
bool r;
void input()
{
FILE *fp;
fp = fopen("accounts.txt","a+");
if(fp==NULL)
{
printf("Error!!");
exit(1);
}
printf("\n\tRecord %d\n\n",j+1);
fprintf(fp,"Record",j+1);
printf("\nName:- ");
for(int i=0;i<24;i++)
{
scanf("%c",ptr->username[i]);
}
fprintf(fp,"Name:- %s",*(ptr->username));
printf("\nAddress:- ");
for(int i=0;i<24;i++)
{
scanf("%c",ptr->address[i]);
}
fprintf(fp,"Address:- %s",*(ptr->address));
printf("\nCustomer Id:- ");
scanf("%d",ptr->id);
fprintf(fp,"Customer Id:- %d",*(ptr->id)); //Error
printf("\nPrevious balance:- ");
scanf("%d",ptr->prebal);
fprintf(fp,"Previous Balance:- %d",*(ptr->prebal)); //Error
printf("\nCurrent Payment:- ");
scanf("%d",ptr->cp);
fprintf(fp,"Current Payment:- %d",*(ptr->cp)); //Error
printf("\nPayment Date:- ");
scanf("%d",ptr->pdate);
fprintf(fp,"Payment Date:- %d",*(ptr->pdate)); //Error
fclose(fp);
r=true;
}
void calc()
{
FILE *fp;
fp = fopen("accounts.txt","a+");
if(fp==NULL)
{
printf("Error!!");
exit(1);
}
float k;
k = 0.10 * (*(ptr->prebal));
if(*(ptr->cp)>0 && *(ptr->cp)<k)
{
ptr->status='o';
}
else
{
ptr->status='c';
}
fprintf(fp,"Account status:- %c",*(ptr->status));
ptr->newbal= *(ptr->prebal) - (*(ptr->cp));
fprintf(fp,"New Balance:- %d",*(ptr->cp));
fclose(fp);
}
and in between i have a display function which displays data from file and this is the main function
int main()
{
int l;
do
{
ptr=&a[j];
r=false;
printf("\n\tMenu\nk=1, Input details \nk=2, Show patient records \nk=3, Exit \n\nEnter your choice:- ");
scanf("%d",&l);
switch(l)
{
case 1:
{
input();
calc();
break;
}
case 2:
{
display();
break;
}
}
if(r==true)
{
j=j+1;
}
} while(l!=3);
return 0;
}
How can i solve this?
The error has a relation to this statement
fprintf(fp,"Previous Balance:- %d",*(ptr->prebal));
The type of the expression ptr->prebal
is int. So you may not apply the operator *.
Also this call
fprintf(fp,"Record",j+1);
does not make a sense because the argument j + 1
is not used.
In calls of fprintf
like this
fprintf(fp,"Name:- %s",*(ptr->username));
there is an invalid type of the argument *(ptr->username)
. Instead there must be ptr->username.
Also you are using invalid arguments in calls of scanf like in this statement
scanf("%c",ptr->username[i]);
You have at least to write
scanf("%c", &ptr->username[i]);
You need to read the descriptions of fprintf
and scanf
before using them.