Display output problem

Here's my problem, I cant seem to display the output. For the output we were supposed to display Item name and the Profit for each corresponding item.

This is my sales.txt it is arranged in this form "item, cost, sale price, number of sales".
1
2
3
4
5
Apacer_Thumb_Drive 180.00 190.00 3
Logitech_Wireless_Keyboard 120.00 140.00 1
Labtec_Stereo_Headset 70.00 75.00 2
Creative_Webcam 210.00 230.00 2
Max2_Floppy_Disk 15.00 18.00 5


Here is my 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
50
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
void readSales(ifstream &dataSales);
void CalcProfits(ifstream &dataSales, string &Item, double &Cost ,double &SalePrice, double &Profit, double &TotalProfit, double &SalesNumber)
;
void display(ifstream &dataSales, ofstream &outdataSales,string &Item, double &Cost ,double &SalePrice, double &Profit, double &TotalProfit, double &SalesNumber);

int main()
{
	ifstream dataSales;
	ofstream outdataSales;
	string Item;
	double Cost, SalePrice, Profit, TotalProfit, SalesNumber;
	readSales(dataSales);
	CalcProfits(dataSales, Item, Cost , SalePrice, Profit, TotalProfit, SalesNumber);
	display(dataSales, outdataSales, Item, Cost , SalePrice, Profit, TotalProfit, SalesNumber);
	return 0;
}

void readSales(ifstream &dataSales)
{
	dataSales.open("sales.txt");
}

void CalcProfits(ifstream &dataSales, string &Item, double &Cost ,double &SalePrice, double &Profit, double &TotalProfit, double &SalesNumber)
{
	TotalProfit=0;
	while (dataSales >> Item >> Cost >> SalePrice >> SalesNumber)
	{
		Profit=(SalePrice-Cost)*SalesNumber;
     	TotalProfit+=Profit;
	}
}

void display(ifstream &dataSales,ofstream &outdataSales, string &Item, double &Cost ,double &SalePrice, double &Profit, double &TotalProfit, double &SalesNumber)
{
	cout <<"REPORT SALES PC SHOP SDN BHD"<<endl;
	cout <<"__________________________________________"<<endl;
	cout <<" ITEM"<<setw(35)<<"PROFIT"<<endl;
	cout <<"__________________________________________"<<endl;
	while (outdataSales << Item << Cost << SalePrice << SalesNumber)
		{
			cout << Item << setw(20) << Profit;
		}
	cout <<"__________________________________________"<<endl;
	cout << "TOTAL PROFIT" <<setw(25) << TotalProfit<<endl;
}


This is the output I get
REPORT SALES PC SHOP SDN BHD
__________________________________________
 ITEM                             PROFIT
__________________________________________
__________________________________________
TOTAL PROFIT                      115
Last edited on
from what i see, the in the function "display", you are writing your data to your file only. to display the data, first read the data and then try to display. try put this code:
void CalcProfits(ifstream &dataSales, string &Item, double &Cost ,double &SalePrice, double &Profit, double &TotalProfit, double &SalesNumber)
{
TotalProfit=0;
while (dataSales >> Item >> Cost >> SalePrice >> SalesNumber)
{
Profit=(SalePrice-Cost)*SalesNumber;
outdataSales << Item << Cost << SalePrice << SalesNumber<<Profit
TotalProfit+=Profit;
dataSales.close();
}
}

and also

void display(ifstream &dataSales,ofstream &outdataSales, string &Item, double &Cost ,double &SalePrice, double &Profit, double &TotalProfit, double &SalesNumber)
{
cout <<"REPORT SALES PC SHOP SDN BHD"<<endl;
cout <<"__________________________________________"<<endl;
cout <<" ITEM"<<setw(35)<<"PROFIT"<<endl;
cout <<"__________________________________________"<<endl;
while (dataSales << Item << Cost << SalePrice << SalesNumber)
{
cout << Item << setw(20) << Profit;
}
cout <<"__________________________________________"<<endl;
cout << "TOTAL PROFIT" <<setw(25) << TotalProfit<<endl;
}


I tried your code, but it seems like the last code got an error. If I do it your way, I wont get my "TotalProfit". You see, in your "void CalcProfit" if I close the file in "while" I will never get "TotalProfit".
edit: I think I have to save each of my profit value into an array, but I dont really know how to do it. Any hint?
Last edited on
Topic archived. No new replies allowed.