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.

Dani AI

Generated

Good start, — and ’s suggestion to read line-by-line and tokenise is the right direction. Below is a compact, practical pattern that fills the gaps: parse each text line into a typed Transaction, store money as integer cents to avoid floating-point rounding, detect the -1 sentinel safely, and (optionally) sort transactions by date before computing a running balance.

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>

struct Transaction {
    int y,m,d;
    long long cents;   // amount in cents (signed)
    int checkNumber;   // -1 == none
    bool operator<(const Transaction& o) const {
        if (y!=o.y) return y<o.y;
        if (m!=o.m) return m<o.m;
        return d<o.d;
    }
};

long long toCents(double amt) { return static_cast<long long>(std::round(amt * 100.0)); }

// read starting balance (first line), then parse remaining lines into Transaction objects
// lines with first token == -1 stop parsing

Key implementation notes and troubleshooting tips:

  • Converting amounts to integer cents (long long) avoids surprises like 0.1 + 0.2 rounding errors and makes equality/comparisons reliable.
  • Read each line with std::getline and parse with std::istringstream so you can detect whether an extra token (check number or 0 for ATM) exists without reading into the next line.
  • Check for malformed lines: if you fail to parse expected tokens, emit a warning and skip the line rather than letting the program silently produce wrong balances.
  • Sorting by date is optional. If transactions in the file are already chronological, skip the sort to preserve original order.
  • For production use add stricter date validation, better error messages, and avoid std::stod exceptions by using std::istringstream conversions with checks.

This approach keeps the core simple to understand while being robust enough for real bank-ledger files.

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. might come in handy at this point.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.