Monsieur Dontes
Monsieur Dontes

Reputation: 7

Put data from a text file into a variable

I am trying to pull data from a text file to put it in a variables. This is what the text file looks like:

Cars.txt

1999 Ford Ranger 3000 156000 used
2000 Mazda Miata 4000 98000 used
2015 Jeep Wrangler 33000 250 new

and my output is supposed to look like:

Category| Number| Total Price| TotalMileage|
     New|      1|       33000|          250|
    Used|      2|        7000|       254000|

This function should read the entire file and compile the statistics. Since each of the variables are reference variables, if you make changes here they should affect the variables used to call the function. After reading in the entire file you should be able to print the statistics.

double newTotalPrice = 0;
    double newTotalMileage = 0;
    double usedTotalPrice = 0;
    double usedTotalMileage = 0;
    int numUsed = 0;
    int numNew = 0;
    std::ifstream fin;
    std::string filename = "cars.txt";
    bool isOpen = GetInputFileStream(&fin, filename);
    AnalyzeFile(fin,
                numUsed,
                numNew,
                newTotalPrice,
                newTotalMileage,
                usedTotalPrice,
                usedTotalMileage);
    PrintStatistics(std::cout,
        numUsed,
        numNew,
        newTotalPrice,
        newTotalMileage,
        usedTotalPrice,
        usedTotalMileage);

  std::cout << "Press ENTER to continue";
  std::cin.get();
}


void AnalyzeFile(std::istream & fin,
    int & numUsed,
    int & numNew,
    double & newTotalPrice,
    double & newTotalMileage,
    double & usedTotalPrice,
    double & usedTotalMileage)
{

 numUsed = 0;             
 numNew = 0;              
 newTotalPrice = 0;       
 newTotalMileage = 0;    
 usedTotalPrice = 0;      
 usedTotalMileage = 0;    
    while (!fin.eof())
    {


        fin >> numNew
        fin >> Price;
        fin >> Mileage;


    }

}

So my question is, how do I read data from a text file and put it in variables? Also, how do I get it to collect all the variables such as the used cars total price and add them up? I feel like I am making this more complicated than it needs to be.

Upvotes: 0

Views: 272

Answers (1)

Jason S.
Jason S.

Reputation: 46

There is some trickiness here dealing with fstream. Here would be one simple way to write the AnalyzeFile() function:

void AnalyzeFile(std::istream & fin,
  int & numUsed,
  int & numNew,
  double & newTotalPrice,
  double & newTotalMileage,
  double & usedTotalPrice,
  double & usedTotalMileage)
{

    int modelYear, Price, Mileage;
    string Make, Model, Condition;

    numUsed = 0;
    numNew = 0;
    newTotalPrice = 0;
    newTotalMileage = 0;
    usedTotalPrice = 0;
    usedTotalMileage = 0;
    do
    {
        //read each field in a line
        Condition = "";
        fin>>modelYear>>Make>>Model>>Price>>Mileage>>Condition;

        if (Condition == "") break; //no valid data loaded

        if (Condition == "used")
        {
            numUsed++;
            usedTotalPrice += Price; //after all entries have been added usedTotalPrice will contain the sum of all the used car prices.
            usedTotalMileage += Mileage;

            //do other calculations
        }
        else if (Condition == "new")
        {
            //do updates for new condition
        }

    } while (!fin.eof() && !fin.bad());
}

Note that this code does not contain any error checking and is only an outline to get you started. One tricky thing: After reading the final string fin.eof() will return true, even though the last line was valid. That is why I check to see if the 'Condition' string has received any data from the file.

Upvotes: 1

Related Questions