fstream: How to take a float from ifstream and do math problems with it

I need to take a number from an ifstream inputFile, add or subtract (depending on what the user wants), do that operation with the number and then write it back into the file. It should end up looking like this in the file:

11/01/2017
Initial Deposit
1000
1000

11/14/2017
Titan Bookstore Online
-245.67
754.33

It has to work with more than 2 transactions. I am able to write the initial deposit and gather the date, transaction name, and amount but not the new total. My code so far is:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//Checking if account exists
    cout << "Enter your 3-digit account number: ";
    cin >> accountNumber;
    
    
    stringstream ss;
    ss << accountNumber << ".dat";
    string filename = ss.str();
    
    fstream inB;
    inB.open(filename.c_str());
    
    
    if (inB.fail())
    {
        cout << endl << "ERROR: That account does not exist." << endl;
    }
    
    
    else
    {
        inB.close();
        
        cout << "Enter the date (mm/dd/yyyy): ";
        cin.ignore();
        getline(cin, date);
        
        cout << "Enter transaction title: ";
        cin.ignore();
        getline(cin, description);
        
        cout << "Enter transaction ammount : $";
        cin >> ammount;
        
        fstream outB;
        outB.open(filename.c_str(), fstream::app);
        
        outB << endl << date << endl << description << endl << ammount << endl;
        
        outB.close();
        
        stringstream ss;
        ss << accountNumber << ".dat";
        string filename = ss.str();
        string line;
        int balance, newBalance = 0;
        
        ifstream inB;
        inB.open(filename.c_str());
        
        if (inB.is_open())
        {

            inB >> balance;
            newBalance += balance;
            
            fstream outB2;
            outB2.open(filename.c_str(), fstream::app);
            
            outB2 << newBalance << endl;
            
            outB2.close();
            
            cout << endl << endl;

            while ( getline(inB, line))
            {
                cout << line << endl;
            }
        }
        
        inB.close();


Last edited on
Show me the code you're trying to used and someone may understand what you're doing wrong.
I updated the post
That's a good start, but you'll want to format that as code to make it more legible.
(The shortcut for code is to highlight the text and then click the <> button to the right.)
Just as my initial thoughts based on your project description, what you want to do is store your initial deposit into a float in a manner such as B >> number;, do your math, then output it as the final amount like B << number;. Upon looking at your code it seems that you're underutilizing the fstream, or using fstream erroneously when you want to be using ifstream for inB and ofstream for outB.

UPTADE 1: I've been testing your code myself and the first big issue I encountered was that your stringstream ss never got initialized. The first thing you should do with objects in C/C++ is either call a constructor on them or assign them something. ss << accountNumber << ".dat"; may include an assignment, but the compiler doesn't know that. Scratch what I said about stringstream. I'm learning too.
Last edited on
Registered users can post here. Sign in or register to post.
<rt id="pwjBlLZ"><small id="pwjBlLZ"></small></rt>
<acronym id="pwjBlLZ"></acronym><rt id="pwjBlLZ"></rt>
<tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr><tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr><acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym>
<acronym id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></acronym>
<option id="pwjBlLZ"></option>
<tr id="pwjBlLZ"><optgroup id="pwjBlLZ"></optgroup></tr>
<acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym><acronym id="pwjBlLZ"></acronym>
<acronym id="pwjBlLZ"><small id="pwjBlLZ"></small></acronym>
  • 6512882421 2018-04-19
  • 4659652420 2018-04-19
  • 2967832419 2018-04-19
  • 8339042418 2018-04-19
  • 8147112417 2018-04-19
  • 2774752416 2018-04-19
  • 4316132415 2018-04-19
  • 6265742414 2018-04-19
  • 1875142413 2018-04-19
  • 4146552412 2018-04-19
  • 8205662411 2018-04-19
  • 959982410 2018-04-19
  • 7153742409 2018-04-19
  • 9349932408 2018-04-18
  • 6024052407 2018-04-18
  • 2113432406 2018-04-18
  • 7629172405 2018-04-18
  • 163882404 2018-04-18
  • 3515922403 2018-04-18
  • 5047802402 2018-04-18