In my program I have to print a general journal with data coming from 2 files, ‘accounts.dat’ and ‘transactions.dat’
accounts.dat has:
1000:first
2000:second
3000:hello
transactions.dat has
1000:first:d:1000.000000
2000:second:c:1500.000000
3000:hello:d:1000.000000
So basically the same data except transactions has 2 more.
When I checked my read function which scans the file data and inputs it into arrays that are in the program, they’re there but when I call the generaljournal function to print it in a specific format, the general journal does not acknowledge the files that are scanned into my arrays.
the function is:
void generaljournal(int accounts[MAX], int accounts3[MAX], char debcred[MAX], double amount[MAX], char accname[MAX][MAXSTRING], char transname[MAX][MAXSTRING], int *totalinput) {
int i = 0;
printf ("\n");
printf ("\t\t\tGeneral Journal\n");
printf ("Account Description Debit Credit\n");
printf ("------- ------------------------------ ---------- ----------\n");
for (i = 0; i < *totalinput; i++)
{
printf (" %d", accounts[i]);
if (debcred[i] == 'd')
{
printf (" %-35s", transname[i]);
printf ("%.2lf\n", amount[i]);
}
else if (debcred[i] == 'c')
{
printf (" %-47s", transname[i]);
printf ("%.2lf\n", amount[i]);
}
}
printf ("\n");
}
All the arrays that are called has the data in the arrays but aren’t being printed when I call the general journal function. I call the read function in my main but even if I put the
read(accounts, accounts3, debcred, amount, accname, transname);
In my general journal function it still doesn’t do anything.
What the output SHOULD look like is something like
Account Description Debit Credit
1000 first 1000.000
2000 second 1500.00
3000 hello 1000.00
Instead it prints just the shell of (accounts, description, debit, credit) with no values underneath.
Does anyone know why it’s not accepting the values in the arrays when I scanned it all from the files in my read function? If more information is needed i’ll supply. My read function is:
void read (int accounts[MAX], int accounts3[MAX], char debcred[MAX], double amount[MAX], char accname[MAX][MAXSTRING], char transname[MAX][MAXSTRING]) {
FILE *fp1 = NULL;
FILE *fp2 = NULL;
fp1 = fopen("accounts.dat", "r");
fp2 = fopen("transactions.dat", "r");
int h = 0;
if (fp1 != NULL) // READING THE FILE
{
while(fscanf(fp1, "%d", &accounts[h]) != EOF)
{
fgetc(fp1);
fscanf(fp1, "%30[^\n]", &accname[h]);
h++;
}
fclose(fp1);
}
else
printf ("Failed to open file\n");
if (fp2 != NULL) // READING THE FILE
{
int j = 0;
while(fscanf(fp2, "%d", &accounts3[j]) != EOF)
{
fgetc(fp2);
fscanf(fp2, "%30[^:]", &transname[j]);
fgetc(fp2);
fscanf(fp2, "%c", &debcred[j]);
fgetc(fp2);
fscanf(fp2, "%lf", &amount[j]);
j++;
}
fclose(fp2);
}
else
printf ("Failed to open file\n");
}
Maybe the value of
*totalinputis0