hey guys/gals. how do i save to a file the data that is printed by this program i wrote.
I have the code to do this in the createFile function but i am unsure in how to output what was printed. Any suggestions?
//class member functions
#include "experiment.h"
Experiment::Experiment()
{
numberOfTests = 0;
}
void Experiment::setExpName()
{
cin.ignore(1);
cout << "Please enter a tag name for this experiment: ";
getline(cin, name);
}
string Experiment::getExpName()
{
return name;
}
void Experiment::setPersonForExperiment()
{
cout << "Please enter the name of person responsible for conducting this experiment: ";
getline(cin, who);
}
string Experiment::getPersonForExperiment()
{
return who;
}
void Experiment::setNumOfTests()
{
cout << "\nHow many tests would you like to run?";
cin >> numberOfTests;
}
int Experiment::getNumOfTests()
{
return numberOfTests;
}
void Experiment::runExperiment()
{ char answer, decision, choice;
cout << "Welcome to the \"I want to be Happy Experiment\" ";
cout << "\nDo you want to enter a new experiement?(Y/N) ";
cin >> answer;
while(answer == 'Y')
{
setExpName();
setPersonForExperiment();
date.setExpDate();
setNumOfTests();
for (int i = 0; i < getNumOfTests(); i++ )
{
cout << "\tEnter (W) to enter Weather data, (S) to enter Sports data: ";
cin >> decision;
switch (decision)
{
case 'W':
tests[i] = new WeatherTest;
tests[i]->getTestInfo();
cout << "\n";
break;
case 'S':
tests[i] = new SportTest;
tests[i]->getTestInfo();
cout << "\n";
break;
default:
cout << "Do not recognize type... try again." << endl;
}
}
cout << "\n\tDo you still want to enter another experiment(Y/N)? ";
cin >> answer;
}
print();
cout << "\n\tDo you want to save this information in a file(Y/N)? ";
cin >> choice;
createFile(choice);
}
void Experiment::print()
{
cout << left << "Name: " << getExpName() << right << setw(50) << "Date: ";
date.print();
cout << endl;
cout << left << "Who: " << getPersonForExperiment() << right << setw(60) << "Total Tests Entered: " << getNumOfTests() << endl;
cout << "\nData" << setw(15) << "Tester" << setw(15) << "Date" << setw(15) << "pos" << setw(15) << "neg" << setw(15) << "ind" << endl;
for( int i = 0; i < getNumOfTests(); i++)
{
tests[i]->print();
cout << "\n";
}
}
void Experiment::createFile(char c)
{
if(c == 'Y')
{
ofstream outClientFile("HappyExperimentTake1.txt", ios::out);
if(!outClientFile)
{
cerr << "\n\tFile could not be opened." << endl;
exit(1);
}
//for( int i = 0; i < getNumOfTests(); i++)
//{
//outClientFile tests[i]->print();---how do i save to a file what was printed by the program
//cout << "\n";
//}
cout << "\n\tFile saved to HappyExperimentTake1.txt.";
}
}