Hello all,
I was just wondering why this function does not write the header to the output file? The cout works fine, but it isn't writing to the output file. What am I missing or doing wrong? Also, this is the first time I've passed the input/output stuff, so am I doing that correctly? Well, I guess I'm not, or it would be working, right?
#include <iostream>
#include <fstream> //Input/output files
#include <string> //For strings
#include <sstream> //For stringstream()
#include <iomanip> //For setw()
usingnamespace std;
//Declare constants
constint MAX_RECORDS = 6;
constint MAX_DATES = 150;
//This is a hierarchical struct since Date is a struct type within member list of MachineRecord
struct Date
{
int month;
int day;
int year;
};
struct MachineRecord
{
string machineNbr;
string name;
Date InstallationDate;
int everyXDays;
int serviceTimeInDays;
Date workDays[MAX_DATES];
};
//Create an array of machine records
MachineRecord pMSchedule[MAX_RECORDS];
//Function prototypes
void DisplayHeader(ofstream& outfile);
int OpenInputOutput(ifstream& infile, ofstream& outfile);
int main()
{
//File descriptors
ifstream infile;
ofstream outfile;
DisplayHeader(outfile);
OpenInputOutput(infile, outfile);
//Close input/output files
infile.close();
outfile.close();
return 0;
}
//*************************************************************************************************
//Function displays my class and personal info
void DisplayHeader(ofstream& outfile)
{
cout << "Erin Corona" << endl
<< "CS 318.20181" << endl
<< "Scheduler Time Maintanence Modified Again - Program 12" << endl
<< "Due Tuesday, March 20, 2012" << endl << endl;
outfile << "Erin Corona" << endl
<< "CS 318.20181" << endl
<< "Scheduler Time Maintanence Modified Again - Program 12" << endl
<< "Due Tuesday, March 20, 2012" << endl << endl;
}//End of function
//************************************************************************************
//Function opens input and output files.
int OpenInputOutput(ifstream& infile, ofstream& outfile)
{
infile.open("machines.txt"); //Open text file to read from
outfile.open("machinesAgain.out"); //Open textfile to write to
if (!infile) //If input file did not open correctly
{
cout << "Error opening input file." << endl;//Display error message
outfile << "Error opening input file." << endl;//Display error message
return 1; //Quit
}
else
{
cout << "Input file opened correctly." << endl;//Display success message
outfile << "Input file opened correctly." << endl;
}
if (!outfile) //If outfile does not open
{
cout << "Error opening output file." << endl;//Display error message
outfile << "Error opening output file." << endl;//Display error message
return 1; //Quit
}
else
{
cout << "Output file opened correctly." << endl << endl << endl;//Display success message
outfile << "Output file opened correctly." << endl << endl << endl;
}
}//End of function
The cout looks like this:
Erin Corona
CS 318.20181
Scheduler Time Maintanence Modified Again - Program 12
Due Tuesday, March 20, 2012
Input file opened correctly.
Output file opened correctly.
But the output file only does this:
Input file opened correctly.
Output file opened correctly.
All I'm asking is how do I get the header stuff (my name and class info) to show up in the output file?
Oh, wait, before I call this one solved, did I pass the input/output stuff correctly? Is that how it should look for other functions I haven't written yet too?
I just tested your program. It seems your program is dependent on machines.txt. Without that file within the same directory as your program, your program fails.
To solve this issue, create a file called machines.txt within the same directory as your program. Your program doesn't create this file, so you have to do it manually.