Reading from a file is just like reading from the keyboard. Let's say you want to read the first two records, you would do it from cin like this (if you were me):
std::string month;
// Read the month
if ( getline ( std::cin, month ) ) {
std::cout<< month <<"\n\n"
<< report_header <<'\n'
<< report_separator <<'\n';
std::string product;
std::string details;
// Assume paired lines denote a record
while ( getline ( std::cin, product ) ) {
if ( !getline ( std::cin, details ) )
break;
std::cout<< format_record ( product, details ) <<'\n';
}
} To read from a file, just use the file stream object instead of std::cin. It's just that simple.
format_record would create a detail line for your report. Really all you have to do in it is split the details string into words and do your calculations on the first and third word. You can ignore the last word because it's redundant:
std::string format_record (
std::string product, std::string details )
{
std::stringstream splitter ( details );
std::string unit;
int quantity;
int sold;
if ( !( splitter>> quantity >> unit >> sold ) )
return "";
// Do your calculations here
// Use an output stringstream to format the results
return formatter.str();
} That should be more than enough to get you started. Keep in mind that you don't have to use the stringstream if your instructor doesn't allow it. You can read formatted input directly from the file instead of using getline. That's an alternative that I'll leave to you.