Hello dbarclay100,
I think the first thing you need to understand is how the extraction operator, >>, works. First what you type on the keyboard if put into the input buffer before it is put into a variable. The line of code
fin >> telephone;
will extract from the input buffer up to and including the first white space or new line whichever comes first and discards either the white space or new line leaving whatever is left in the input buffer for the next read.
So what is happening is that
fin >> telephone;
is only extracting "(555)" into "telephone" because of the white space and leaving the rest for
fin >> social;
to extract until it finds a white space.
By the time you get to
fin >> course;
"- 1234" is left in the input buffer and since "course" is a string it will accept the "-" leaving the last four digits of the phone number for the first test score to extract.
"telephone" and "social" need to be read with a "getline" and
fin >> years;
needs to be followed with
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
To make sure the input buffer is empty. Your use of
fin.ignore();
uses the default of one character and the new line character which may not be enough to clear the buffer because it will stop after one character and never look for the new line.
Even if you do not understand it right now do understand that it is the best way to clear the input buffer. The part
std::numeric_limits<std::streamsize>::max()
is just getting a large number, the maximum size of the input buffer, for any given operating system and the compiler that creates the program as different computers do have a different size for this number based on what is in the header file used for a given computer's operating system.
To start with I would work in the uncommented part until you get that working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
for (int i = 0; i < n; ++i)
{
getline(fin, name);
fin >> age;
fin.ignore();
getline(fin, address);
fin >> years;
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
fin >> telephone; // <--- change to "getline".
fin >> social; // <--- change to "getline".
//for (int j = 0; j < NUMBER_OF_COURSES; ++j)
//{
// fin >> course;
// for (int k = 0; k < NUMBER_OF_SCORES; ++k)
// {
// }
//}
std::cout << std::endl; //<--- Used as a break point in the IDE for testing.
}
|
When you get to the last two for loops you have a new problem. Reading "courses" in the outer for loop will leave "courses" with the last course name read because each time through the outer for loop you will over write "courses" with the next read.
On the inner for loop if you want to use five dfferent variables to hold the test scores you can not use a for loop. If you use a 2D array to hold the test scores it will work. Also you should consider using a 1D array to hold the course names.
While you get the first part working think about how to deal with the course names and grades and give a little thought what to do with the information once it is read and before you go to the next record.
Hope that helps,
Andy