2016-01-20 96 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仍然會進入無限循環;它有一個空間。 –