Hello Orion98,
I have been looking at and thinking about the program for awhile. I find that there are several problems.
Lines 6 - 19. This would work great if they were contained in a header file, but not in the only ".cpp" that makes up the program.
The header guard is to prevent the code from being compiled more than once in a program.
Since the struct is in only 1 file it is compiled only once. The header guard, lines 6,7 and 19, have no real use. You can do away with them.
Lines 23 and 24 are fine if you want to keep them, but the more common way is to use
ifstream inputFile("Students.txt"/*, std::ios::binary*/);
. This will define the file stream variable and open the file at the same time. Testing both ways with MSVS2017 opening the file in "binary" mode did not seem to make any difference when using the ".read()" function. Although I have always seen the ".read()" function used with a file opened in "binary" mode.
The if/else statement is backwards and wrong. The only way to say that the file opened is if it did not open. Otherwise you will be saying that the file did not open when it actually did.
Also the "return" and "exit()" will not let you past line 35. "exit()" is a C function that you should avoid using in a C++ program. "exit90" is an immediate termination of the program and it does not know about C++ classes or how to clean them up before leaving the program.
The code would work better as:
1 2 3 4 5 6 7 8 9 10
|
if (!inputFile)
{
cout << "\n Failed to open file!";
return 1;
}
else
{
cout << "\n File successfully opened!";
}
|
The if condition will catch more problems with the file stream than the ".is_open()" function. The "return 1" not only follows the instructions, but will clean up any classes that were created, like the string class.
You will need to correct this before you can read the file and do anything.
The else part is not needed. You could easily say:
1 2 3 4 5 6 7 8
|
if (!inputFile)
{
cout << "\n Failed to open file!";
return 1;
}
cout << "\n File successfully opened!";
|
If you wanted to say that the file did open, but even this is not necessary for the program, but useful in testing.
After I fixed this I added the code:
1 2 3 4
|
Student* students[SIZE]{ nullptr };
char record[RECSIZE]{};
inputFile.read(record, RECSIZE);
|
"RECSIZE" is like "SIZE", Defined as a constant variable.
constexpr int SIZE{ 100 }, RECSIZE{ 52 };
I have only worked to this point of just reading the file for 1 record. From you will need to break up the variable "record" to store it in the struct. Remember this struct is part of the array and not a separate object.
Something else to keep in mind. The input file has age as 3 characters, but the struct defines as a numerical variable. Numeric variables do not store leading zeros, so when it comes time for output to the screen, or file, you will need to code for the leading zero before you output the short.
Another point:
short int age;
is fine, but you only need
short age;
. "short", "long" and "long long" are all considered to be of type "int".
That should be enough to read 1 record. From there you need to decide how you can break up the variable "record" and put the information into the struct in the array. This is based on what you have learned so far, which no one has any idea what that is.
When you do get to the point of using "new" to create the dynamic memory needed
DO NOT forget to use
"delete" to free the memory created or you will have a memory leak.
Andy