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.";
	}
}

Recommended Answers

All 2 Replies

well you need to use fstreams. Namely a ofstream. you can create a ofstream like this

ofstream fout("experiment.dat");

// then use fout like cout

fout << name << title << whatever;

the file streams have the same functions and work the same as the console and keybord streams. here is a link for ofstream http://www.cplusplus.com/reference/iostream/ofstream/

Hey,

You may also consider using Standard I/O of C. It's just simple that; with fopen() open a file, with fprintf() write to the file and with fclose() close the file when you're done.

Check out the link:
http://www.cppreference.com/wiki/c/io/start

-hakan

commented: This is the C++ forum. I'm sure instructors would frown on using C functions when C++ is available. -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.