### 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.

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 #include "stdafx.h" #include #include #include 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.