Sequential read/write

Hi all,

Im working on a program and I am somewhat stuck on one of the functions. I am trying to read in bank accounts and perform operations on them. I want to pay interest to accounts with a positive savings, move negative savings to credit and charge interest, and charge interest on positive credit balances. The program requires a file called a1.txt to read in values.
a1.txt contains the following values:
Jim 23.41 0
Bob 58.32 0
Sally -1.34 0

My problem is this, when I run the program I can get only Sally to calculate. The first two arent outputting. I think it has something to do with my while (line 80) loop calculations but I can't figure out what it is. Any insight would be appreciated. Here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void ProcessAccounts(std::ifstream &ifile, std::ofstream &ofile, double rate)
{

  // 1) a positive savings balance, pay them interest
  // 2) a negative savings balance, transfer it to credit (credit is positive)
  // 3) a credit balance, charge them interest

std::string name;
double savings;
double credit;
rate = (rate/100) + 1;

SetInputStreamPos(ifile, 0);


while (ifile >> name >> savings >> credit)

{

	if (savings > 0)

	{
		savings = savings * rate;
	}

	else if (savings < 0)

	{
		credit= savings * (-1);
		credit = (credit * rate);
		savings=0;
	}

 	else if (credit > 0)

	{
		credit*=rate;
	}




}

ofile << name << " " << savings << " " << credit << std::endl;
 
} 
 
Last edited on
My read here is that since the output on line 107 is outside the while loop, it only gets called once after the third record (Sally) has been read in and processed. So the variables for name, savings and credit are holding Sally's info. The info for the previous accounts is overwritten with every loop through the while structure.

I'd try moving the output line into the while loop at the end, so you write out the results of the current account before reading in a new line and overwriting data.

You sir, are a genius. Ive been ripping my hair out the last couple days trying to figure out what was wrong with my syntax. Thank you!!!
You're welcome - glad that helped. Sometimes it's hard to see things in code you've been working on for so long.

- wildblue (neither a genius nor a sir :) )
oh, Madam. I feel like Michael scott from the office lol https://www.youtube.com/watch?v=AR8cSyL7WhU
Topic archived. No new replies allowed.