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...
void atm :: deposit(void) // Takes bal variable from bank class and keeps it as a constant total, deposit adds to bal
{
longdouble 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;
}
void atm :: deposit(void) // Takes bal variable from bank class and keeps it as a constant total, deposit adds to bal
{
longdouble 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.
{
longdouble 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;}
elseif (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;
}
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
void atm :: deposit(void) // Takes bal variable from bank class and keeps it as a constant total, deposit adds to bal
{
longdouble 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;
}