Correct and Incorrect Calculation inside of a loop! Why?

I need help trying to figure out why my calculations seem off. I'm using the following file.
ABC 23 18.50 23.25 1.00 1.30
XYZ 5 226.20 175.00 5.00 4.00

My calculation for ProfitOrLoss are wrong. I worked them out with paper and pencil and the results are not the same.
For the ABC line my calculations are correct to what my program shows and what I get by doing the math on paper.
For the XYZ I get a negative number, but the number is also wrong. I do not know why it would be causing this problem.

Here is a sample output using the file I typed out above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Please open the file associated with the Multiple Stock Sales.

Type the name of the file.
Example Format: StockPrices.txt

Please Enter Here: Shares.txt
Shares.txt has opened successfully.

Stock  Shares   P Price   S Price  Profit (Loss)
_____  ______   _______   _______  _____________
ABC        23     18.50     23.25         106.95
XYZ         5    226.20    175.00        -265.00

Total Profit (Loss) for 28 shares = $ -158.05
Press any key to continue . . .


When I calculated the answer manually with paper and pencil for XYZ I got $247.
What is wrong here?

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

bool ReadFile (ifstream&, string);
float ProfitFunction (ifstream&, string, string, int&, float&, float&, float&, float&, float&, int&);

int main ()
{
	ifstream inFile;
	string filename = "Shares.txt";
	bool IsItOpen = true;
	string StockName;
	int NUMofShares, TotalShares = 0;
	float PurchasePrice, SalePrice, PurchaseCommission, SaleCommission, ProfitOrLoss, TotalProfitOrLoss = 0;
	

	cout << "Please open the file associated with the Multiple Stock Sales.\n" << endl;

	inFile.open(filename);
	IsItOpen = ReadFile(inFile, filename);

	if (IsItOpen == false)
	{
		cout << "Closing down the program due to file error..." << endl;
		return -1;
	}
	inFile.close();

	TotalProfitOrLoss = ProfitFunction (inFile, filename, StockName,  NUMofShares, PurchasePrice, SalePrice,
										PurchaseCommission, SaleCommission, ProfitOrLoss, TotalShares);

	cout << "\nTotal Profit (Loss) for " << TotalShares << " shares = $ " << TotalProfitOrLoss << endl;

	system("pause");
	return 0;
}

bool ReadFile (ifstream& myFile, string NameOfFile)
{
	cout << "Type the name of the file." << endl;
	cout << "Example Format: StockPrices.txt" << endl;
	cout << "\nPlease Enter Here: ";
	cin >> NameOfFile; //prompting user to enter the filename

	if (myFile.is_open() && NameOfFile == "Shares.txt")//validating the filename that was entered
	{
		cout << NameOfFile << " has opened successfully.\n" << endl;
		return true; 
	}
	else //if an error occurred opening the file, the program will close
		return false;
}

float ProfitFunction (ifstream& inFile, string filename, string StockName, 
					  int& NUMofShares, float& PurchasePrice, float& SalePrice,
					  float& PurchaseCommission, float& SaleCommission, float& ProfitOrLoss, int& TotalShares)
{
	float TotalProfit = 0;

	cout << "Stock " << setw(5) << " Shares " << setw(10) << " P Price " << setw(10);
	cout << " S Price " << setw(10) << " Profit (Loss)" << endl;
	cout << "_____ " << setw(5) << " ______ " << setw(10) << " _______ " << setw(10);
	cout << " _______ " << setw(10) << " _____________" << endl;

	inFile.open(filename);

	while (!inFile.eof())
	{
		inFile >> StockName;
		inFile >> NUMofShares;
		inFile >> PurchasePrice;
		inFile >> SalePrice;
		inFile >> PurchaseCommission;
		inFile >> SaleCommission;

		if(!inFile) break; // Checks if file operation is a success

		//Formula for calculating Profit/Loss
		ProfitOrLoss = ((NUMofShares * SalePrice) - SaleCommission) - ((NUMofShares * PurchasePrice) + PurchaseCommission);
		
		cout << left << StockName << ' ';
		cout << setw(9) << right << NUMofShares << ' ';
		cout << fixed << setprecision(2);
		cout << setw(9) << right << PurchasePrice << ' ';
		cout << setw(9) << right << SalePrice << ' ';
		cout << setw(14) << right << ProfitOrLoss << endl;

		TotalShares += NUMofShares;
		TotalProfit += ProfitOrLoss;
	}

	inFile.close();

	return TotalProfit;
}
Last edited on
Line 27: try to initialize all your variables

and look on the file.txt the value of saleCommission or purchasecommission
Last edited on
ar2007,

Did you mean Line 17? If so, I just tried that. It still produces the same result.
I coded the variables to look at both Sale and Purchase Commission and the values are correct.
Last edited on

yes line 17. sorry
try adding at the end of the while loop () (line 70)

InFile.ignore(256,' ' ) ;
Last edited on
ar2007,

Add inFile.ignore(256, ' '); at around line 92 or 93 you mean?

If so, I tried that and it made the calculation way worse. I'm getting very incorrect answers. I still need help.
My calculation for ProfitOrLoss are wrong

Can you give us the formula how the profit and how the loss are calculated?
Mantorr22,

Here's the formula from the book.
If the value is positive that is the profit. If the value is negative that is the loss.

Profit (or Loss) = (( NS * SP ) - SC ) - (( NS * PP ) + PC )
NS = Number of Shares
SP = Sale Price per Share
SC = Sale Commission Paid
PP = Purchase Price per Share
PC = Purchase Commission

In the file that I am reading, if I organized the text nicely here. I'll show you what each value in the text file corressponds with.

Stock Name|NS| PP | SP | PC | SC |
ABC 23 18.50 23.25 1.00 1.30
XYZ 5 226.20 175.00 5.00 4.00

Edit: The text doesn't look aligned here when I post it for whatever reason. But try to follow the guide lines of (Stock Name, Number of Shares, Purchase Price per Share, etc.)
Last edited on
Stock Name|NS| PP | SC | PC | SC |
ABC 23 18.50 23.25 1.00 1.30
XYZ 5 226.20 175.00 5.00 4.00

Why are there two columns named "SC"? I expect them to be different?
Mantorr22,

The SC after PP should be SP.

Should be Stock Name, NS, PP, SP, PC, SC. In that order.

I'll change it. But I still need help.
I worked them out with paper and pencil and the results are not the same.

What do you mean? Can you give us a sample calculation from yourself?
Yes of course,

Here is a calculation for stock ABC by hand.

((23 * 23.25) - 1.30) - ((23 * 18.50) + 1.00)
= 533.45 - 426.50
= 106.95 (This is correct in my program and calculation by hand as both are the same result)

Here is a calculation for stock XYZ by hand.

((5 * 226.20) - 4.00) - ((5 * 175.00) + 5.00)
= 1,127 - 880
= 247 (The program when ran shows an answer of -265.00)

EDIT: On XYZ when doing it by hand I just realized that I flipped some values.
It should be set up like this.

((5 * 175.00) - 4.00) - ((5 * 226.20) + 5)
= 871 - 1136
= -265 (The program's answer is actually correct afterall)
Last edited on
Mantorr22,

Thank you for your help. It looks like the program is calculating fine afterall. I just made some mistakes when I calculated it out manually.

Problem Solved.
Topic archived. No new replies allowed.