I have a problem. This code reads accurately the account names off disk, but not the balance. It just shows up as $0.00. Please tell me what the problem is.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void addAccount(void);
void listAccounts(void);
struct account_data
{
char name[60];
double balance;
struct entry
{
int check_num;
char info[128];
union
{
double debit;
double credit;
};
};
};
int main()
{
char c;
int done = 0;
char go;
while(done == 0)
{
puts("\nDigital Checkbook\n");
puts("A - Add new account\n");
puts("L - List accounts\n");
puts("Q - Quit\n");
printf("Your choice: ");
c = getchar();
fflush(stdin);
c = toupper(c);
switch(c)
{
case ('A'):
addAccount();
break;
case ('L'):
listAccounts();
break;
case ('Q'):
printf("\nAre you sure you want to quit? Y/N: ");
go = getchar();
go = toupper(go);
if(go == 'Y')
{
done = 1;
fflush(stdin);
system("cls");
}
else if(go == 'N')
{
fflush(stdin);
system("cls");
}
break;
default:
puts("\nInvalid answer ");
} /*End switch*/
} /*End while*/
return(0);
} /*End main()*/
void addAccount(void)
{
FILE *Accounts;
struct account_data account;
printf("Enter account name: ");
scanf("%s",&account.name);
printf("How much money is currently in it? $");
scanf("%f",&account.balance);
Accounts = fopen("account.dat","a");
if (Accounts == NULL)
{
puts("Error opening file");
exit(1);
}
fwrite(&account,sizeof(account),1,Accounts);
fclose(Accounts);
puts("Account added!");
fflush(stdin);
}
void listAccounts(void)
{
FILE *Accounts;
struct account_data account;
int x;
Accounts = fopen("account.dat","r");
if(Accounts == NULL)
{
puts("No data in file, or file not found.");
return;
}
while(1 == 1)
{
x = fread(&account,sizeof(account),1,Accounts);
if(x == 0) break;
printf("\nAccount name:%s\n",account.name);
printf("Balance: $%.2f\n",account.balance);
}
fclose(Accounts);
}
That is the relevant part.