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 :: 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;
}
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;
}
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.
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;
}
}
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.
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$
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(); // 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.
{
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;
}
Does not separate the Date Deposit 25$ Date Withdraw 25$ to something like this...
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 endreturn dt;
}
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;
}