I know this is a really simple question but Ive been searching for the answer to this for about an hour now. Ive done it before...a few years ago....cant find that code but I am required to build the structures in a header file.
//-----------------------------main body
int main()
{
cout << "Welcome to the GradeCaculator Deluxe." << endl;
GetStudentList ();
GetClassHours ();
// 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;
Student MyStudents;
Student MyGrades;
ifstream StudentFile;
string File1;
//int NumberOfLines = 0;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
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;
cout << "\nThe file has been successfully opened for reading.\n";
return File1;
}//end GetStudentList
//--------GetClassHours
string GetClassHours()
{
Grades MyHours [15];
ifstream HoursFile;
string File2;
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
-----.h----
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents;
What do you mean build the structs? Are you saying you need to define your functions within the .h file or in a .cpp that #includes the respective .h file? Because basically,
The #ifndef, #define, and #endif are preprocessor directives that basically tell any .cpp file that you want to include the header file in to include it, UNLESS it's already been included at some point.
For example, you have a.h, b.h, c.h, a.cpp, b.cpp, c.cpp. You have #included a.h into b.h and b.h into c.h. Putting the earlier syntax into each of those .h files, will allow the compiler to see that a.h is already included in b.h, and since I'm putting b.h into c.h, I don't have to put a.h into c.h again.
As for your function definitions, all you do is
1 2 3 4
struct C:
{
// function1() { /* function body */ }
}
//-----------------------------main body
int main()
{
struct students MyStudents;
struct grades MyGrades;
cout << "Welcome to the GradeCaculator Deluxe." << endl;
GetStudentList ();
GetClassHours ();
OpenOutputFile ();
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;
//struct student MyStudents;
//Student MyStudents;
//int NumberOfLines = 0;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
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;
cout << "\nThe file has been successfully opened for reading.\n";
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()
{
ifstream 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 File4;
}//end GetClassHours
//-------GetCreditHours
//int GetCreditHours ()
//{
//}//end CreditHours
-----header andy.h
#include <string>
#ifndef andy.h
#define andy.h
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;