-1

I'm not sure what's causing this code to not work. I'm not getting any error messages. It just gives me a blank output. transactiondata is a file that has a list of employees (e), customers (c) and transactions (t). I'm supposed to output a list that shows the transactions, which customers made them with which employees, and how it affected their balance.

#include <stdio.h>
#include <string.h>
   

int main(int argc, char* argv[])
{
    FILE *fpointer = fopen("transactiondata", "r");                       

    if(fpointer == NULL)                                                      
    {
        printf("Error. Could not read file.\n");
        return 1;
    }

    char status, transactionType, eName[30], cName[30], employees[55][30], customers[55][30];             
    float balances[55], bal, transaction; 
    int eID, cID;

    while(fscanf(fpointer, "%c", &status) != EOF);
    {
        switch(status)
        {
            case 'e':
            fscanf(fpointer, "%i %s", &eID, eName);
            strcpy(employees[eID], eName);
            break;

            case 'c':
            fscanf(fpointer, "%i %s %f", &cID, cName, &bal);
            strcpy(customers[cID], cName);
            balances[cID] = bal;
            break;

            case 't':
            fscanf(fpointer, "%i %i %c %f", &cID, &eID, &transactionType, &transaction);
        
            if(transactionType == 'w')
            {
                balances[cID] -= transaction;
                transactionType = '-';
            }
            else
            {
                balances[cID] += transaction;
                transactionType = '+';
            }

            printf("%10s %10s\t\t%c$%0.2f %8.2f\n", customers[cID], employees[eID], 
            transactionType, transaction, balances[cID]);
            break;

            default:
            break;
        }
    }

    return 0;
}
ChiefsCML
  • 1
  • 1
  • What do you get if you echo what you scan and the return value of fscanf() in all cases? – Yunnosch Sep 10 '22 at 06:51
  • 2
    Please provide a sample "transactiondata" file to demonstrate the issue. – Allan Wind Sep 10 '22 at 06:51
  • If you don't have any 't' status there will be no output. – Allan Wind Sep 10 '22 at 06:52
  • This seems like a very good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your program. For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Sep 10 '22 at 06:52
  • 1
    fscanf() can fail so maybe add some error checking? – Allan Wind Sep 10 '22 at 06:54
  • By the way, `fscanf(fpointer, "%c", &status)` will not work very well in a file with more than one line, since it will read the newline. – Some programmer dude Sep 10 '22 at 06:54
  • Assume there is a record per line strip the newline with `fscanf(fpointer, " %c", &status)` – Allan Wind Sep 10 '22 at 07:12
  • 1
    `while(fscanf(fpointer, "%c", &status) != EOF);` has incorrect `;` – Weather Vane Sep 10 '22 at 07:25
  • Minimize scope of variables as much as possible. If you don't check the return value of `fscanf()` any of the variables it sets are potentially undefined. Also any of those `fscanf()` calls may return EOF. All the string reads are subject to overflow as is the strcpy. Use #define for the magic values. For the transaction type, check that type other than 'w' is as expected (i.e. 'd'?). Don't use float for money. – Allan Wind Sep 10 '22 at 07:31
  • Please look at the warnings emitted by your compiler. [See here](https://godbolt.org/z/TK4TGMzK8): `-Wmisleading-indentation: this 'while' clause does not guard this statement, but the latter is misleadingly indented as if it were guarded by the 'while'` – AKX Sep 10 '22 at 07:38
  • I appreciate the link @Someprogrammerdude I definitely have never debugged before lol I haven't learned yet. That will be a good resource for me! – ChiefsCML Sep 10 '22 at 16:16
  • You're right @AllanWind I should have included that my bad! Thank you for pointing out that stray semicolon to me – ChiefsCML Sep 10 '22 at 16:17

1 Answers1

0

I reverse engineering your code to arrive at the follwing sample transactiondata file:

e 1 alice
c 2 megacorp 1.2
t 2 1 w 3.4

I found, indeed, that the program didn't print anything. You resolve this by removing the stray ';` after the while statement to make it:

     while(fscanf(fpointer, "%c", &status) != EOF)

The program then prints:

  megacorp      alice           -$3.40    -2.20
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • In other words, this should have probably been closed as a typo ;-) – AKX Sep 10 '22 at 07:38
  • Yep. I had already voted to close it due to missing debug data. – Allan Wind Sep 10 '22 at 07:39
  • While you were reverse engineering that (well done!), did you wonder (as I do) why the data would use "w " instead of the conventional "-" (and "? " to mean "+") on the transaction line??? The marvels one lives to see... – Fe2O3 Sep 10 '22 at 07:57
  • @Fe203 the w is a part of the transaction data. the transactions either have a w or d for withdrawal or deposit. so i have to read that. i'm also a first year coding student and i've never done any of this before LOL . thanks for the help guys! :) – ChiefsCML Sep 10 '22 at 16:05