Home > database >  C looping issues
C looping issues

Time:06-16

I am having difficulty locating and solving a bug in my basic ATM program. The issue manifests in the withdrawal option of both the checking and savings accounts. After the account has zero available funds it will allow the user to enter a negative number essentially over-drafting the account. I am trying to prevent this from happening.

Additionally, if there are zero funds available to withdraw the program continues to loop, thereby not allowing the user to utilize the menu to select a different option.

I have provided examples of the screen output illustrating the issues and the some of the source code.

Issue

Issue

#include <iomanip>
#include <iostream>

using std::cin;
using std::cout;

double checking_balance = 2500.00, savings_balance = 1000.00, savings_amount,
       checking_amount;

int menu;

int main() {
  do {
    // Main Menu
    cout << std::fixed << std::setprecision(2);
    cout << "\n";
    cout << "\tWelcome to Seabreeze Bank\n";
    cout << "*********************************\n\n";
    cout << "1. Savings Account\n";
    cout << "2. Checking Account\n";
    cout << "3. Quit\n\n";
    cin >> menu;
    cout << "\n\n";

    // User validation for the Main Menu
    if (menu < 1 || menu > 3) {
      cout << "You have entered an invalid option.\n";
      cout << "Please enter a number 1-3 > ";
      cin >> menu;
      cout << "\n\n";
    }

    switch (menu) {
    case 1:
      int savings_menu;
      do {
        // Savings Account Menu
        cout << "\t\tSavings Account\n\n";
        cout << "Please enter a menu item (1-3) >\n";
        cout << "*********************************\n";
        cout << "1. Withdrawal\n";
        cout << "2. Deposit\n";
        cout << "3. Main Menu\n";
        cout << "\n\n";
        cin >> savings_menu;
        cout << "\n";
        // Withdrawal selection for Savings Account
        if (savings_menu == 1) {
          cout << "How much would you like to withdraw from your savings "
                  "account: ";
          cin >> savings_amount;
          cout << "\n";
          while (savings_balance < savings_amount) {
            cout << "You do not have enough funds in your account to withdraw "
                    "that much\n";
            cout << "Please enter a smaller amount: ";
            cin >> savings_amount;
            cout << "\n";
          }
          while (savings_amount <= 0) {
            cout << "Please enter an amount greater than 0: ";
            cin >> savings_amount;
          }
          savings_balance -= savings_amount;
          cout << "Your Savings Account Balance: " << savings_balance << "\n\n";
          if (savings_balance == 0) {
            cout << "You now have zero funds in your Savings Account.\n\n";
          }
        }

CodePudding user response:

This is a floating point arithmetic problem.

In general, floating point arithmetic is not exact because of rounding errors, saving_amount is not equal to 0 but it is almost equal to 0.

I prefer the casting option because in certain cases using the round() function will need a bit more modification if the result is negative.

Instead of "while (savings_amount <= 0)" you should use while (int(saving_amount) <= 0). Similarly you have to do casting where you are using conditional operator in your program.

To prevent entering negative number to withdraw, you have to put an "if" statement to check if number is negative.

  • Related