0

I am trying to create code with basic C programming that will produce a running "net" from a starting balance. So far I have used the "while" loop that runs and counts the loops but does not calculate correctly.

The starting balance will be 1000.00 and it should be input to this information that produces a net.

So how do I loop, in a way, to maintain a running total? I also need to make sure the output produces positive floats listed under "Amount".

Eva
  • 3
  • 2
  • What would be ‘Net’ and ‘amount’? – joão gabriel s.f. May 06 '18 at 22:32
  • [Do not use `float` for currency](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency/). Trust me on this. You will get burned. – MFisherKDX May 06 '18 at 22:36
  • [while(!feof(fp)) is always wrong](https://stackoverflow.com/questions/5431941) – user3386109 May 06 '18 at 23:00
  • Net is essentially a running total beginning with 1,000 (the starting balance) and the "amount" in the amount column is calculated from this each loop. ie...1,000 + (-2325.19) (first amount) = -1325.19 (first net), then this first net -1325.19 is + 4735.41 (second amount) = 3410.22 (second net), and so on. – Eva May 07 '18 at 00:31
  • Also, what do you suggest for currency if not float? This is what my professor has suggested to use. And what in place of while(!feoff(fp))? – Eva May 07 '18 at 00:32
  • Lizzy, read the links posted above by myself and @user3386109 -- they do a very good job explaining the fundamental issues and solutions. Also the fact your professor recommends float for currency is troubling. – MFisherKDX May 07 '18 at 01:04
  • ok, I see. Thank you. He may just be suggesting simplest solutions since we are beginners. But I'd prefer to start with good habits, so I will give those a read. – Eva May 07 '18 at 01:18

1 Answers1

0
  1. you calculate EndBal in wrong place, move it below fscanf.
  2. EndBal will always calculate wrong because you update it wrong. use EndBal=balance before you enter loop and use EndBal += amount inside.
  3. net does the same as EndBal, chose one of them or add net=EndBal before printing.
  4. in case you will use the final balance value somewhere else, don't declare balance as a const

Edit: don't forget to select this answer if it does what you need.

Edit: below is the code applying above steps

void process (void) {
  EndBal = balance;
  while(!feof(datafile)){
    count++;
    fgets(transcation, 11, datafile);
    fscanf(datafile,"%f\n", &amount);
    EndBal += amount;
    printf("%-14s%11.2f%20.2f%\n",transcation,amount,EndBal);
  }
}
Yılmaz Durmaz
  • 1,457
  • 9
  • 21
  • Ok, that makes sense. But the only issue is I still need to calculate from the 1,000 starting balance first and loop onward from there. So somewhere I do need to work in 1,000 because it is what gives the first net from the first amount (take in) and it calculates from there. – Eva May 07 '18 at 00:40
  • And balance does not change and must be put into the program (not the txt file). – Eva May 07 '18 at 00:46
  • I've put: net = amount + 1000; before the loop, and that seems to be producing everything correctly. If you disagree, please let me know. Thanks! – Eva May 07 '18 at 01:26
  • @Lizzy, I guess you didn't notice that it is in step 2 and 3 :)) choose either `net` or `EndBal` to continue. by the way, check what is `+=` if you need – Yılmaz Durmaz May 07 '18 at 01:43
  • @Lizzy, I added the code applying the steps I suggested. don't forget to select the answer if this is really what you need. – Yılmaz Durmaz May 07 '18 at 01:52