function to write to an opened text file

I am starting to get the function thing down....maybe...but I'm having problems making a function to write text out to a log file. please see below..."WriteTextToLogFile"

-----code-----

//
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "andy.h"

//using namespace std;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours ();
string OpenOutputFile ();
//string OpenLogFile();
//void GetLogFileName(string&)
//int OpenLogFile(ofstream&);


//-----------------------------variables
//Student MyStudents;
//Student MyGrades;
ofstream OutLogName;
string LogFileName;
string TextToWrite;


//-----------------------------main body
int main()
{
ofstream OutLogName;
string LogFileName;
// struct students MyStudents;
// struct grades MyGrades;

cout << "Welcome to the GradeCaculator Deluxe." << endl;
GetStudentList ();
GetClassHours ();
OpenOutputFile ();
OpenLogFile(OutLogName);
//OpenLogFile();
// getline(File1) ;
//return 0;
}//end main
//--------GetStudentList
string GetStudentList()

{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
string ClassName;
char Grade;
int Index = 0;
int LineIndex = 0;
ifstream StudentFile;
string File1;
ofstream LogFile;
string LogFileName;
//struct student MyStudents;
student MyStudents;
grades MyGrades;
string TextToWrite;
//int NumberOfLines = 0;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
LogFile << "Filename successfully entered\n";
getline(cin, File1);
StudentFile.open(File1.c_str());
if(!StudentFile.is_open())
{
exit(EXIT_FAILURE);
}
// while (getline (inputFile, File1))
//{
//++LineIndex;
//}
//cout << "Number of Lines in text file is: " << LineIndex << endl;
while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
{
MyStudents.FirstName = FirstName;
MyStudents.LastName = LastName;
MyStudents.StudentId = StudentId;
MyStudents.NumberOfGrades = NumberOfGrades;
for (int Index = 0; Index < NumberOfGrades; ++Index)
{
(StudentFile >> ClassName >> Grade);
MyGrades.ClassName = ClassName;
MyGrades.Grade = Grade;
}
}
cout << MyStudents.FirstName << ", " << MyStudents.LastName << ", " << MyStudents.StudentId << ", " << MyStudents.NumberOfGrades << endl;
cout << MyGrades.ClassName << ", " << MyGrades.Grade << endl;

return File1;
}//end GetStudentList
//--------GetClassHours
string GetClassHours()
{
//int Grades MyHours [15];
ifstream HoursFile;
string File2;
//struct grades MyGrades;
cout <<"Enter the file name for the list of Class Hours, including the extension::" << endl;
getline(cin, File2);
HoursFile.open(File2.c_str());
if(!HoursFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
return File2;
}//end GetClassHours
//-------OpenOutputFile
string OpenOutputFile()
{
ifstream OutputFile;
string File3;
cout <<"Enter the file name to write the DATA to, including the extension::" << endl;
getline(cin, File3);
OutputFile.open(File3.c_str());
if(!OutputFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
return File3;
}//end GetClassHours
//-------OpenLogFile
//string OpenLogFile()
//{
// ofstream LogFile;
// string File4;
// cout <<"Enter the file name to write the Log File data to, including the extension::" << endl;
// getline(cin, File4);
// LogFile.open(File4.c_str());
// if(!LogFile.is_open())
// {
// exit(EXIT_FAILURE);
// }
// cout << "\nThe file has been successfully opened for reading.\n";
// return 0;
//}//end GetClassHours
//-------GetLogFileName
void GetLogFileName (string& LogFileName)
{
cout << "Enter the file name to write the Log File data to, including the extension::";
cin >> LogFileName;
TextToWrite = "\nThe file has been successfully opened for reading.\n";
WriteLogFileData (TextToWrite);
cout << TextToWrite << endl;
}//end GetLogFileName
//-------OpenLogFile
int OpenLogFile(ofstream& OutLogName)
{
string LogFileName;
GetLogFileName(LogFileName);
OutLogName.open(LogFileName.c_str());
if (!OutLogName)
{
cout << "File FAILED to open....Program Terminated" << endl;
return 1;
}//end if
return 0;
}//end OpenLogFile

void WriteLogFileData (string& TextToWrite)
{
//string TextToWrite;
OutLogName << TextToWrite << endl;
}//end WriteLogFileData


First, please add the code tags to your post (and systematic&informative indentation). See http://www.cplusplus.com/articles/jEywvCM9/

You have global variables. Change your program to have only local variables.
OpenLogFile() calls GetLogFileName() which calls WriteLogFileData() to write some data, but all of this happens before OpenLogFile actually opens the log file, so the write fails. Also, the call to OpenLogFile in main() is at the very end of main().
Topic archived. No new replies allowed.