How do I get this output to a file.....I can get it to print on the screen, but I can't figure it out for the life of me....

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void abc(ifstream&);

void main()
{
	ifstream inFile;
	ofstream outFile;

	double courseAvg;
	int numberoftests;
	string Name;

	inFile.open("tests.txt");
	outFile.open("finalgrades.txt");

	numberoftests = 0;
	courseAvg = 0.0;
	outFile << fixed << showpoint;
	outFile << setprecision(1);

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl;
	}
	else
	{
		outFile.open("finalgrades.txt");
		outFile << fixed << showpoint;
		outFile << setprecision(1);

		for (int i =1; i <= 10; i++)
		{
			inFile >> Name;  
			cout << Name << " ";
			outFile << Name << " ";
			abc(inFile);
		}
		
	}
	cin >> Name;
}//End of Main

void abc(ifstream& x)
{
	int gr;

	for (int i = 1; i <= 5; i++)
	{
		x >> gr;
		cout << " " << gr;
	}
	cout << endl;
}

Recommended Answers

All 4 Replies

I notice you open the output file twice: once at line 20 and then again at 33. Perhaps delete line 20.

And your sub-routine abc uses cout to output to the console, but does not output to the output file. In fact, I see no output to the output file other than the name.

I notice you open the output file twice: once at line 20 and then again at 33. Perhaps delete line 20.

And your sub-routine abc uses cout to output to the console, but does not output to the output file. In fact, I see no output to the output file other than the name.

What do I need to make it output to an output file....I know "cout" makes it appear in a console, but what would be the code to output to a certain file?

How do I get this output to a file.....I can get it to print on the screen, but I can't figure it out for the life of me....

what should I include, in order to output to a file?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void abc(ifstream&);

void main()
{
	ifstream inFile;
	ofstream outFile;

	double courseAvg;
	int numberoftests;
	string Name;

	inFile.open("tests.txt");
	outFile.open("finalgrades.txt");

	numberoftests = 0;
	courseAvg = 0.0;
	outFile << fixed << showpoint;
	outFile << setprecision(1);

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl;
	}
	else
	{
		outFile.open("finalgrades.txt");
		outFile << fixed << showpoint;
		outFile << setprecision(1);

		for (int i =1; i <= 10; i++)
		{
			inFile >> Name;  
			cout << Name << " ";
			outFile << Name << " ";
			abc(inFile);
		}
		
	}
	cin >> Name;
}//End of Main

void abc(ifstream& x)
{
	int gr;

	for (int i = 1; i <= 5; i++)
	{
		x >> gr;
		cout << " " << gr;
	}
	cout << endl;
}

I've tried making the void abc(ofstream x) loop, but it still won't work
what should I include, in order to output to a file?

I've tried making the void abc(ofstream x) loop, but it still won't work
what should I include, in order to output to a file?

Does the input and output routine have to be in a sub-routine? If not, why not have the loop in the main program. Then you could do the file output loop the same way you output Name on line 41 (and eliminate sub-routine abc).

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.