Hello vaughanthedon,
Please provide the input file that you are using. It is a great help to others so they do not have to guess at what you are using.
On line 6 you you define a file stream and open the file, but how do you know that it is open?
The next line
if (infile.is_open())
will always be true until the program ends and the stream is closed. This may work, but your file stream could be open and still not be usable.
What I see more often is:
1 2 3 4 5 6
|
if (!infile)
{
std::cout<< "\n An error message\n";
return 1; // <--- Because there is no point in continuing with the program until it is fixed.
}
|
The 1 or any number greater than (0) zero means that something is wrong.
Lines 10 - 16 are the correct way of using what you have in the while condition, but
salem c's example is the best way. This will read a file of any size without knowing how many records are in the file.
Inside the while loop you need to take the line that you have read and get just the name, or first column, from the whole line that was read.
When it comes to the input file. Were you given a file to use? Or did you have to make one up and can you change it?
With out seeing the input file I am not sure what is the best approach for this program.
Andy