This is the first program i made using ifstram and fstream. its kinda like the title says a checking book program.
i dont know what you guys will think of it but everything works and im pretty proud of it becuase i made it after reading about fstream for about 10 min and then asking a few qquestions here
ignore the 2 comments at the very bottom of the code. i did not feel like taking em out.
GeSHi (cpp):
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
char Use;
char run_over;
/* date / time chars */
char dateStr[9];
char timeStr[9];
/* date / time chars */
cout << "Welcome to the check book program" << endl;
do
{
cout <<"1-Deposit\n2-Withdraw\n3-Check balance\n4-Transaction History\nPlease choose one :";
cin >> Use;
if( Use >='1' && Use <='4')
{
switch(Use)
{
case '1':
{
long double deposit;
long double money_output;
long double deposit_amount;
ifstream infile;
ofstream outfile;
//opens the file
infile.open("MoneyFile.txt");
if(!infile.good())
{
infile.close();
cout << "\nThis is your first time using";
cout << " the checking book program";
cout << ".\nYour balance will be set to 0\n";
outfile.open("MoneyFile.txt");
outfile << 0;
outfile.close();
}
else
{
//checks for the amount of money
infile >> money_output;
infile.close();
cout << "Your current balance is "<< money_output;
cout << "\nHow much would you like to deposit? :";
cin >> deposit;
/* ===does date and time saving=== */
_strdate( dateStr );
_strtime( timeStr );
outfile.open("DepositHistory.txt",ios::app);
char date[] = "Deposit date ";
outfile << date;
outfile << dateStr;
char time[] = " Deposit time ";
outfile << time;
outfile << timeStr;
char amount[] = " amount: ";
outfile << amount << deposit << '\n';
outfile.close();
/* ===end of date and time stuff=== */
deposit_amount = money_output + deposit;
outfile.open("MoneyFile.txt");
//writes to the file
outfile << deposit_amount;
//closes the file
outfile.close();
cout <<"Your current balance is " << deposit_amount;
}
}
break;
case '2':
{
long double withdraw;
long double new_amount;
long double money_output;
ifstream infile;
ofstream outfile;
//opens file
infile.open("MoneyFile.txt");
if(!infile.good())
{
infile.close();
cout << "\nThis is your first time using";
cout << " the checking book program";
cout << ".\nYour balance will be set to 0\n";
outfile.open("MoneyFile.txt");
outfile << 0;
outfile.close();
}
else
{
infile >> money_output;
infile.close();
if(money_output == 0)
{
cout << "Your balance is " << money_output << " so you dont have any money to take out" << endl;
cout << "Please deposit some money first" << endl;
break;
}
else
{
cout << "Your current balance is "<< money_output << "\nHow much would you like to take out? :";
cin >> withdraw;
if (withdraw > money_output)
{
cout << "You can't take out more then you have" << endl;
break;
}
else
{
/* ===does date and time saving=== */
_strdate( dateStr );
_strtime( timeStr );
outfile.open("DepositHistory.txt",ios::app);
char date[] = "Withdraw date ";
outfile << date;
outfile << dateStr;
char time[] = " Withdraw time ";
outfile << time;
outfile << timeStr;
char amount[] = " amount: ";
outfile << amount << withdraw << '\n';
outfile.close();
/* ===end of date and time stuff=== */
//finds out the new balance
new_amount = money_output - withdraw;
outfile.open ("MoneyFile.txt");
outfile << new_amount;
//closes file
outfile.close();
cout << "Your balance is " << new_amount;
}
}
}
}
break;
case '3':
{
long double money_output = 0.0;
ifstream infile;
ofstream outfile;
//opens file
infile.open("MoneyFile.txt");
if(!infile.good())
{
infile.close();
cout << "\nThis is your first time using";
cout << " the checking book program";
cout << ".\nYour balance will be set to 0\n";
outfile.open("MoneyFile.txt");
outfile << 0;
outfile.close();
}
else
{
infile >> money_output;
infile.close();
/* ^v did this becuase from what i know if you
do the top one it will take the number out
and store it in money_output but then the
file will have nothing in it
the bottom one will stick the anoumt back into
the file.
*/
/*
outfile.open("MoneyFIle.txt");
outfile << money_output;
//closes file
outfile.close();
*/
cout << "Your current balance is "<< money_output;
}
}
break;
case '4':
{
cout << "\n\n ";
cout << " Transaction history:" << endl;
ifstream infile;
infile.open("DepositHistory.txt");
while(!infile.eof())
{
char history;
infile.get( history);
cout << history;
}
infile.close();
}
break;
default :
{
cout << "Invalid Operation Please try again!" << endl;
}
break;
}
}
else
{
cout << "Choose a correct option" << endl;
}
do
{
cout << "\nWould you like to make another withdraw or deposit?(y/n) :";
cin >> run_over;
if( run_over != 'n' && run_over != 'N' && run_over != 'y' && run_over != 'Y')
{
cout << "\nEnter a Y or N" << endl;
}
}
while( run_over != 'n' && run_over != 'N' && run_over != 'y' && run_over != 'Y');
}
while( run_over == 'y' || run_over == 'Y' );
}
Created by GeSHI 1.0.7.18
lemme know what you guys think
