2016-03-06 128 views
-2

好的,因此對於我的學校項目,我們基本上製作了一個菜單,其中包含最多20位用戶輸入信息並在需要時進行更改。一切工作正常。但是,我們的任務讓我們檢查郵政編碼和帳戶餘額的整數值輸入。我使用了一個用於ZipCode驗證的do-while循環,直到輸入正數和數字。但是,我得到了一個無法修復的死循環。這是我的代碼。如果你把它放到編譯器中,錯誤在57-68行。每次我輸入一個字母而不是一個整數,我就會得到一個無限循環。但我不明白爲什麼。謝謝!我的程序中的無限循環

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cstdlib> 

using namespace std; 

struct Account //Structure to be used throughout 
{ 
    string CustomerName; 
    string CustomerAddress; 
    string City; 
    string State; 
    int ZIPCode; 
    string Telephone; 
    int AccountBalance; 
    string DateOfLastPayment; 

}; 



//function prototypes 
void valueChangeFunc(string, Account[], int);//This function will be used in option 2 for editing a certain customer information 



int main() 
{ 
    Account array[20]; //Array to hold up to 20 customers 
    int choice; //this will hold which option the user decides to make 1-4 
    bool flagZip = false; //This will be used later on as a flag for a do-while loop 
    bool flag = false; //This will be used as a flag for the do-while loop right now 
    int index = 0; //Index for our customers. This tells how many customers have been entered 
    do //This do while loop will continue to ask the user what option to do until array fills up with 20 people and 4 is not entered 
    { 
    cout << "1. Enter new account information \n" <<endl 
    << "2. Change account information \n" << endl 
    << "3. Display all account information\n" <<endl 
    << "4. Exit the program \n" <<endl; 
    cin >> choice; 
if (choice > 4 || choice <= 0)//If user enters a number bigger than 4 or less then or equal to 0. Error! 
cout << "Please enter a number between 1 and 4" << endl; 

else if(choice == 1) 
{ 
cout << "CustomerName: "; 
     cin.ignore(); 
    getline(cin, array[index].CustomerName); 
    cout << "CustomerAddress "; 
    getline(cin, array[index].CustomerAddress); 
    cout << "City: "; 
    getline(cin, array[index].City); 
    cout << "State: "; 
    getline(cin, array[index].State); 

    do 
    { 
    cout << "Zip Code: "; 
    cin >> array[index].ZIPCode; 
    cin.ignore(); 
    if (!isdigit(array[index].ZIPCode) && array[index].ZIPCode <= 0) 
    cout << "Please enter a valid entry " << endl; 

    else 
    flagZip = true; 

    }while(flagZip == false); 

    cout << "Telephone: "; 
    getline(cin, array[index].Telephone); 

    flagZip = false; 

    do 
    { 
    cout << "AccountBalance: "; 
    cin >> array[index].AccountBalance; 
    cin.ignore(); 
    if (array[index].AccountBalance <= 0) 
    cout << "Please enter a valid entry " << endl; 

    else 
    flagZip = true; 
    }while(flagZip == false); 

    cout << "DateOfLastPayment: "; 
    getline(cin, array[index].DateOfLastPayment); 


    cout << "\n\nCustomerName: " << array[index].CustomerName << endl; 
    cout << "CustomerAddress " << array[index].CustomerAddress <<endl; 
    cout << "City: " << array[index].City << endl; 
    cout << "State: " << array[index].State << endl; 
    cout << "Zip Code: " << array[index].ZIPCode << endl; 
    cout << "Telephone: " << array[index].Telephone <<endl; 
    cout << "AccountBalance: " << array[index].AccountBalance << endl; 
    cout << "DateOfLastPayment: " << array[index].DateOfLastPayment << endl; 
    cout << "You have entered information for customer number " << index << endl << endl; 

    index++; 
} 
else if(choice == 2 && index != 0) 
{ 
int num; 
string valueChange; 
do 
{ 
cout << " Customer number: "; 
cin >> num; 

if (num > (index-1) || num < 0) 
cout << " There is no customer with that number " << endl; 

}while (num > (index-1)); 
    cout << "\n\nCustomer Name: " << array[num].CustomerName << endl; 
    cout << "Customer Address " << array[num].CustomerAddress <<endl; 
    cout << "City: " << array[num].City << endl; 
    cout << "State: " << array[num].State << endl; 
    cout << "ZIPCode: " << array[num].ZIPCode << endl; 
    cout << "Telephone: " << array[num].Telephone <<endl; 
    cout << "Account Balance: " << array[num].AccountBalance << endl; 
    cout << "Date of last payment: " << array[num].DateOfLastPayment << endl; 
    cout << "You have requested information for customer number " << num << endl << endl; 

    cout << "What value do you want to change? (press 4 to change 'Date of last payment') \n"; 
    cin.ignore(); 
    getline(cin,valueChange); 

    valueChangeFunc(valueChange, array, num); 

    cout << "\nHere is the new value you entered for " << valueChange << endl; 

    cout << "\n\nCustomer Name: " << array[num].CustomerName << endl; 
    cout << "Customer Address " << array[num].CustomerAddress <<endl; 
    cout << "City: " << array[num].City << endl; 
    cout << "State: " << array[num].State << endl; 
    cout << "ZIPCode: " << array[num].ZIPCode << endl; 
    cout << "Telephone: " << array[num].Telephone <<endl; 
    cout << "Account Balance: " << array[num].AccountBalance << endl; 
    cout << "Date of last payment: " << array[num].DateOfLastPayment << endl << endl; 

} 

else if(choice == 3 && index != 0) 
{ 
    int num2; 
do 
{ 
    cout << "Enter the Customer Number to display information regarding that customer" << endl; 
    cin >> num2; 
if (num2 > (index-1) || num2 < 0) 
cout << "That Customer does not exist " <<endl; 
} 
while(num2 > (index-1)); 


    cout << "\n\nCustomerName: " << array[num2].CustomerName << endl; 
    cout << "CustomerAddress " << array[num2].CustomerAddress <<endl; 
    cout << "City: " << array[num2].City << endl; 
    cout << "State: " << array[num2].State << endl; 
    cout << "Zip Code: " << array[num2].ZIPCode << endl; 
    cout << "Telephone: " << array[num2].Telephone <<endl; 
    cout << "AccountBalance: " << array[num2].AccountBalance << endl; 
    cout << "DateOfLastPayment: " << array[num2].DateOfLastPayment << endl; 
    cout << "You have entered information for customer number " << index << endl << endl; 

} 

else 
flag = true; 

}while (flag == false); 

    return 0; 
} 

void valueChangeFunc(string valueChange2, Account array[], int num) 
{ 
    if (valueChange2 == "Customer Name" || valueChange2 == "Customer name" || valueChange2 == "customer Name" || valueChange2 == "customer name") 
    { 
    cout << "\nEnter new value for Customer Name: " <<endl; 
    getline(cin, array[num].CustomerName); 

    } 

    if (valueChange2 == "Customer Address" || valueChange2 == "Customer address" || valueChange2 == "customer Address" || valueChange2 == "customer address") 
    { 
    cout << "\nEnter new value for Customer Address: " <<endl; 
    getline(cin, array[num].CustomerAddress); 

    } 
    else if(valueChange2 == "city" || valueChange2 == "City") 
    { 
     cout << "\nEnter new value for City: " << endl; 
     getline(cin, array[num].City); 

    } 
    else if(valueChange2 == "state" || valueChange2 == "State") 
    { 
     cout << "Enter a value for State: " << endl; 
     getline(cin,array[num].State); 

    } 

    else if(valueChange2 == "Zip Code" || valueChange2 == "zip Code" || valueChange2 == "Zip code" || valueChange2 == "zip code") 
    { 
     cout << "\nEnter a value for Zip Code: " << endl; 
     cin >> array[num].ZIPCode; 

    } 

    else if(valueChange2 == "telephone" || valueChange2 == "Telephone") 
    { 
     cout << "\nEnter a value for Telephone: " << endl; 
     getline(cin, array[num].Telephone); 

    } 

    else if(valueChange2 == "Account Balance" || valueChange2 == "Account balance" || valueChange2 == "account Balance" || valueChange2 == "account balance") 
    { 
     cout << "\nEnter a value for account balance: " << endl; 
     cin >> array[num].AccountBalance; 

    } 
    else if(valueChange2 == "4") 
    { 
     cout << "\nEnter the value for Date of last payment: " << endl; 
     getline(cin, array[num].DateOfLastPayment); 

    } 

    else 
    cout << "Not entered correctly. Please enter a valid entry to edit " << endl; 

} 

再次一切工作,直到我開始使用循環來檢查ZipCode的數字值。當我輸入一個字母時,循環只能是無限的。它適用於負數和正數。

+0

調試器可以在幾秒鐘內爲您找到它。值得學習使用它的時間。 – user4581301

+0

gcc 5.3編譯這段代碼時沒有任何錯誤。如果您不理解編譯器的錯誤消息,除了發佈代碼之外,還應該包含來自編譯器的實際錯誤。 –

+1

編譯器沒有錯誤。當我在ZipCode部分輸入一個字母時,只是一個無限循環。 –

回答

0

簡短的回答,可以發現in cplusplus.com(讀第三段)

龍答:

此錯誤是不是唯一的郵編,就可以產生同樣的錯誤,當你輸入一個字母,而不是號碼在第一個cin呼叫。

cin不是類型安全,這樣使用一個錯誤類型爲未定義的行爲(比如您所遇到的無限循環)的輸入結果,這就是爲什麼cin是有點容易出錯。此外,cin獲得輸入值,但它不會刪除換行符,從其他方法獲得的內容看起來更骯髒。

說到其他方法,如鏈接所述,儘可能使用getline()。您可以使用該函數獲取緩衝區包含的字符串,並在必要時執行其他類型檢查。無缺陷程序比短程序更重要。

作爲個人提示:儘量使用while而不是do..while。這不是編程的一般規則,但它看起來更具可讀性並且通常更容易理解(恕我直言)。

另外,嘗試使用函數將程序拆分爲多個部分。例如,您正在打印存儲在屏幕上的信息的塊可以變成一個函數。你正在使用相同的cout結構(它會縮短你的代碼4 * 9行)。

最後,如果你使用一個整數作爲分離算法的密鑰值,嘗試使用switch...case而不是if-else塊。 (再次,只是我的意見)。

+1

謝謝!很多偉大的建議!我是一名Java程序員,所以我剛剛啓動C++。由於使用Java,我不熟悉'cin'輸入錯誤。所以謝謝!我明白你的意見是要使某個功能失效。我沒有這樣做的原因是因爲我的老師做了限制。但是,我再次看到你的觀點。謝謝你的建議! –