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

    0
    ReputationRep:
    I'm working on this question and got a bit stuck while coding.

    A file carmiles has data about the fuel spending for a car, the weekly dates are in ascending order.

    date (ddmmyyyy)
    fuel price per liter (decimal currency)
    money spent (decimal currency)
    miles driven (real value)

    Write a program that inputs the start date and end date. The program then reads the file, locates the first date greater than the start date, adds up the total price per liter and miles driven. The program then counts how many records are found before the end date is reached. Calculate the mean price per liter over this period.

    I started by using structs and also tried this using classes. Could someone please check if these are correct and help me input some data so that it gets saved to the file carmilesdata.txt (I want to create a txt file). I know about using fstream but am a bit confused on how to save data here.

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    
    struct car
    {
        int date;
        int fuelPrice;
        int spent;
        float miles;
    };
    
    
    int main()
    {
    
    
        return 0;
    }
    Code:
    #include <iostream>
    #include <fstream>
    
    
    class cars
    {
    public:
        int date;
        int fuelPrice;
        int spent;
        float miles;
    };
    
    
    using namespace std;
    Offline

    0
    ReputationRep:
    So the user of this program enters a start and end date, then the program finds all the files between those dates and then adds up what, exactly? Also, is there a specific structure to these files given in the question, or can you make one up? If the latter, then I would recommend making the filename the same as the date of the file...

    Quick thing for fstream:

    Code:
    ofstream myfile; //Creates the 'myfile' variable that will store file information.
    myfile.open ("example.txt"); //Opens the file, and assigns it to the 'myfile' variable.
    myfile << "Writing this to a file.\n"; //Overwrites file content with this.
    myfile.close(); //Closes file.
    That will WRITE TO a file. To read from a file you use "ifstream" instead of "ofstream", and you can use something like cout << myfile.rdbuf << endl;
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Qwertish)
    So the user of this program enters a start and end date, then the program finds all the files between those dates and then adds up what, exactly? Also, is there a specific structure to these files given in the question, or can you make one up? If the latter, then I would recommend making the filename the same as the date of the file...

    That will WRITE TO a file. To read from a file you use "ifstream" instead of "ofstream", and you can use something like cout << myfile.rdbuf << endl;
    It adds up the total of the prices and the miles driven. No, there's no structure. I'll tell you where I'm stuck. exactly. That way it'll be easier

    The main question I have is once I make the file "carmiles" and enter some data, do I use ofstream to open it in another file? If so, how do I get my program to add up the totals? Do I have to declare new variables in the second file? How exactly do I perform addition, etc. on a file that is opened in another file?

    Thanks for the code de and the help. Could you please help me with this?

    I know it looks like a lot but I am confused with this a bit. I'd be grateful if you could help please
    Offline

    0
    ReputationRep:
    (Original post by Diiiii)
    It adds up the total of the prices and the miles driven. No, there's no structure. I'll tell you where I'm stuck. exactly. That way it'll be easier

    The main question I have is once I make the file "carmiles" and enter some data, do I use ofstream to open it in another file? If so, how do I get my program to add up the totals? Do I have to declare new variables in the second file? How exactly do I perform addition, etc. on a file that is opened in another file?

    Thanks for the code de and the help. Could you please help me with this?

    I know it looks like a lot but I am confused with this a bit. I'd be grateful if you could help please
    So will you be manually making the file "carmiles" in Notepad then? If so, then you would use ofstream to open it within your program and extract data from it.

    It would be really helpful to know the exact structure of the file. What exactly is in it? My suggestion would be to have the name of the file as the date (e.g. "01-03-2013"; you don't really need the .txt extension), and then have each item on a new line in the file (don't have any labels in the file, you don't need them).

    Is this a textbook question or something? Could you scan the whole thing in or take a photo of it?
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Qwertish)
    So will you be manually making the file "carmiles" in Notepad then? If so, then you would use ofstream to open it within your program and extract data from it.

    It would be really helpful to know the exact structure of the file. What exactly is in it? My suggestion would be to have the name of the file as the date (e.g. "01-03-2013"; you don't really need the .txt extension), and then have each item on a new line in the file (don't have any labels in the file, you don't need them).

    Is this a textbook question or something? Could you scan the whole thing in or take a photo of it?
    It's a question in a paper. I'll attach a pic.

    Let's say it's in this format

    Date Fuel price/liter Money spent Miles
    Ex. 03022013 100 175 15
    Attached Images
      
    Offline

    0
    ReputationRep:
    (Original post by Diiiii)
    It's a question in a paper. I'll attach a pic.

    Let's say it's in this format

    Date Fuel price/liter Money spent Miles
    Ex. 03022013 100 175 15
    Ohhhh, right. So the file looks like this:

    Code:
    01012013
    1.39
    200
    1000
    08012013
    1.40
    120
    800
    etc...

    Right. What I would do, is use ofstream to output the file into a string variable ("#include <string>", then "string container();").

    You can then use string operators to separate it out into separate bits. Put the numbers into separate arrays (e.g., date[], fuel[], spent[], miles[]), and then you can work with them more easily.
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by Qwertish)
    ................................
    Thanks I managed to get the code done for the first part and I've posted it on a separate thread.
 
 
 
  • 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
    Have you ever participated in a Secret Santa?
    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.