Did a quick search but couldn't find anything on ostream and multiple files depending on data.

I'm pretty sure it's something very simple flying over my head, but I can't seem to get this to work. Basically what I want to do is write to a file depending on the data. If the student has a certain amount of credits they're written to one file, if they don't have this certain amount, they're written to another. Everything compiles fine, the first file even gets created (outfile1). I can't get the 2nd file to write though. Any help is greatly appreciated.

Here is a snippet.

for( int i = 0 ; i < numOfLines; i++)
			{
				inFile >> fname >> lname >> d >> n >> c;
				x[i].setF(fname);
				x[i].setL(lname);
				x[i].setD(d);
				x[i].setN(n);
				x[i].setCredit(c);

				if ( c >= 12)
				{				
				x[i].printToFile(outFile1);				
				}
				else
				{
				x[i].printToFile(outFile2);
				}
			}

ouFile1 and outFile two have already been opened.

You'll have to show us the printToFile function.

I also recommend

if ( c >= 12)
{				
x[i].printToFile(outFile1);				
std::cout << "File1" << std::endl;
}
else
{
x[i].printToFile(outFile2);
std::cout << "File2" << std::endl;
}

to make sure the conditional is triggered correctly.

Dave

Here it is

void Student::printToFile(ofstream &dataout)
{
	dataout << fname << " " << lname;
	if(credits >= 12)
		dataout << " You registered for  "<<credits<<" credits, your tuition is $2675.00"<<endl;
	else if(credits >= 9 && credits < 12)
		dataout <<" You registered for "<<credits<<" credits, your tuition is $2006.50"<<endl;
	else if(credits >=6 && credits < 9)
		dataout <<" You registered for "<<credits<<" credits, your tuition is $1350.50"<<endl;
	else if(credits < 6)
		dataout <<" You registered for "<<credits<<" credits, your tuition is $670.00"<<endl;
}

Did you do what I said to check which file was being written? Also, show us the declaration of the ofstreams. In fact, why don't you make a < 20 line example that demonstrates the problem.

Dave

I'll just post the entire thing (should've done so to begin with)

#include "Person.h"
int main()
{
	string lname;
	string fname;
	string d;
	string line;
	int n;
	int c;
	int numOfLines;
	ifstream inFile;
	ofstream outFile1, outFile2;

	inFile.open("C:\\Temp\\names.txt");
	outFile1.open("C:\\Temp\\less_Than_12.txt");
	outFile2.open("C;\\Temp\\12_or_More.txt");

	if ( !inFile )
	{
		cout << " Can't open the input file." << endl;
		cin.get();
		return 1;
	}

	while( getline(inFile, line) )
	{
		numOfLines++;
	}

	inFile.clear();
	inFile.seekg(0);

	cout << "The file contained " << numOfLines << " lines.\n\n";
	Student *x = new Student[numOfLines];

	for( int i = 0 ; i < numOfLines; i++)
			{
				inFile >> fname >> lname >> d >> n >> c;
				x[i].setF(fname);
				x[i].setL(lname);
				x[i].setD(d);
				x[i].setN(n);
				x[i].setCredit(c);
				if ( c >= 12)
				{
				
				x[i].print();
				x[i].printToFile(outFile1);
				
				}
				else
				{
				
				x[i].print();
				x[i].printToFile(outFile2);
				
				}
			}

	return 0;
}

I added x.print, which prints to display, like you suggested.


void Student::print()
{
	Person::print();
	if(credits >= 12)
		cout<<" You registered for  "<<credits<<" credits, your tuition is $2675.00"<<endl;
	else if(credits >= 9 && credits < 12)
		cout<<" You registered for "<<credits<<" credits, your tuition is $2006.50"<<endl;
	else if(credits >=6 && credits < 9)
		cout<<" You registered for "<<credits<<" credits, your tuition is $1350.50"<<endl;
	else if(credits < 6)
		cout<<" You registered for "<<credits<<" credits, your tuition is $670.00"<<endl;
}

Sure enough, every line in the file gets printed to the screen, but I already knew that (I was using the display before trying files).

My problem is that only one file is being created, which is the file that contains all students with 12 or more credits.

I just realized I had a semi-colon in the directory of the 2nd file I wanted to open right after pasting that.

So it works now? If so great, please mark the thread as solved.

Dave

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.