Header File

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.

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

using namespace std;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours ();
//-----------------------------create structures
//struct student
//{
// string FirstName;
// string LastName;
// int StudentId;
// int NumberOfGrades;
// }MyStudents;

// struct Grades
// {
// string ClassName;
// char Grade ;
// }MyGrades;
//-----------------------------variables



//-----------------------------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;

struct Grades
{
string ClassName;
char Grade ;
}MyGrades;
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,

1
2
3
4
5
6
7
8
9
10
11
12
// .h
#include<anyLibYouNeed>
#ifndef (name)
#define (name)

struct C:
{
    // <typename> function1();
    // <typename> function2();
}

#endif 


1
2
3
4
5
6
7
8
9
// .cpp
#include ".h"

C::function1()
{
    // body
}

// etc. 
Yes define structures in header. Maybe even the functions, not sure about that.

need more info on:
#ifndef (name)
#define (name)
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 */ }
}

As you can see, ive tried every combination of placement, etc,,that I can think of but it will not compile

-----code
#include<string>
#include <iostream>
#include <fstream>
#include <cmath>
#include<cstdlib>
#include "Andy.h"

using namespace std;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours ();
string OpenOutputFile ();
string OpenLogFile();

//-----------------------------create structures
//struct student
//{
// string FirstName;
// string LastName;
// int StudentId;
// int NumberOfGrades;
// }MyStudents;

// struct Grades
// {
// string ClassName;
// char Grade ;
// }MyGrades;
//-----------------------------variables
//Student MyStudents;
//Student MyGrades;


//-----------------------------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;

}MyStudents;

struct grades
{
string ClassName;
char Grade ;

}MyGrades;

#endif
What are the errors you're getting specifically?
well, after closing my eyes, I realized that there were spelling errors. all is good with this part.....
thanks for the help!
Topic archived. No new replies allowed.