Banking/ATM program, how do I set the current date/time for each action the user does?

I already finished my program, but the last step I'm supposed to complete is a transaction history function. I have the foundation down and the time function complete, but everytime I run it, the same time appears for all actions, also, there is a space between every action, which I do not want. So for example, it shows up like this..

Date - 4/16/16 10:55pm

Withdraw $35

Date - 4/16/16 10:55pm (Even though I did it at a different time)

Deposit $100

When I want it to show up on the same line like this...

Date - 4/16/16 10:58pm Withdraw - Amount - Status (whether it went thru or not)

Date - 4/16/16 11pm Withdraw - Amount - Status

Also, my thing does this weird error where it restates the last action twice. so like this...

Date

Deposit

Deposit

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
 void atm :: deposit(void) // Takes bal variable from bank class and keeps it as a constant total, deposit adds to bal
{
   long double deposit;
   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 << "Deposit $" << deposit << endl;
    /*
    use ofstream to save data into transHistory.txt using append:
    ofstream file;
    file.open("transHistory.txt", ios_base::app);
    */

}

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<< time <<  history <<  endl;

 }
}
file.close();

}
string Date_Time() // Function to get date and time
{
    time_t now = time(0);
    string dt=ctime(&now);
     return dt;
}
Bumpity
Okay I figured out the time part, but now, each space in the code shows up on a completely seperate line in the actual program, like so

Date
Deposit
250
Date
Check
Balance
250

Anyone know why that's the case? I want each action to be on the same line like so

Date - Deposit - 250

Here is the updated bit

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
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(10) << 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 << endl;
    /*
    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 << endl;
}
void atm :: displayBalance() // Deposits and withdrawls are taken into account, and the constant total bal is displayed here.
{
    string time = Date_Time();
    cout << "Your current balance is: $" << setprecision(10) << bal << endl;
    if (bal >= 1000000)
        cout << "Wow, you're a high roller!" << endl; // More easter eggs, I have fun with this!
        ofstream file;
        file.open("transHistory.txt", std::ios_base::app);
        file << time << "Check Balance $" << setprecision(10) << bal << endl;

}
void atm :: ShowTransHist() //Gets the transaction history from the file path string and display
{
   ifstream file;
   string history;
   file.open("transHistory.txt");
   if (file.is_open()) {
 while (!file.eof()) { // Reads all lines in the file


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

 }
}
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
Anyone? This is the last step and I can finally turn this in.
I just want to know how to seperate lets say a deposit action with a withdraw action instead of them being on the same line, been trying for over an hour
buuump
Whu do you use endl when you don't want a new line?
 
file << time << "Deposit $" << setprecision(10) << deposit << endl;
I'm going to restate my question as I've edited the code a bit. 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
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 :: 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
Bump
last bump for a while...
Bump. My last code is what I need help figuring out line spacing with.
anyone on here~
bump
Topic archived. No new replies allowed.