I need some help please in c++. I'm trying to process different files so have created a main, MonthlyFiles and DaysData files. My problem is when I run the program it crashes when trying to save the output file (i.e. the DaysData - it ouputs the information to the screen and then stops responding.
Main:
Expand|Select|Wrap|Line Numbers
- #include <string>
- #include <stdlib.h>
- #include "MonthlyFiles.h"
- #include "DaysData.h"
- using namespace std;
- int main()
- {
- int option;
- MonthlyFiles m;
- do
- {
- cout<<"Please select a number from the menu below to execute the needed operation:" << '\n';
- cout<<"1: To process August Electricity consumption for each day."<<'\n';
- cout<<"2: To process September Electricity consumption for each day."<<'\n';
- cout<<"3: To process the Electricity consumption for the two months together."<<'\n';
- cout<<"4: Quit"<<'\n';
- cout<<'\n';
- cout<<"Please input option: ";
- cin>> option;
- cout<<'\n';
- switch(option)
- {
- case 1:m.process();
- break;
- case 2:m.processSept();
- break;
- case 3:m.processboth();
- break;
- case 4:cout<<"bye";
- exit(0);
- default:
- cout<<"Please enter a valid option";
- cout << '\n';
- break;
- }
- }while(option!=1);
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- #include "MonthlyFiles.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- void MonthlyFiles::Clear()
- {
- string temp = "";
- }
- std::string MonthlyFiles::process()
- {
- ifstream infile;
- vector<string> fileNames = vector<string>(); //Creates a new array called fileNames to store the names of the filenames
- vector<DaysData> dataVector = vector<DaysData>(); //Creates a new array called DataVector to store the data of each day.
- infile.open( "Aug.txt"); // Retrieves the file names for a the month from a particular txt.
- if( !infile)
- {
- cout << "Cannot open input file"<< endl; // If file cannot open display an error message.
- //return -1;
- }
- while( !infile.eof()) //loop through the file till the end of the contents
- {
- getline(infile, temp); // Process each line
- fileNames.push_back(temp); //Add each filename to the array
- }
- int i;
- for(i = 0; i < fileNames.size(); i ++)
- {
- //This part of the code opens each file up and adds it to a new Daysdata array
- ifstream dataFile;
- string fileLoc = "Data/"; //The location of the file.
- dataFile.open((fileLoc+fileNames.at(i)).c_str()); //This opens the new data file for a particular file.
- if(dataFile.is_open())
- {
- DaysData d = DaysData();
- dataFile >> d;
- dataVector.push_back(d);
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- #include "DaysData.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- DaysData::DaysData()
- {
- }
- void DaysData::Clear()
- {
- string line = "";
- }
- void operator >>(istream & infile, DaysData & D)
- {
- string m_csvline;
- ofstream writeFile;
- writeFile.open("2001-Aug.csv", ios::app);
- while(!infile.eof())
- {
- getline(infile, m_csvline); // Retrieves each line in the csv file.
- string tempcsv = m_csvline; //assigns it to a string variable
- D.myVec.push_back(tempcsv); //adds the data from above into the vector.
- }
- string line1=D.myVec.at(9);
- string lastline=D.myVec.at(D.myVec.size()-2);
- D.date.setDates(line1);
- D.date.setDates(lastline);
- float powerline1=D.powers.setPower(line1);
- float powerlastline=D.powers.setPower(lastline);
- float power=powerlastline-powerline1;
- cout<<D.date.getDates() <<";"<< power<<endl;
- //This section here concatenates temp1 so that it can be sent to the output file.
- stringstream temp1;
- temp1 << D.date.getDates() << ";" << power << '\n';
- string out = temp1.str();
- writeFile << out; //write the data to the output file
- writeFile.close();
- }