0

I am just getting started with C++, so please explain things to me in the most basic way you can. I really don't even know where to start to be honest. Below is a problem that I would like to learn how to code. Examples are welcome. Thank you.

Suppose you want to keep your bank transactions on your computer, and you'd rather keep the information as a simple text file that you can manipulate using C++ programs, rather than being slave to Excel or Quicken. You decide to use the following format for your bank transaction file:
The first line will be the starting balance, and the last line will be -1.

All other lines will contain transactions.

There are three types of transactions: Deposits, checks and ATM withdrawals.

Deposit lines are of the form:
Year Month Day Amount

The year, month and day are all integers, and the amount is a positive double.

Check lines are of the form:
Year Month Day Amount Number

The year, month and day are all integers. The amount is a negative double and number is the check number, which is a positive integer.

ATM withdrawal lines are of the form:
Year Month Day Amount 0

The year, month and day are all integers, and the amount is a negative double.

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by Nick Evan
0

I really don't even know where to start to be honest.

So you have a file with x lines in them, and you know that the last line will be a '-1', you could use the getline() command to get one line at a time.

For example:

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

using namespace std;

int main()
{
    string line;
    ifstream infile("yourfile.txt");
    if (!infile.is_open())
    {
        cout << "failed to open!";
        return 0;
    }
    while (getline(infile,line))
    {
        cout << line;
        // do stuff with your line here
    }
    return 0;
}

Now you need to parse each line to filter out the date/data. Stringstreams might come in handy at this point.

Try experimenting a bit with this and come back if you have any questions

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.