1

Program idea: Console will prompt user to key in their fuel consumption details. After a full set of data is keyed in (date, amount, brand and mileage) and I continue on with the program, logging in as much as I want and finally decide to output the text into notepad, the text document will show me all the entries I've keyed in. The for loop works like it should, looping infinitely until user inputs 'N' or 'n'.

ISSUE: My issue with this is that no matter how many entries I key in and continue, my output text document will only show me the last entry I keyed in. Why is this so?

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

using namespace std;

int main()
{
    int count = 0;
    double amount, mileage;
    string date, brand;
    char ans;

    cout << "****************************" << endl;
    cout << "CAR FUEL CONSUMPTION HISTORY" << endl;
    cout << "****************************" << endl;

    for (;;)
    {
        cout << "\nDate (dd/mm/yy): ";
        cin >> date;

        cout << "Amount Payable: $";
        cin >> amount;

        cout << "Brand: ";
        cin >> brand;

        cout << "Mileage (km): ";
        cin >> mileage;

        cout << "\nContinue? (Y/N): ";
        cin >> ans;

        if (ans == 'N' || ans == 'n')
        {
            break;
        }
        else
        {
            count++;
        }
    }

    ofstream openFile;
    openFile.open("conHist.txt");

    // NOT WORKING!!!! >:(
    while(openFile.good())
    {
        openFile << "Entry " << count << endl;;
        openFile << "Date (dd/mm/yy): " << date << endl;
        openFile << "Amount Payable: $" << amount << endl;
        openFile << "Brand: " << brand << endl;
        openFile << "Mileage: " << mileage << "km" << endl;
        openFile.close();
    }

    return 0;
}
halfer
  • 18,701
  • 13
  • 79
  • 158
ErrFunn
  • 29
  • 3
  • 2
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 01 '17 at 17:12
  • Shouldn't you be writing to the file in the for loop? – NathanOliver Feb 01 '17 at 17:13
  • Sorry! I'm not good at programming I just have a module in school which requires programming :( Mind helping me out with a fix code mate? – ErrFunn Feb 01 '17 at 17:15
  • You're just overwriting all the variables on each iteration of the `for`. Go through the code line by line in your head and it should be clear what it's doing. – Greg Kikola Feb 01 '17 at 17:17
  • Walk through each line of your code, and explain to an imaginary friend exactly what it does and what it means... alternatively, use your debugger to step though it and tell you what each line means. – UKMonkey Feb 01 '17 at 17:18
  • can you guys tell me where I went wrong? is the for loop implementation correct? I'm guessing it's because no matter how much data I input to the console, the new data I input keeps overwriting the old data and that's why it only shows the newest data keyed in only. if this is the issue.. I'm not sure how to solve it as I just barely started programming :/ – ErrFunn Feb 01 '17 at 17:22
  • @πάνταῥεῖ, I have copied that comment in a bookmarklet. Hope it is not copyrighted :) – R Sahu Feb 01 '17 at 17:24
  • @ErrFunn Yes, you're overwriting all your data with new data. You'll have to do something with that data _before_ you overwrite it. That is, you'll need to process your data after you read it within the `for` loop. – Greg Kikola Feb 01 '17 at 17:25
  • @ErrFunn Also check: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – πάντα ῥεῖ Feb 01 '17 at 17:27
  • @RSahu You're welcome, it's not copyrighted of course. Let's hope it helps at least some new users, OP didn't bother to follow the advice so far. – πάντα ῥεῖ Feb 01 '17 at 17:29
  • @Greg Kikola mmm... what do I have to do before that? I'm guessing smth along the lines of array.. not entirely sure – ErrFunn Feb 01 '17 at 17:34
  • @πάντα ῥεῖ hey! Sorry for not looking at the link! :( Really rushing for time here hahaha will look at it soon! Going to bed now – ErrFunn Feb 01 '17 at 17:34
  • You have added in "urgent" begging in three times now, and three times it has been removed. Please do not add that in again - it is not welcome here. I have downvoted in order to remind you of this. Note also that we are not a homework factory - we will give clues, but someone will not be helping you if they do it for you. – halfer Feb 01 '17 at 17:58

1 Answers1

0

You are not storing your data, but overwriting it in the loop:

for (;;)
{
    cout << "\nDate (dd/mm/yy): ";
    cin >> date;

    cout << "Amount Payable: $";
    cin >> amount;

    cout << "Brand: ";
    cin >> brand;

    cout << "Mileage (km): ";
    cin >> mileage;

    cout << "\nContinue? (Y/N): ";
    cin >> ans;

    if (ans == 'N' || ans == 'n')
    {
        break;
    }
    else
    {
        count++;
    }
}

You need to place the data into some type of container like std::vector.

Also, I recommend creating a structure to hold all the data for one entry:

struct Fuel_Record
{
  std::string date;
  double      amount;
  std::string brand;
  double      mileage;
};

You can then create a container to hold all the data you've collected:

std::vector<Fuel_Record> fuel_database;

Your input loop can be modified as:

Fuel_Record f;
for (;;)
{
    cout << "\nDate (dd/mm/yy): ";
    cin >> f.date;

    cout << "Amount Payable: $";
    cin >> f.amount;

    cout << "Brand: ";
    cin >> f.brand;

    cout << "Mileage (km): ";
    cin >> f.mileage;

    // Add the record to the container
    fuel_database.push_back(f);

    cout << "\nContinue? (Y/N): ";
    cin >> ans;

    if (ans == 'N' || ans == 'n')
    {
        break;
    }
    else
    {
        count++;
    }
}

Edit 1: Not storing the data
You may not have learned about containers or structures. In that case, you will need to process the data after you have collected it:

double total_payable = 0.0;
double total_mileage = 0.0;
for (;;)
{
    cout << "\nDate (dd/mm/yy): ";
    cin >> date;

    cout << "Amount Payable: $";
    cin >> amount;

    cout << "Brand: ";
    cin >> brand;

    cout << "Mileage (km): ";
    cin >> mileage;

    cout << "\nContinue? (Y/N): ";
    cin >> ans;

    // Process the collected data:
    total_mileage += mileage;
    total_payable += payable;

    if (ans == 'N' || ans == 'n')
    {
        break;
    }
    else
    {
        count++;
    }
}
Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
  • I added in the structure you recommended and created the container and made the following changes in my for loop. However when I try to build it, it throws me an error saying "fuel_database was not declared in this scope". :/ Thanks for the help tho man really do appreciate it! – ErrFunn Feb 02 '17 at 01:58
  • okay I brought the structure outside of int Main() and it allows me to build it now however, after running the program and opening the text document, it didn't write my date and brand and the amount and mileage come out weird. Here's how the output looks like: Entry 1 Date (dd/mm/yy): Amount Payable: $2.48473e-200 Brand: Mileage: 6.95314e-308km – ErrFunn Feb 02 '17 at 02:24