C++ Learning Community Forum
August 01, 2010, 03:29:31 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: checking book type thing  (Read 666 times)
oulyt
C++ Freak
***
Posts: 340



View Profile
« on: January 20, 2009, 12:33:44 AM »

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.

Code
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 Cheesy
Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #1 on: January 20, 2009, 07:24:25 PM »

Why not do this, in one place
Code:
ofstream outfile("MoneyFile.txt");
Maybe break up your code into a few functions where it makes sense as well.
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #2 on: January 20, 2009, 07:27:46 PM »

yeah. o.0 i should of written functions from the start. but i wanted to make sure i was doing it right. i'll rewrite it with functions
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!