Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    I've created a file called "carMiles.txt" and attached the code I've used to do it. I now want to read the data from this file and calculate the total price, for a start.

    Code:
    #include <iostream>
    #include <fstream>
    
    
    class cars
    {
    public:
        int date;
        int fuelPrice;
        int spent;
        float miles;
    };
    
    
    using namespace std;
    
    
    int main()
    {
        int date;
        int fuelPrice;
        int spent;
        float miles;
    
    
        ofstream carM ("carMiles.txt");
    
    
        if (carM.is_open())
        {
            cout << "File is open" << endl;
        }
            else {
                cout << "Try again, the file is not open" << endl;
            }
    
    
        cars day1;      // Declare day1 of type car
        cars day2;      // Declare day2 of type car
    
    
        //day1 specification
        day1.date=8012013;
        carM << day1.date;
        day1.fuelPrice=200;
        carM << "    " << day1.fuelPrice;
        day1.spent=175;
        carM << "    " << day1.spent;
        day1.miles=12.5;
        carM << "    " << day1.miles << endl;
    
    
        //day2 specification
        day2.date=15122013;
        carM << day2.date;
        day2.fuelPrice=200;
        carM << "   " << day2.fuelPrice;
        day2.spent=110;
        carM << "    " << day2.spent;
        day2.miles=9;
        carM << "    " << day2.miles << endl;
    
    
        //Print day info
        cout << "\n" << endl;
        cout << "DATE       PRICE     SPENT     MILES" << endl;
        cout << "\n" << day1.date <<"     " << day1.fuelPrice <<"       " << day1.spent <<"        " << day1.miles << endl;
        cout << day2.date <<"    " << day2.fuelPrice <<"       " << day2.spent <<"         " << day2.miles << endl;
    
    
        carM.close();
    
    
        return 0;
    }
    The next code is where I have problems

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    
    int main()
    {
        ifstream openCar ("carMiles.txt");
        int countC=0, howMany, sDate, eDate;
        float totMiles=0, totPrice=0, avMiles, avCost, pricePMile;
    
    
        cout << "Enter start date: ";
        cin >> sDate;
    
    
        cout << "Enter end date: ";
        cin >> eDate;
    
    
        while ((!openCar.eof()) && date<=sDate)
        {
            getline(openCar, line)
            totPrice = totPrice + price;
        }
        return 0;
    }
    Could someone please help me with this?
    Offline

    14
    ReputationRep:
    So exactly what are you stuck on? How to read the file back in?

    You can use the ifstream like you use cin. So to read an entry from the file you could do something like

    Code:
    int date;
        int fuelPrice;
        int spent;
        float miles;
    
        openCar >> date >> fuelPrice >> spent >> miles;
    I'm not sure that's exactly right, I rarely use the standard library for file input. But it's something along those lines. It can tell the type of variable you're extracting data into so it knows how to convert it from text. I think it also skips whitespace.


    And for the date, I suggest storing a timestamp rather than in DDMMYYYY format (which I assume is what you're doing). I think C++ has some basic date handling and timestamp functions built in. The reason is, in the format you're using, you can't just do a regular integer comparison.

    2nd Jan - 02012013
    1st Feb - 01022013

    Notice that if you treat them as normal integers, 2nd Jan is actually a bigger number than 1st Feb.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Psyk)
    So exactly what are you stuck on? How to read the file back in?

    You can use the ifstream like you use cin. So to read an entry from the file you could do something like

    Code:
    int date;
        int fuelPrice;
        int spent;
        float miles;
    
        openCar >> date >> fuelPrice >> spent >> miles;
    I'm not sure that's exactly right, I rarely use the standard library for file input. But it's something along those lines. It can tell the type of variable you're extracting data into so it knows how to convert it from text. I think it also skips whitespace.


    And for the date, I suggest storing a timestamp rather than in DDMMYYYY format (which I assume is what you're doing). I think C++ has some basic date handling and timestamp functions built in. The reason is, in the format you're using, you can't just do a regular integer comparison.

    2nd Jan - 02012013
    1st Feb - 01022013

    Notice that if you treat them as normal integers, 2nd Jan is actually a bigger number than 1st Feb.
    Thank you so much. I think we did do code to skip whitespace, I'll use that.
    I'll look the timestamp function up too. Thanks for that, I didn't notice that the latter date would be smaller because I treated as an integer
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Are unpaid trial work shifts fair?
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.