Line spacing dealing with files

Everytime I run it, It shows up like this...

4/16/16 10:55pm Withdraw 35$ 4/16/16 10:58pm deposit 50$

This is what shows up from my transHistory function, if the user selects to view it. But...

I want it to show up on different lines like this...

4/16/16 10:55pm Withdraw 35$

4/16/16 10:58pm Deposit 50$

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
 void atm :: deposit(void) // Takes bal variable from bank class and keeps it as a constant total, deposit adds to bal
{
   long double deposit;
   string time = Date_Time();
   cout << "Enter how much you would like to deposit: ";
   cin >> deposit;
   bal = deposit+bal;
   cout << "You have deposited: $" << setprecision(100) << deposit << endl;
   if (deposit >= 100000)
    cout << "Now that is a lot of money!" << endl; // Easter egg
    ofstream file;
    file.open("transHistory.txt", std::ios_base::app);
    file << time <<  "Deposit $" << setprecision(10) <<  deposit;
    /*
    use ofstream to save data into transHistory.txt using append:
    ofstream file;
    file.open("transHistory.txt", ios_base::app);
    */

}

void atm :: withdraw(void) // Similar to deposit, but subtracts from bal.
{
    long double withdraw;
    string time = Date_Time();
    cout << "Enter how much you would like to withdraw: ";
    cin >> withdraw;
    if (bal > withdraw || bal == withdraw){
    bal= bal - withdraw;
    cout << "You have withdrawn: $" << setprecision(10) << withdraw << endl;}
    else if (bal < withdraw)
    cout << "You don't have enough funds to cover your withdraw request!" << endl;
    ofstream file;
    file.open("transHistory.txt", std::ios_base::app);
    file << time << "Withdraw $" << setprecision(10) << withdraw;
}
void atm :: ShowTransHist() //Gets the transaction history from the file path string and display
{
   ifstream file;
   string history;
   string time = Date_Time();
   file.open("transHistory.txt");
   if (file.is_open()) {
 while (!file.eof()) { // Reads all lines in the file


    file >> history;
    cout<< history << " ";

 }
}
file.close();

}
string Date_Time() // Function to get date and time
{
    time_t now = time(0);
    string dt=ctime(&now);
     return dt;
}
Last edited on
Clearly you didn't post everything. The word withdraw is no where in the code you posted.
Either that or it's in your input file and and you didn't post that.
Oh my fault, the withdraw is just like the deposit one, but I guess I'd need to show more.
Bumpity
Using std::endl or '\n' to output newlines to the file, just like you do with cout.
Everytime I do that though, it shows up like this

Apr
18
2016
Deposit
250$

It outputs a new line for every space, which is not what I want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void atm :: ShowTransHist() //Gets the transaction history from the file path string and display
{
   ifstream file;
   string history;
   string time = Date_Time();
   file.open("transHistory.txt");
   if (file.is_open()) {
 while (!file.eof()) { // Reads all lines in the file


    file >> history;
    cout<< history << " " << endl;

 }
}


If I do that the above example happens.
Last edited on
Only output newlines where you want them.
If I put it in the deposit/withdraw functions, it still does not output them with a new line, they are all stuck together
Last edited on
Bump.
Line 8: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin operation
  }
I corrected the mistake and it no longer reads an extra record, but I still cannot endl the text on the file, so on the transhistory, it still shows up like Date Deposit 25$ Date Withdraw 25$
Anyone?
Last bump before this is due... I cannot for the life of me figure out how to seperate whats on the file, even this

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
void atm :: deposit(void) // Takes bal variable from bank class and keeps it as a constant total, deposit adds to bal
{
   long double deposit;
   string time = Date_Time(); // So that date for each deposit is saved into transhistory.txt, Date_Time is called and assigned to time
   cout << "Enter how much you would like to deposit: ";
   cin >> deposit;
   bal = deposit+bal;
   cout << "You have deposited: $" << setprecision(10) << deposit << endl; // setprecision is used so that the user can enter lots of money
   if (deposit >= 100000)
    cout << "Now that is a lot of money!" << endl; // Easter egg
    ofstream file;
    file.open("transHistory.txt", std::ios_base::app); // std::ios_base::app used in order to keep more than one user input in file
    file << time << "Deposit $" << setprecision(10) << deposit << endl;

}
void atm :: withdraw(void) // Similar to deposit, but subtracts from bal.
{
    long double withdraw;
    string time = Date_Time();
    cout << "Enter how much you would like to withdraw: ";
    cin >> withdraw;
    if (bal > withdraw || bal == withdraw){
    bal= bal - withdraw;
    cout << "You have withdrawn: $" << setprecision(10) << withdraw << endl;}
    else if (bal < withdraw)
    cout << "You don't have enough funds to cover your withdraw request!" << endl;
    ofstream file;
    file.open("transHistory.txt", std::ios_base::app);
    file << time << "Withdraw $" << setprecision(10) << withdraw << endl;
}


Does not separate the Date Deposit 25$ Date Withdraw 25$ to something like this...

Date Deposit 25$
Date Withdraw 25$
I lied. Bump
final bump!
1
2
3
4
5
6
7
8
9
std::string Date_Time() // Function to get date and time
{
    std::time_t now = time(0);
    std::string dt = std::ctime(&now);

    dt.pop_back() ; // *** added: remove the new-line character at the end

    return dt;
}
Last edited on
std string has no member named pop_back? What does this error mean?
> What does this error mean?

An old (pre-C++11) compiler/library is being used.
http://en.cppreference.com/w/cpp/string/basic_string/pop_back

This would work with legacy C++:
1
2
3
4
5
6
7
8
9
10
std::string Date_Time() // Function to get date and time
{
    std::time_t now = time(0);
    std::string dt = std::ctime(&now);

    // dt.pop_back() ; // *** added: remove the new-line character at the end
    dt.erase( dt.size()-1, 1 ) ;

    return dt;
}
Topic archived. No new replies allowed.