Still struggling - lines in a file

Hi! I still don't know how to get/read single characters and an amount separately.
This is what I have in a file:

46780976 3750.40
W 250.00
D 1200.00
W 75.00
W 975.00
D 1200.00
I 5.50
W 400.00
W 600.00
D 450.50
W 35.65

In the first line: account number followed by the account balance. In the next lines: W (for withdrawal) and the amount... and D (for deposit) and I (for interest)...

HOW do I get and the account number and the balance separately?? And the same goes for the W's, D's and I's separately from the amounts?? If I get the W (for instance) I need to say that it's equal to Withdrawal...

The output should look like this at the end:

Account number: 46780976

Opening balance: R3750.40

Transaction   Amount    Balance
Withdrawal    250.00    4000.00
Deposit       1200.00Ct 2800.00
Withdrawal    75.00     2725.00
Withdrawal    1375.00   1350.00
Deposit       1200.00Ct 1550.00
Interest      5.50      1555.50
Withdrawal    400.00    1155.50
Withdrawal    600.00     555.50
Deposit       450.00Ct  1005.50
Withdrawal     35.65    969.85
Banking costs  25.00    969.60

Closing balance: R969.60


PLEASE HELP ME!!
read line after line from the file and drop the characters you dont need each time

http://www.cplusplus.com/doc/tutorial/files/
http://www.cplusplus.com/reference/istream/istream/ignore/
I'm not sure that I understand the question. What didn't you get in the previous thread?
http://www.cplusplus.com/forum/beginner/92631/

There I provided a skeleton code which required just a few changes to make it do everything you need.

HOW do I get and the account number and the balance separately??
Already done in the previous code.

And the same goes for the W's, D's and I's separately from the amounts??
Already done in the previous code.

If I get the W (for instance) I need to say that it's equal to Withdrawal...
Use a switch-case structure or a series of if-else-if statements

http://www.cplusplus.com/doc/tutorial/control/#switch
http://www.cplusplus.com/doc/tutorial/control/#if
Last edited on
Yeah I see what you mean! Sorry! I didn't think to change variables so it matches mine (stupid me)... Thanks a lot! I really appreciate it!
Don't worry, I'm still looking at this question, if you need more help, feel free to ask.

Meanwhile, I do have a question of my own. In the output above, the decimal points don't line up vertically. Normally I would expect them to be aligned like this:
Account number: 46780976

Opening balance: R3750.40

Transaction     Amount      Balance
Withdrawal      250.00      4000.00
Deposit        1200.00Ct    2800.00
Withdrawal       75.00      2725.00
Withdrawal     1375.00      1350.00
Deposit        1200.00Ct    1550.00
Interest          5.50      1555.50
Withdrawal      400.00      1155.50
Withdrawal      600.00       555.50
Deposit         450.00Ct    1005.50
Withdrawal       35.65       969.85
Banking costs    25.00       969.60

Closing balance: R969.60

I just wondered whether this is how it is supposed to look?
Here's a suggested way to progress from the earlier version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int main()
{
    ifstream fin("data.txt");

    if (!fin)
    {
        cout << "could not open file" << endl;
        return 1;
    }

    int account;
    double balance;
    char action;
    double amount;

    fin >> account >> balance;
    cout << "Account number: " << account;
    cout << "\n\nOpening balance: R" << balance << "\n" << endl;

    cout << "Transaction     Amount     Balance" << endl;

    while (fin >> action >> amount) {
        switch (action)
        {
            case 'W':
                transaction(balance, "Withdrawal", amount, true);
                break;

            case 'D':
                transaction(balance, "Deposit", amount, false);
                break;

            case 'I':
                transaction(balance, "Interest", amount, false);
                break;

            default:
                transaction(balance, "** UNKNOWN **", amount, true);
        }
    }
    transaction(balance, "Banking Costs", 25.00, true);

    cout << "\nClosing balance: R" << balance << endl;

    return 0;
}


The way in which I handled this was to use a separate function which would either add or subtract the amount from the balance as required, and print out a line with the details. Function declaration is this:
void transaction(double &balance, string description, double amount, bool negative);
... what code you use in there is up to you.
Last edited on
Yeah, the decimal points are supposed to line up vertically... But I'm not so familiar with C++, it's very new so I don't know the functions for alignment of commas, I only know the "\t"...

Thank you so much for your help! I really needed it!
How to line up the decimal points

There are lots of formatting options provided by the C++ output stream cout. I can't seem to find a good reference page, the information is available (google it) but you may need to look in more than one place.

The #include <iomanip>  header is required.
Example:
1
2
3
4
5
6
7
8
9
    double n = 42.1;

    cout << '['
         << fixed              // use a fixed-point style
         << setprecision(2)    // two decimal places
         << setw(10)           // width of the entire field
         << right              // left or right justified
         << n                  // the value itself
         << ']' << endl;

Output:
[     42.10]


You can also use the setw() and left/right options with strings too.




>Chervil< thanks a lot! Your examples is much better explained! :) I really appreciate it!
Hi,

I am also working on this same question and am attempting to use if, else. Below is what I have built so far but my if,else seems to have a problem as it runs the if part as well as the else. Also need to work out how to repeat this to read the next few deposits or withdrawals.

Any assistance would be appreciated here.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <fstream>
#include <iostream>
#include <cstdlib>
int main()
{
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;
    in_stream.open("bank.dat");
    if (in_stream.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }
    out_stream.open("bankstatement.dat");
    if (out_stream.fail())
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
    int accountNumber, withdrawal, deposit, interest, bankCharge;
    char* transaction;
    float openingBalance, transactionAmount, balance;
    in_stream >> accountNumber >> openingBalance >> transaction >> transactionAmount;
    out_stream << "Account Number:  " << accountNumber << "\n"
               << "\n"
               << "Opening Balance: R" << openingBalance << "\n"
               << "\n"
               << "Transaction\t\tAmount\tBalance\tBank Cost\n";
 //   in_stream >> transaction, transactionAmount;
    {if (transaction = "W")
    {   balance = openingBalance - transactionAmount;
        out_stream << "Withdrawal\t\t"
                   << transactionAmount << "\t\t"
                   << balance << "\n";
    }
    else (transaction = "D");
    balance = openingBalance + transactionAmount;
    out_stream << "Deposit\t\t"
               << transactionAmount << "\t\t"
               << balance << "\n";}

 //   out_stream << endl;
    in_stream.close();
    out_stream.close();
    return 0;
}
Topic archived. No new replies allowed.