Finding the sum of all the values of a variable.

Hello, I'm writing a program for my c++ class that requires me to read data from a file and the profits from each sales person. At the end i need to figure out the the total profits of all salespeople. However since i used a loop to process the data, I am a bit confused about how to calculate the total profits. I shall post my source code and the data file that goes along with it. Thanks to whoever decides to help.


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void openFile(void);
void process(void);
void calculate(void);
void getData(void);
void accumulate(void);
void printFormat(void);

char PT1[] = "\nAlhaji Kanu GTC SALES REPORT 02/26/13 ";
char CL1[] = "Salesperson Name: ";
char CL2[] = "Total Sales: ";
char CL3[] = "Cost of Sales: ";
char CL4[] = "Net Profit: ";

ifstream inFile;
string salesPerson;
double totalSales;
double salesCost;
double netProfit;
double totalNet;




int main()
{
openFile();
if (!inFile.fail())
{
process();
accumulate();
inFile.close();
}

return 0;
}

void openFile(void)
{
inFile.open ("C:\\salesreport.txt");

return;
}

void process(void)
{
getData();
while(salesPerson != "stop")
{
calculate ();
printFormat();
getData();
}

return;
}

void getData(void)
{
inFile >> salesPerson;
inFile >> totalSales;
inFile >> salesCost;

return;
}

void calculate (void)
{
netProfit = totalSales - salesCost;

return;
}

void printFormat(void)
{
cout << PT1 << endl;
cout << endl << endl;
cout << CL1 << "" << salesPerson << endl;
cout << CL2 << "" << totalSales << endl;
cout << CL3 << "" << salesCost << endl;
cout << CL4 << "" << netProfit << endl;

return;
}

void accumulate(void)
{
totalNet = netProfit;
cout << "\nThe total net profit is: $" << totalNet << endl;

return;
}

(can't find an attach,ents button so here is the data)
Hickle 2245.78 1072.49
Persells 11710.14 9735.38
Tipay 4567.51 3119.22
Zuller 5793.59 4204.45
stop
You're calling accumulate once in your main line. Also, accumulate isn't accumulating anything.

You want to initialize totalNet to zero. Then EACH time you canculate netProfit, you want to add it to totalNet. Then when you're done, you can print totalNet.

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to read your code, makes it easier to respond to your question, and improves the chances of getting a response.
Thank a lot. I was able to get it to compile and show the right profit. Again, thanks for your help.


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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void openFile(void);
void process(void);
void calculate(void);
void getData(void);
void accumulate(void);
void printFormat(void);

char PT1[] = "\nAlhaji Kanu               GTC SALES REPORT               02/26/13 ";
char CL1[] = "Salesperson Name: ";
char CL2[] = "Total Sales: ";
char CL3[] = "Cost of Sales: ";
char CL4[] = "Net Profit: ";

ifstream  inFile;
string salesPerson;
double totalSales;
double salesCost;
double netProfit;
double totalNet=0;




int main()
{
	openFile();
	if (!inFile.fail())
	{
		process();
		accumulate();
		inFile.close();
	}
	
	return 0;
}

void openFile(void)
{
	inFile.open ("C:\\salesreport.txt");

	return;
}

void process(void)
{
	getData();
	while(salesPerson != "stop")
	{
		calculate ();
		printFormat();
		getData();
	}

	return;
}

void getData(void)
{
	inFile >> salesPerson;
	inFile >> totalSales;
	inFile >> salesCost;

	return;
}

void calculate (void)
{
	netProfit = totalSales - salesCost;
	totalNet += netProfit;
	
	return;
}

void printFormat(void)
{
	cout << PT1 << endl;
	cout << endl << endl;
	cout << CL1 << "" << salesPerson << endl;
	cout << CL2 << "" << totalSales << endl;
	cout << CL3 << "" << salesCost << endl;
	cout << CL4 << "" << netProfit << endl;

	return;
}

void accumulate(void)
{
	cout << "\nThe total net profit is: $" << totalNet << endl;

	return;
}
Topic archived. No new replies allowed.