annaSparkles
Programmer
hi, im required to write a program that processes information in a file called 'info.dat'. i've gotten this program to work, but my problem is with the info.dat file. i had to write another program to enter the data into info.dat, but 2 of the records are giving me trouble, could someone please look at my code and see whats wrong with it:
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct memberData
{
int account;
int pin;
char name[20];
char sname[20];
char phone[8];
double lbalance;
double linstal;
double cbalance;
};//end structure memberData
//prototypes
int menu(void);
void newRecord(FILE *newfile);
void displayFile(FILE *readfile);
int main()
{
int request; //user's request
FILE *myfile; //data.dat file pointer
//fopen opens the file; prints error message if file cannot be opened
if((myfile = fopen("info.dat","a+")) == NULL)
{
printf("\t\t File Cannot Be Opened.\n");
}//end if
fclose(myfile); //fclose closes the file
//fopen opens the file; prints error message if file cannot be opened
if((myfile = fopen("info.dat","r+")) == NULL)
{
printf("\t\t File Cannot Be Opened.\n");
}/*end if*/
else
{
/*allow user to specify action*/
while((request = menu()) != 3)
{
switch(request)
{
case 1:
newRecord(myfile);
getch();
break;
case 2:
displayFile(myfile);
getch();
break;
default:
printf("\n\t\t Invalid Entry.\a\n");
getch();
break;
}//end switch
}//end while
fclose(myfile);
}//end else
return 0;
}//end main
//allow user to enter request
int menu(void)
{
int selection; //variable to store user's request
system ("cls"); //clears the display screen
//display available options
printf("\n\n\t\t*********************************\n"
"\t\t** 1 - Create Records. **\n"
"\t\t** 2 - Display Records. **\n"
"\t\t** 3 - Quit Program. **\n"
"\t\t*********************************\n"
"\t\t Enter Request: ");
//receive user's selection
scanf("%d",&selection);
return selection;
}//end menu
//create and store new record
void newRecord(FILE *newfile)
{
int idNum, npin; //variable to store user's selection
//create memberData with default information
struct memberData member = { 0, 0, "", "", "", 0.0, 0.0, 0.0 };
//prompt user to enter record key
printf("\n\n\t\tEnter New Identification Number: ");
scanf("%d", &idNum);
fseek(newfile,(idNum - 1) * sizeof(struct memberData),SEEK_SET);
fread(&member, sizeof(struct memberData),1,newfile);
//displays error message if record key is already in use
if(member.account != 0 )
{
printf("\n\n\t\t Id No:%d Already Exists.\n\n",member.account);
}/*end if*/
else
{
//prompt user to enter to enter data
printf("\t\tEnter PIN: ");
scanf("%d",&npin);
member.pin = npin;
printf("\t\tEnter First Name: ");
scanf("%s",member.name);
printf("\t\tEnter Surname: ");
scanf("%s",member.sname);
printf("\t\tEnter Telephone No: ");
scanf("%s",member.phone);
printf("\t\tEnter Loan Balance: ");
scanf("%lf",&member.lbalance);
printf("\t\tEnter Loan Instalment: ");
scanf("%lf",&member.linstal);
printf("\t\tEnter Cash Balance: ");
scanf("%lf",&member.cbalance);
member.account = idNum;
//moves file pointer to correct record in file
fseek(newfile,(member.account - 1) * sizeof(struct memberData),SEEK_SET);
//inserts record in file
fwrite(&member,sizeof(struct memberData),1,newfile);
}//end else
}//end newRecord
//display contents of the file
void displayFile(FILE *readfile)
{
//reate memberData with default information
struct memberData member[100] = { 0, 0, "", "", "", 0.0, 0.0, 0.0 };
int count = 0; //count entries in file
printf("\n\n%-7s %-5s %-12s %-9s %-7s %-11s %-12s %-6s\n\n","Acct.",
"PIN","First Name","Surname","Phone","Loan Bal.",
"Loan Inst.","Cash");
//reposition file pointers at start of file
rewind(readfile);
/*print all records from the file to the screen*/
while(!feof(readfile))
{
fread(&member[count],sizeof(struct memberData),1,readfile);
if(member[count].account != 0)
{
printf("%-7d %-5d %-12s %-9s %-7s %-11.2f %-12.2f %-6.2f\n",
member[count].account,member[count].pin,member[count].name,
member[count].sname,member[count].phone,member[count].lbalance,
member[count].linstal,member[count].cbalance);
count++;
}//end if
}//end while
}//end displayFile