Reading lines from a file!

Hi all, I need help on how to read lines in a .dat file!
I have the following in the file:

46780946 3750.40
W 250.00
D 1200.00
W 75.00
W 375.00
D 1200.00
I 5.50
W 400.00
W 600.00
D 450.50
W 35.65

The first line is the account number followed by the account balance. In the following lines W (withdrawal) and the amount, etc... (D - deposit and I - interest).
How do I read line 1 first but with the account number and the account balance separately? And line 2 the W separately from the amount until the last line??

My output should look like this:

Account number: 46780946

Opening balance: R3750.40

Transaction     Amount      Balance
Withdrawal       250.00      4000.00
Deposit         1200.00Ct    2800.00
.
.
. etc

Closing balance:   R969.60


Please help me!
You can use fgets to read line http://www.cplusplus.com/reference/cstdio/fgets/

Once you have the line stored in the character array, you can use sscanf
http://www.cplusplus.com/reference/cstdio/sscanf/ to pull out individual elements from the line
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
#include <iostream>
#include <fstream>

using namespace std;

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 << "Acoount Number: " << account << endl;
    cout << "Balance: " << balance << endl;

    while (fin >> action >> amount) {
        cout << "action: " << action << "  amount: " << amount << endl;
    }

    return 0;
}
Thanks a lot!!
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;
}
@Rascal - you may find it useful to look at this related thread:
http://www.cplusplus.com/forum/beginner/93212/#msg500853

Meanwhile, I tried your code above and the program failed at line 24
 
    in_stream >> accountNumber >> openingBalance >> transaction >> transactionAmount;


I think the reason is an uninitialised pointer at line 22:
char* transaction;

Actually, since the transaction is just a single character rather than a string, you don't need a pointer here, a simple char would do.

Around line 31 onwards should be contained in some sort of loop, in order to read and process each transaction. Again, I refer you to the other thread for an example.

Edit:
Your if statement has multiple issues.
a) comparing two c-strings would need the strcmp() function.
b) the "=" symbol is the assignment operator, it sets the variable on the left equal to the value of the expression on the right. When you use it in an if () condition, it will always be true, hence the if is executed. The test for equaity is done using the "==" operator.
c) the else is executed because there are missing braces { } around that part.
Last edited on
Topic archived. No new replies allowed.