Hello nicholasjb1996,
Yes and No.
For an input file a while loop would be the best choice and is generally written as:
while (std::getline(inFile, line))
. This way when it tries to read past end of file it sets "eof" causing the while condition to become false and the while loop fails.
For the output stream the while condition would have to be different. Right now you are basing the condition on output stream being good and it will always be good unless you close the stream or something happens with line 31 to change the state bits.
Some other problems I see:
When you open a file for input you need to follow this with a check to make sure the file is open.
1 2 3 4 5 6 7 8 9 10
|
const std::string inFileName{ "" };
std::ifstream inFile("inFileName");
if (!inFile)
{
std::cout << "\n File " << inFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". Also optional.
return 1; //exit(1); // If not in "main".
}
|
The if statement is the most important part and as the comment says line 8 is optional. I need it for the way my IDE is setup. line 9 the return is for when this is used in "main" and the "exit" is used if you put this in another file.
Then there is the point that you are writing to your output file before you read anything from your input file. Line 31 could work better if it followed line 37 for what you are doing.
You have a nice class, but you are not using it to it full potential. When you read the input file and store the name in the class variable "name" it would be a good idea to store the class in a vector of type "Data" for later use. This way you could use a while loop or a for loop to write to the output file.
Almost forgot when opening a file for output the check to see if the stream is open in not always necessary, but a good idea. When opening a file stream for output is the file does not exist it will create it. So the only thing to cause a problem is if the file name is wrong.
I will say that this is an interesting problem to figure out. I have to see what I can come up with and let you know.
Hope that helps,
Andy