I need to prompt the user for a txt file and display what the text in it. Our example is

Fishing line
132 spools  24 spools
Fish hooks
97 packages 45 packages
123 packages    37 packages
Fish nets
12 ea.      5 ea.
Tackle box
75 ea.      15 ea.
Fishing poles
135 ea.     18 ea.

The first line of the data file contains the month for which the report is being generated.
Then follows the description of a store item on the next line. Then follows how much inventory was present at the start of the month along with the quantity description.
Then follows how many of these items where sold during the month along with the quantity description. Then make it display like this
MONTH: January

ITEM                   BEGIN QTY         UNITS SOLD         ENDING QTY         %SOLD
--------------         ---------         ----------         ----------         -----
Fishing line                 132 spools          24                108                        18.18
Fish hooks                    97 packages        45                 52                      46.39
Sinkers                      123 packages        37                 86                      30.08
Fish nets                     12 ea.              5                  7                             41.67

I can do the calculations and everything but im so confused on how to read the info from a file this is what i have so far and im totally confused on how to read each part from the file and then assign it to a variable.
This is what i got so far:

#include <fstream>  
#include <iostream> 
#include <string>   
#include <cstdlib> 

#define cls system("cls")

using namespace std;

string getInputFileName();

int main ()
    ifstream inFile;
    string fileName;

     fileName = getInputFileName();


    if (!inFile)
        cerr << "Unable to open file " << fileName << endl;

    return 0;
string getInputFileName()
   string fName; 

   cout << "Please enter the fully qualified name of the " << endl
        << "input text file (i.e. including the path): ";
   cin >> fName; 
   cout << endl;

   return fName;

after i know how to read in the file i can do the rest, i just need some help or a hint on how to do that! Thank you.

Edited by Dani: Formatting fixed

10 Years
Discussion Span
Last Post by Narue

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 ) )

    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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.