This file will not read in the balance correctly. Could you tell me why?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* Function Prototypes */
void addAccount(void);
void listAccounts(void);
void deleteAccount(void);
/* The structure of a basic account */
struct account
{
int number;
char name[50];
double balance;
struct entry
{
int number; /*Checkbook entry */
int date;
char info[100];
union
{
double credit; /*credit or debit, not both */
double debit;
};
};
struct account *next; /* Pointer to next account in linked list */
};
struct account *firsta,*currenta,*newa; /* Linked list pointers */
int anum = 0;
int main()
{
char c;
firsta = NULL;
int done = 0; /* for while loop */
char go; /* for exit program condition */
while(done == 0)
{
fflush(stdin);
puts("\nDigital Checkbook\n");
puts("A - Add new account\n");
puts("L - List accounts\n"); /* Main menu */
puts("D - Delete Account\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 ('D'):
deleteAccount();
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)
{
newa = (struct account *)malloc(sizeof(struct account)); /* Allocate proper space*/
/* Check to see if this is the first record. If so, then initalize all the pointers to this first structure in the database */
if(firsta == NULL)
{
firsta = currenta = newa;
}
/* Otherwise, find the end of the structure list, the one with the NULL pointer, and add on the new structure you just allocated memory for */
else
{
currenta = firsta; /*make first record current */
while(currenta->next != NULL) /*Loop through all till NULL pointer found */
{
currenta = currenta->next; /* last record found */
currenta->next = newa; /*save the address of new */
currenta = newa; /* make current record new*/
}
}
/* Enter info */
anum++;
printf("%27s: %5i\n","Account number",anum);
currenta->number = anum;
printf("%27s: ","Enter account balance (Without commas)");
scanf("%s",¤ta->balance);
/* Cap new record with NULL pointer so I know it's the last record */
currenta->next = NULL;
}
void listAccounts(void)
{
if(firsta == NULL)
{
puts("There are no records to list!");
}
else
{
currenta = firsta;
do
{
printf("Account number: %d\n",currenta->number);
printf("Balance: %lf",currenta->balance);
}
while((currenta=currenta->next) != NULL);
}
getchar();
return;
}
void deleteAccount(void)
{
}