-
array and file I/O help
i am new to programming and am struggling with a homework project. Please help
Here are the requirements
need a program to store account information of a bank
need to store
id, one-word name, current balance.
i need to read transactions from a file. typical transactions look like this
1234 jon 123.45
4001 jake 1005.50
i have a few rules to go by..
can only have a max of 16 accounts
id's are int between 1000 and 9999
names are one word with less than 10 letters
names on transactions must match names in account
reject transactions that produce negative balances
i need to use arrays for id, name, amounts and store them in ascending order based on id.
i need to provide a reason for any account transaction being rejected... i.e invalid id, name, etc..
after reading all transactions from file need to write the id, name and final balance to file called account.txt..
heres what i have so far... i need some direction what to do next.. thanks
:confused: :confused: :confused:
#define LIMIT 48
#define LENGTH 16
int main()
{
char acct_info[LIMIT][LIMIT], buffer[4*LENGTH];
FILE *file;
int i, size = 0;
file = fopen("transact.txt", "r");
if(file ==NULL)
{
perror("UNABLE TO OPEN transact.txt\n");
system("pause");
return -1;
}
while(fscanf(file, "%s", buffer) ==1)
{
if(strlen(buffer) < LENGTH -1 )
{
if(size < LIMIT)
{
strcpy(acct_info[size], buffer);
++size;
}
else
printf("there is no room in the array for %s\n", buffer);
}
else
printf("%s is too long\n", buffer);
}
fclose(file);
file = fopen("account.txt", "w");
if(file ==NULL)
{
perror("UNABLE TO OPEN account.txt\n");
system("pause");
exit (-2);
}
for(i = 0; i < size; ++i)
fprintf(file, "%s\n", acct_info[i]);
fclose(file);
system("pause");
return 0;
}
-
-
I couldnt figure out how to delete the other thread.. but i added more info to this thread
-
>i need some direction what to do next..
Scrap what you have an start over. I generally get bad reactions when I suggest that, but if you use a well structured approach from the start it makes your task much easier.
When you start over, write a simple program to handle a single transaction on one record read from stdin. This saves you from fiddling with files and loops until you know that you can successfully work with one account.
Write the code piece by piece and test it thoroughly. For example, first write the code to read in a single account and validate it. Then when that works the way you want, write the code to perform a transaction. When that works, set up your error checking and messages.
At this point you should have a working program for a single record. Now you can start reading from a file. When that works properly, convert your single variables to arrays and wrap a loop around the code to read and handle one record. Tidy things up and you're done.
Most importantly, have a good understanding of the problem and any solutions before you write any code. So many people make the mistake of writing the code before they understand the problem. The result is bad, ugly, patchy code, and they end up duct taping it until it compiles and runs. But they can't guarantee correctness and the programs are usually bloated and error-prone.