Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <stdlib.h> int main() { int account; char name[30];

ID: 3630007 • Letter: #

Question


#include <stdio.h>
#include <stdlib.h>

int main()
{
int account;
char name[30];
double balance;

FILE *cfPtr;

if((cfPtr = fopen("clients.dat","w")) == NULL){
printf("File could not be opened ");
}
else{
printf("Enter Account Info, Name and Balance ");
printf("Enter EOF to end input ");
printf("? ");
scanf("%d%s%lf",&account,name,&balance);

/*write to file*/
while(!feof(stdin)){
fprintf(cfPtr,"%d,%s,%.2f ", account, name, balance);
printf("? ");
scanf("%d%s%lf",&account,name,&balance);
}

fclose(cfPtr);

}
return 0;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

int main()
{
int account;
char name[30];
double balance;

FILE *cfPtr;

if((cfPtr = fopen("clients.dat","w")) == NULL){
printf("File could not be opened ");
}
else{
printf("Enter Account Info, Name and Balance ");
printf("Enter EOF to end input ");
printf(" ");
scanf("%d%s%lf",&account,name,&balance);

/*write to file*/
while(!feof(stdin)){
fprintf(cfPtr,"%d,%s,%.2f ", account, name, balance);

// here i removed 2 lines

// now give a try
}

fclose(cfPtr);

}
return 0;
}