程序陷入无限循环

时间:2016-01-20 11:43:36

标签: c++

我试图让这个程序正常运行。它应该像伪代码一样按照描述执行,但是当我执行程序并尝试打开一个新帐户时,如果我在客户名称字段中放置了多个字符,程序就会进入一个无限循环,我知道如何修复这个问题。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
using namespace std;

int choice, account_number;
long acc_entry;
long acc_no = 112280;
double balance, deposit, withdrawal;
int interest = 1.67;
string customer_name;

void display_menu();
void get_choice();
void menu_selection(int selection);
void open_account();
void make_withdrawal();
void make_deposit();
void add_interest();
void display_transaction();

void main()
{
    get_choice();

}

void display_menu()
{
    system("CLS");
    cout << "\n\n\t\t\t\tACCOUNT MENU";
    cout << "\n\t\t\t\t============\n";
    cout << "\n\t\t\t\t1. Open Account";
    cout << "\n\t\t\t\t2. Make Withdrawal";
    cout << "\n\t\t\t\t3. Make Deposit";
    cout << "\n\t\t\t\t4. Add Interest";
    cout << "\n\n\t\t\t\t5. Exit";
}

void open_account()
{
    system("CLS");
    cout << "\n\n\t\t\t\tOPEN ACCOUNT";
    cout << "\n\t\t\t\t============\n\n";
    cout << "\tPlease enter your name\n\n\t";
    cin >> customer_name;
    cout << "\n\n\tPlease enter initial despoit\n\n\t";
    cin >> deposit;
    balance = balance + deposit;
    account_number = acc_no + 1;
    cout << "\n\n\tYour new account number\n\n\t" << setfill('0') << setw(8) << account_number;
    get_choice();
}

void make_withdrawal()
{
    system("CLS");
    cout << "\n\n\t\t\t\tMAKE WITHDRAWAL";
    cout << "\n\t\t\t\t===============\n\n";
    cout << "\tPlease enter Account Number\n\n\t";
    cin >> acc_entry;
    if (acc_entry == account_number)
    {
        cout << "\n\n\tPlease enter amount to withdraw\n\n\t";
        cin >> withdrawal;
        if (withdrawal > balance)
        {
            cout << "\n\n\tYou are exceeding your limit";
            cin.ignore();
            cin.get();
        }
        else
        {
            balance = balance - withdrawal;
            cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
            cin.ignore();
            cin.get();
        }
    }
    else
    {
        cout << "\n\n\tAccount number does not exist.";
        cin.ignore();
        cin.get();
    }
    get_choice();
}

void make_deposit()
{
    system("CLS");
    cout << "\n\n\t\t\t\tMAKE DEPOSIT";
    cout << "\n\t\t\t\t============\n\n";
    cout << "\tPlease enter Account Number\n\n\t";
    cin >> acc_entry;
    if (acc_entry == account_number)
    {
        cout << "\n\n\tPlease enter amount to deposit\n\n\t";
        cin >> deposit;
        balance = balance + deposit;
        cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
        cin.ignore();
        cin.get();
    }
    else
    {
        cout << "\n\n\tAccount number does not exist.";
        cin.ignore();
        cin.get();
    }
    get_choice();
}

void add_interest()
{
    string yn;
    system("CLS");
    cout << "\n\n\t\t\t\tADD INTEREST";
    cout << "\n\t\t\t\t============\n\n";
    cout << "\tPlease enter Account Number\n\n\t";
    cin >> acc_entry;
    if (acc_entry == account_number)
    {
        cout << "\n\n\tDo you wish to add interest [Y/N]\n\n\t";
        getline(cin, yn);
        if (yn == "Y" || yn == "y")
        {
            balance = balance * interest;
            cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
            cin.ignore();
            cin.get();
        }
    }
    else
    {
        cout << "\n\n\tAccount number does not exist.";
        cin.ignore();
        cin.get();
    }
    get_choice();
}

void display_transaction()
{
    system("CLS");

    cout << "\n\n\t\t\t\tCLOSED";
    cout << "\n\t\t\t\t======\n\n";
    if (account_number != 112280)
    {
        cout << "\tCustomer Name  : - " << customer_name;
        cout << "\n\n\tAccount Number : - " << setfill('0') << setw(8) << account_number;
        cout << "\n\n\tBalance        : - " << fixed << setprecision(2) << (char)156 << balance << "\n\n";
    }
    cin.get();
}

void get_choice()
{
    display_menu();
    do
    {

        cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
        cin >> choice;
        menu_selection(choice);
    } while 
        (choice << 1 || choice >> 5);
        cin.ignore();

}

void menu_selection(int a)
{
    switch (a)
    {
    case 1:
    {
        open_account();
        break;
    }
    case 2:
    {
        make_deposit();
        break;
    }
    case 3:
    {
        make_withdrawal();
        break;
    }
    case 4:
    {
        add_interest();
        break;
    }
    case 5:
    {
        display_transaction();
        break;
    }
    default:
        {
        cout << "hello";
        }
    }
}

2 个答案:

答案 0 :(得分:2)

void get_choice()
{
    display_menu();
    do
    {

        cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
        cin >> choice;
        menu_selection(choice);
    } while 
        (choice < 1 || choice > 5); // << and >> aren't for comparison
        cin.ignore();

}

答案 1 :(得分:2)

get_choice函数中,您有一个do-while循环,其中包含以下条件:

while (choice << 1 || choice >> 5); // this is going to run for a bit

<<>>不是比较运算符;相反,它们是bit shift operators

因为您的选择可以向左或向右移动,所以它会进入无限循环。这是一个简单的修复,真的 - 将它们改为比较运算符!

对于客户名称,如果有空格或换行符,std::cin将在第一个停止读取。请务必使用std::getline从stdin读取字符串。

open_account中,将其替换为:

cin >> customer_name;

用这个:

std::getline(cin, customer_name);

另外,请修改此行:

int interest = 1.67; // This should be a double

你不应该真的使用全局变量,但那是一个完全不同的故事。