FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

First C++ Code




Did this for my classmate. It's a banking system (deposit, withdraw, inquiry).

Code:


#include <iostream>
#include <iomanip>
using namespace std;

int displayMenu();
double createNew();
void balInquiry(double balance);
double deposit(double balance);
double withdraw(double balance);
double accClosed(double balance);

int access = 0;
int main() {
   int option;
   double balance;
   do {
      option = displayMenu();
      switch(option) {
         case 1: balance = createNew(); break;
         case 2: balInquiry(balance); break;
         case 3: balance = deposit(balance); break;
         case 4: balance = withdraw(balance); break;
         case 5: balance = accClosed(balance); break;
         case 6: break;
      }
   } while(option != 6);

   return 0;
}

int displayMenu() {
   int choice;
   system("cls");
   option:
   cout << "\n****************\n"
       << "* BANK ACCOUNT *\n"
       << "****************\n\n"
       << "MENU\n [1] New Account\n [2] Balance Inquiry\n [3] Deposit\n"
       << " [4] Withdraw\n [5] Close Account\n [6] Exit\n\n > ";
   cin >> choice;

   if(choice < 1 || choice > 6) {
      cout << "Invalid input.\nPress Enter to return.\n";
      system("pause>p");
      goto option;
   }

   if(choice == 1) {
      if(access == 1) {
         access = 1;
         cout << "Cannot perform operation until account is closed."
             <<"\nPress Enter to return.\n";
         system("pause>p");
         goto option;
      }
      else {
         access = 1;
         return choice;         
      }
   }
   else {
      if(access == 1) {
         return choice;
      }
      else {
         cout << "Cannot choose option " << choice << " until option 1 is chosen."
             << "\nPress Enter to return.\n";
         system("pause>p");
         goto option;
      }
   }

   return choice;
}

double createNew() {
   double balance;
   int random = rand() % 22767 + 10000;
   do {
      system("cls");
      cout <<"\nEnter initial balance (> 10000)\n > ";
      cin >> balance;
      if(balance < 10000) {
         cout << "\nInput must be more than 10000.\n"
             << "Press Enter to return.\n";
         system("pause>p");
      }
      else {
         cout << "\nAccount No. " << random << " has been created.\n"
             << "Press Enter to return.\n";
         system("pause>p");
         break;
      }
   } while(balance < 10000);
   return balance;
}

void balInquiry(double balance) {
   cout << "\nYour current balance -> "
       << fixed << showpoint << setprecision(2) << balance << endl
       << "Press Enter to return.\n";
   system("pause>p");
}

double deposit(double balance) {
   double deposit, interest;
   do {
      cout << "\nInput deposit amount (> 100)\n > ";
      cin >> deposit;
      if(deposit < 100) {
         cout << "\nInput must be more than 100.\n"
             << "Press Enter to return.\n";
         system("pause>p");
      }
      else {
         balance += deposit;
         interest = 0.03 * balance;
         balance += interest;
         cout << "\nAmount deposited.\nPress Enter to return.\n";
         system("pause>p");
         break;
      }
   } while(deposit < 100);
   return balance;
}

double withdraw(double balance) {
   double withdraw;
   do {
      cout << "\nInput withdraw amount (> 100 and < balance)\n > ";
      cin >> withdraw;
      if(withdraw < 100 || withdraw > balance) {
         cout << "\nInput must be more than 100 and less than the balance.\n"
             << "Press Enter to return.\n";
         system("pause>p");
      }
      else {
         balance -= withdraw;
         cout << "\nAmount withdrawn.\nPress Enter to return.\n";
         system("pause>p");
         break;
      }
   } while(withdraw < 100 || withdraw > balance);
   return balance;
}

double accClosed(double balance) {
   balance = 0, access = 0;
   cout << "\nAccount closed.\nPress Enter to return.\n";
   system("pause>p");
   return balance;
}




0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.