2016-01-20 49 views
-2

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

#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"; 
     } 
    } 
} 
+0

相当多的代码需要浏览。请体谅并提交一份清单。顺便说一句,“int interest = 1.67;'没有做你想做的事。用调试器查看你自己。 – Bathsheba

+0

刚才我的代码中的主要问题只是在开放账户功能 –

+1

我给你一个大提示。 '<<' and '>>'不是比较运算符。 (请参阅'get_choice'函数)。 – erip

回答

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

愚蠢的错误我知道,一直盯着这个代码约3小时知道一切都刚刚开始看起来一样抱歉。 –

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读取字符串。与此

cin >> customer_name; 

open_account,更换此

std::getline(cin, customer_name); 

而且,解决这一行:

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

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

+0

...或货币数量的浮点类型。 – Bathsheba

+0

或整个'std'命名空间。我们可以继续,但我认为这比这里更适合codereview。 :) – erip

+0

我知道我不应该使用全局变量,这个代码只是从讲师给我们调试和写出测试日志,我已经改变了运营商。如果字符串customer_name仍然会进入无限循环;它有一个空间。 –