Having problem with C++ file handling
- by caramel1991
Our lecturer has given us a task,I've attempted it and try every single effort I can,but I still struggle with one of the problem in it,here goes the question:
The company you work at receives a monthly report in a text format. The report contains the
following information.
• Department Name
• Head of Department Name
• Month
• Minimum spending of the month
• Maximum spending of the month
Your program is to obtain the name of the input file from the user. Implement a structure to
represent the data:
Once the file has been read into your program, print out the following statistics for the user:
• List which department has the minimum spending per month by month
• List which department has the minimum spending by month by month
Write the information into a file called “MaxMin.txt”
Then do a processing so that the Department Name, Head of Department Name, Minimum
spending and Maximum spending are written to separate files based on the month, eg Jan, Feb,
March and so on.
and of course our lecturer does send us a text file with the content:
Engineering Bill Jan 2000 15000
IT Jack Jan 300 20000
HR Jill Jan 1500 10000
Engineering Bill Feb 5000 45000
IT Jack Feb 4500 7000
HR Jill Feb 5600 60000
Engineering Bill Mar 5000 45000
IT Jack Mar 4500 7000
HR Jill Mar 5600 60000
Engineering Bill Apr 5000 45000
IT Jack Apr 4500 7000
HR Jill Apr 5600 60000
Engineering Bill May 2000 15000
IT Jack May 300 20000
HR Jill May 1500 10000
Engineering Bill Jun 2000 15000
IT Jack Jun 300 20000
HR Jill Jun 1500 10000
and here's the c++ code I've written
ue#include
include
include
using namespace std;
struct Record {
string depName;
string head;
string month;
float max;
float min;
string name;
}myRecord[19];
int main () {
string line;
ofstream minmax,jan,feb,mar,apr,may,jun;
char a[50];
char b[50];
int i = 0,j,k;
float temp;
//float maxjan=myRecord[0].max,maxfeb=myRecord[0].max,maxmar=myRecord[0].max,maxapr=myRecord[0].max,maxmay=myRecord[0].max,maxjune=myRecord[0].max;
float minjan=myRecord[1].min,minfeb=myRecord[1].min,minmar=myRecord[1].min,minapr=myRecord[1].min,minmay=myRecord[1].min,minjune=myRecord[1].min;
float maxjan=0,maxfeb=0,maxmar=0,maxapr=0,maxmay=0,maxjune=0;
//float minjan=0,minfeb=0,minmar=0,minapr=0,minmay=0,minjune=0;
string maxjanDep,maxfebDep,maxmarDep,maxaprDep,maxmayDep,maxjunDep;
string minjanDep,minfebDep,minmarDep,minaprDep,minmayDep,minjunDep;
cout<<"Enter file name: ";
cina;
ifstream myfile (a);
//minmax.open ("MaxMin.txt");
if (myfile.is_open()){
while (! myfile.eof()){
myfilemyRecord[i].depNamemyRecord[i].headmyRecord[i].monthmyRecord[i].minmyRecord[i].max;
cout << myRecord[i].depName<<"\t"<
cout<<"Enter file name: ";
cinb;
ifstream myfile1 (b);
minmax.open ("MaxMin.txt");
jan.open ("Jan.txt");
feb.open ("Feb.txt");
mar.open ("March.txt");
apr.open ("April.txt");
may.open ("May.txt");
jun.open ("Jun.txt");
if (myfile1.is_open()){
while (! myfile1.eof()){
myfile1myRecord[i].depNamemyRecord[i].headmyRecord[i].monthmyRecord[i].minmyRecord[i].max;
if (myRecord[i].month == "Jan"){
jan<< myRecord[i].depName<<"\t"<
if (maxjan< myRecord[i].max){
maxjan=myRecord[i].max;
maxjanDep=myRecord[i].depName;}
//if (minjan myRecord[i].min){
// minjan=myRecord[i].min;
//minjanDep=myRecord[i].depName;
//}
for (k=1;k<=3;k++){
for (j=0;j<2;j++){
if (myRecord[j].minmyRecord[j+1].min){
temp=myRecord[j].min;
myRecord[j].min=myRecord[j+1].min;
myRecord[j+1].min=temp;
minjanDep=myRecord[j].depName;
}}}
}
if (myRecord[i].month == "Feb"){
feb<< myRecord[i].depName<<"\t"<
//if (minfebmyRecord[i].min){
//minfeb=myRecord[i].min;
//minfebDep=myRecord[i].depName;
//}
for (k=1;k<=3;k++){
for (j=0;j<2;j++){
if (myRecord[j].minmyRecord[j+1].min){
temp=myRecord[j].min;
myRecord[j].min=myRecord[j+1].min;
myRecord[j+1].min=temp;
minfebDep=myRecord[j+1].depName;
}}}
}
if (myRecord[i].month == "Mar"){
mar<< myRecord[i].depName<<"\t"<
if (myRecord[i].month == "Apr"){
apr<< myRecord[i].depName<<"\t"<
if (minaprmyRecord[i].min){
minapr=myRecord[i].min;
minaprDep=myRecord[i].min;}
}
if (myRecord[i].month == "May"){
may<
if (minmaymyRecord[i].min){
minmay=myRecord[i].min;
minmayDep=myRecord[i].depName;}
}
if (myRecord[i].month == "Jun"){
jun<< myRecord[i].depName<<"\t"<
if (minjunemyRecord[i].min){
minjune=myRecord[i].min;
minjunDep=myRecord[i].depName;}
}
i++;
myfile.close();
}
minmax<<"department that has maximum spending at jan "<
else{
cout << "Unable to open file"<
}
sorry inside that code ue#include should has iostream along with another two #include fstream and string,but at here it was treated as html tag,so i can't type it.
my problem here is,I can't seems to get the minimum spending,I've try all I can but I'm still lingering on it,any idea??THANK YOU!