Why is my output file blank?

Nov 23, 2018 at 10:05pm
Learning file handling from this code by experimenting and modifying it. It is running properly, but output file is currently empty after adding a few things.

The code needs 2 files with these inputs:

salesMan.txt:
12345 Blue
32214 Red
23422 Green
57373 Yellow
35864 Black
54654 White

salesData.txt:
12345 1 893
32214 1 343
23422 3 903
57373 2 893
35864 5 329
54654 9 392
12345 2 999
32214 4 892
23422 4 895
23422 2 492
57373 6 892
35864 10 1223
54654 11 3420
12345 12 322
35864 5 892
54654 3 893
12345 8 494
32214 8 9023
23422 6 223
23422 4 783
57373 8 8834
35864 3 2882

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
const int noSalesPerson = 6;

struct salesPersonRec{
	string ID, name, mostID, leastID, mostName, leastName;
	double saleByQuarter[4], totalSale;
	int mostAmount, leastAmount;
};

void initialize(ifstream& indata, salesPersonRec list[], int listSize);
void getData(ifstream& infile, salesPersonRec list[], int listSize, salesPersonRec object);
void saleByQuarter(salesPersonRec list[], int listSize, double totalByQuarter[]);
void totalSaleByPerson(salesPersonRec list[], int listSize);
void printReport(ofstream& outfile, salesPersonRec list[], int listSize, double saleByQuarter[]);
void maxSaleByPerson(ofstream& outData, salesPersonRec list[], int listSize);
void maxSaleByQuarter(ofstream& outData, double saleByQuarter[]);
void saleInQuarter(ofstream& outData, salesPersonRec object);


int main(){
	ifstream infile;
	ofstream outfile;
	string inputFile;
	string outputFile;
	double totalSaleByQuarter[4];
	salesPersonRec salesPersonList[noSalesPerson], Object;
	cout<<"Welcome to SalesDataAnalysisProgram(improved)!\n\n";
	cout<<"Enter the sales person ID file name: ";
	cin>>inputFile;
	cout<<endl;
	infile.open(inputFile.c_str());
	if(!infile){
		cout<<"Cannot open the input file."<<endl;
		return 1;
	}
	initialize(infile, salesPersonList, noSalesPerson);
	infile.close();
	infile.clear();
	cout<<"Enter the sales data file name: ";
	cin>>inputFile;
	cout<<endl;
	infile.open(inputFile.c_str());
	if(!infile){
	    cout<<"Cannot open the input file.";
	    return 1;
	}
	cout<<"Enter the output file name: ";
	cin>>outputFile;
	cout<<endl;
	outfile.open(outputFile.c_str());
	outfile<<fixed<<showpoint<<setprecision(2);
	getData(infile, salesPersonList, noSalesPerson, Object);
	saleByQuarter(salesPersonList, noSalesPerson, totalSaleByQuarter);
	totalSaleByPerson(salesPersonList, noSalesPerson);
	printReport(outfile, salesPersonList, noSalesPerson, totalSaleByQuarter);
	maxSaleByPerson(outfile, salesPersonList, noSalesPerson);
	maxSaleByQuarter(outfile, totalSaleByQuarter);
	saleInQuarter(outfile, Object);
	infile.close();
	outfile.close();
}

void initialize(ifstream& indata, salesPersonRec list[], int listSize){
	for(int index=0;index<listSize;index++){
		indata>>list[index].ID>>list[index].name;
    	for(int quarter=0;quarter<4;quarter++){
    		list[index].saleByQuarter[quarter]=0.0;
    	}
    	list[index].totalSale=0.0;
	}
}

void getData(ifstream& infile, salesPersonRec list[], int listSize, salesPersonRec object){
	int index, quarter, month, mostIndex, leastIndex, mostQuarter, leastQuarter;
	string sID;
	double amount;
	infile>>sID;
	while(infile){
		infile>>month>>amount;
		for(index=0;index<listSize;index++){
			if(sID==list[index].ID){
	    		break;
      		}
		}
		if(1<=month and month<=3){
			quarter=0;
		}
		else if(4<=month and month<=6){
			quarter=1;
		}
		else if(7<=month and month<=9){
			quarter=2;
		}
		else{
			quarter=3;
		}
		if(index<listSize){
			list[index].saleByQuarter[quarter]+=amount;
		}
		else{
    		cout<<"Invalid salesperson's ID."<<endl;
		}
    	infile>>sID;
    	if(list[index].saleByQuarter[quarter]>0){
    		if(list[index].saleByQuarter[quarter]>list[leastIndex].saleByQuarter[leastQuarter]){
    			leastIndex=index;
    			leastQuarter=quarter;
			}
		}
		if(list[index].saleByQuarter[quarter]>list[mostIndex].saleByQuarter[mostQuarter]){
			mostIndex=index;
			mostQuarter=quarter;
		}
	}
	object.mostID=list[mostIndex].ID;
	object.leastID=list[leastIndex].ID;
	object.mostName=list[mostIndex].name;
	object.leastName=list[leastIndex].name;
	object.mostAmount=list[mostIndex].saleByQuarter[mostQuarter];
	object.leastAmount=list[leastIndex].saleByQuarter[leastQuarter];
}

void saleByQuarter(salesPersonRec list[], int listSize, double totalByQuarter[]){
	for(int quarter=0;quarter<4;quarter++){
		totalByQuarter[quarter]=0.0;
	}
	for(int quarter=0;quarter<4;quarter++){
    	for(int index=0;index<listSize;index++){
			totalByQuarter[quarter]+=list[index].saleByQuarter[quarter];
    	}
	}
}

void totalSaleByPerson(salesPersonRec list[], int listSize){
	for(int index=0;index<listSize;index++){
    	for(int quarter=0; quarter<4;quarter++){
		list[index].totalSale+=list[index].saleByQuarter[quarter];
    	}
	}
}

void printReport(ofstream& outfile, salesPersonRec list[], int listSize, double saleByQuarter[]){
	outfile<<"------------ Annual Sales Report ------------"<<endl;
	outfile<<endl;
	outfile<<"   ID"<<setw(10)<<"Name"<<setw(10)<<"QT1"<<setw(10)<<"QT2"<<setw(10)<<"QT3"<<setw(10)<<"QT4"<<setw(10)<<"Total"<<endl;
	outfile<<"_________________________________________________________________________________________"<<endl;
	for(int index=0;index<listSize;index++){
		outfile<<list[index].ID<<setw(10)<<list[index].name;
		for(int quarter=0;quarter<4;quarter++){
			outfile<<setw(10)<<list[index].saleByQuarter[quarter];
    	}
    	outfile<<setw(10)<<list[index].totalSale<<endl;
	}
	outfile<<"Total"<<setw(10)<<"    ";
		for(int quarter=0;quarter<4;quarter++){
    	outfile<<setw(10)<<saleByQuarter[quarter];
	}
	outfile<<endl<<endl;
}

void maxSaleByPerson(ofstream& outData, salesPersonRec list[], int listSize){
	int maxIndex=0;
	for(int index=1;index<listSize;index++){
		if(list[maxIndex].totalSale<list[index].totalSale){
		maxIndex=index;
    	}
	}
  outData<<"Max Sale by Sales Person: ID = "<<list[maxIndex].ID<<", Amount = $"<<list[maxIndex].totalSale<<endl;
}

void maxSaleByQuarter(ofstream& outData, double saleByQuarter[]){
	int maxIndex=0;
	for(int quarter=0;quarter<4;quarter++){
		if(saleByQuarter[maxIndex]<saleByQuarter[quarter]){
		maxIndex=quarter;
    	}
	}
	outData<<"Max Sale by Quarter: Quarter = "<<maxIndex+1<<", Amount = $"<<saleByQuarter[maxIndex]<<endl;
}

void saleInQuarter(ofstream& outData, salesPersonRec object){
	outData<<"Most Sale in All Quarters by Sales Person: ID = "<<object.mostID<<" Name = "<<object.mostName<<endl;
	outData<<"Amount = $"<<object.mostAmount<<endl;
	outData<<"Least Sale in All Quarters by Sales Person: ID = "<<object.leastID<<" Name = "<<object.leastName<<endl;
	outData<<"Amount = $"<<object.leastAmount<<endl;
}
Last edited on Nov 23, 2018 at 10:08pm
Nov 24, 2018 at 12:03am
line 78 none of your variables are initialized. Some of them are used before being initialized.
Nov 24, 2018 at 11:58am
Yep, that was my mistake. Thank you very much.
Topic archived. No new replies allowed.