Hello ETlinny,
I do have the program working at least to the read part. As a beginner whit I did is likely ahead of what you know, but because of what you want to do you may have to learn earlier.
What I did was to create a function, "ReadFile()", to read the file. To do this I created a vector of type Profile in main and passed this vector by reference to the function.
As a side note when it comes to functions, classes and structs I like to capitalize the first letter of the name. This helps me to remember that it is not a variable name.
Inside the function I defined the variables needed to use when reading the file. The last is a vector to deal with line four of the input file. I created two other "std:string"s to use later.
After opening the file stream I check to make sure it did open properly. See code from last message.
I used a while loop to read the file:
while (std::getline(myfile, Name))
. This will read the file until there is nothing left. When the "myfile" stream fails so also will the while loop.
Inside the while loop I read the other lines of the file. I used this to read the "Age":
1 2
|
myfile >> Age;
myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
|
Line 2 will clear the new line (\n) from the input buffer which is needed before the next "std::getline" makes a read. Note: "myfile" can be replaced with "std::cin" when working with the key board.
I used "std::getline" to read the name and place because these fields could have two or more words and since these variables are "std::strings" this is the best way. When reading "Age", defined as an int, I used the formatted input. Hense the above code.
After reading the last line of the record into a "std::string" I called "line" I put this into a string stream where I used a while loop to extract each part of the string stream, generally referred to and used as "ss" although you could use any name, this makes reading the stream as being just a small portion of the entire file. This when you have read everything from the "ss" it is like reaching the "eof". I did this in order to fill a vector before creating object of the class to put in the vector of classes.
The last thing to do is put the information into the vector that was created in main and passed to the function.
I know that you may not understand some or most of this, so create what code you can so I can see what you can do and we can work it out from there.
Hope that helps,
Andy