Hello,
I am wanting to read a data file that is placed in my project file where the driver.cpp, function.cpp and function.h are located.
I want to the user create an output file name. How can I do this and where should it be placed?
Also I put my other question below some of this code.
Thanks for the help!
Driver.cpp
Code:
// Driver.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Function.h"
using namespace std;
int main ()
{
//vars
int flowData [400];
string Dates [400];
int highIndex, lowIndex;
Header();
int total = ReadFile(Dates, flowData);
bool Write;
////Problems reading or getting file; Closes program
//if (total == -99)
//{
// cout <<"\nTrouble Reading input file, closing program";
// exit(1);
//}
AnalyzeFile(flowData, total, highIndex, lowIndex);
Write = WriteFile();
cout << "\n";
return 0;
}
Function.cpp
Code:
// Function.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Function.h"
using namespace std;
#define FILE "FlowData.csv"
#define FILEOUT "FlowDataResults.txt"
void Header ()
{
cout <<"\nProgram: 7 Part: 2"
<<"\nData from http://www.usbr.gov/lc/region/g4000/NaturalFlow/NaturalFlows1906-2008_withExtensions_1.26.11.xlsx"
<<"\nObject: Web Research & Reading & Writing Data Files\n\n";
}
int ReadFile(string Dates [], int flowData [])
{
ifstream input;
input.open(FILE);
//Checking file and seeing if I can open it
if (!input)
{
cout <<"\nCan't open file, exiting!";
return false;
}
//Reading the file
cout <<"\nReading: " << FILE << endl;
int i = 0;
while (!input.eof() )
{
getline(input, Dates[i], ',');
input.ignore();
++i;
}
//Telling user where the location is
cout <<"Located: Folder" << endl;
cout <<"Written: Yes";
cout <<"\n\nGood Bye";
input.close();
return i;
}
void AnalyzeFile(int flowData [], int total, int &rHigh, int &rLow)
{
rHigh = rLow = 0;
int highFlow, lowFlow;
highFlow = lowFlow = flowData[0];
for(int i = 1; i < total; ++i)
{
//Seeing the low flow
if (flowData[i] < lowFlow)
{
lowFlow = flowData[i];
rLow = i;
}
//Seeing the high flow
if (flowData [i] > highFlow)
{
highFlow = flowData [i];
rHigh = i;
}
//Displays the results with high and low flows along with the acre feet per month
//Need to put this somewhere so it can print the results
cout << "\nThe highest flow was " << flowData[rHigh];
cout << "\nAcre-feet per month in " << flowData[rHigh] << endl;
cout << "\nLowest flow was " << flowData[rLow];
cout << "\nAcre-feed per month in " << flowData[rLow] << endl;
}
}
bool WriteFile()
{
//Asking user for file name
ofstream output;
output.open(FILEOUT);
//Checking to see if I can open the results text file
if (!output)
{
cout <<"\nFile cannot open!";
return false;
}
//printing the results in a text file .txt
output <<"C++ Data Created By: "
<<"\nThanks for using the C++ Data Reader";
output.close();
return true;
}
I'm also wanting to know where I should put these two lines at.
Code:
//Displays the results with high and low flows along with the acre feet per month
//Need to put this somewhere so it can print the results
cout << "\nThe highest flow was " << flowData[rHigh];
cout << "\nAcre-feet per month in " << flowData[rHigh] << endl;
cout << "\nLowest flow was " << flowData[rLow];
cout << "\nAcre-feed per month in " << flowData[rLow] << endl;
I don't want to show the results to the user in the cmd, but want to show the results in a text file the user created.
Function.h
Code:
// Function.h
#include <string>
using namespace std;
void Header();
int ReadFile(string Dates [], int flowData []);
void AnalyzeFile(int flowData [], int total, int &rHigh, int &rLow);
bool WriteFile();