2016-11-20 30 views
-2

我已經寫了一個簡單的程序,顯示出售的莎莎總罐,以及最少和最大類型的莎莎出售。該程序工作正常,除非我故意使用壞的用戶輸入,如字母和負數。調用函數inputValidation似乎工作,直到我完成輸入每個莎莎類型出售的所有罐子。然後它顯示大部分結果和崩潰。C++爲什麼我的輸入驗證會崩潰我的程序?

// this program allows a business to keep track of sales for five different types of salsa 

#include <iostream> 
#include <string> 
#include <climits> 
using namespace std; 

// function prototype 
int inputValidation(int); 

int main() 
{ 
    const int SIZE = 5; 
    string names[SIZE] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" }; 
    int jars[SIZE]; 
    int totalSold = 0; 

    // get the number of jars sold for each salsa 
    int tempJars; 
    for (int i = 0; i < SIZE; i++) 
    { 
     cout << "Enter the number of " << names[i] << " salsa jars sold this past month.\n"; 
     cin >> tempJars; 
     // call to input validation function 
     jars[i] = inputValidation(tempJars); 
     totalSold += jars[i]; 
    } 

    // determine the lowest and highest salsa type sold 
    int lowest = jars[0], 
     highest = jars[0], 
     leastType, 
     greatestType; 

    for (int i = 0; i < SIZE; i++) 
    { 
     if (jars[i] < lowest) 
     { 
      lowest = jars[i]; 
      leastType = i; 
     } 

     if (jars[i] > highest) 
     { 
      highest = jars[i]; 
      greatestType = i; 
     } 
    } 

    // display results 
    for (int i = 0; i < SIZE; i++) 
    { 
     cout << "You sold " << jars[i] << " jars of " << names[i] << " salsa.\n"; 
    } 
    cout << "You sold a total of " << totalSold << " jars of salsa.\n" 
     << names[leastType] << " salsa sold the least amount of jars, which was " << lowest << " jars sold.\n" 
     << names[greatestType] << " salsa sold the most amount of jars, which was " << highest << " jars sold.\n"; 
} 

/* 
    definition of function inputValidation 
    inputValidation accepts an int value as its argument. It determines that the value is a number that is 
    greater than 0. If it is not, then the user is prompted to input an acceptable value. the value is 
    returned and stored in the corresponding element of the jars array. 
*/ 

int inputValidation(int jars) 
{ 
    do 
    { 
     while (cin.fail()) 
     { 
      cin.clear(); // clear the error flags 
      cin.ignore(INT_MAX, '\n'); // return cin to usable state 
      cout << "You may only enter non negative numbers. Please try again.\n"; 
      cin >> jars; 
     } 

     if (jars < 0) 
     { 
      cout << "You may only enter non negative numbers. Please try again.\n"; 
      cin >> jars; 
     } 
    } while (jars < 0); 

    return jars; 
} 
+0

你確定了它崩潰的線嗎? –

+2

難道你不覺得簡單地使用'std :: getline()'來讀取一行的輸入值,將它轉換爲'std :: istringstream',然後使用'>>'來解析輸入,而不是在'std :: cin'上做所有這些脆弱的錯誤處理?哦,如果所有數量相同,那麼'leastType'和'greatestType'永遠不會被設置,導致'names [leastType]'和'names [greatestType]'的未定義行爲,這可能是導致崩潰的原因。 –

+0

Sam Varshavchik,你說得對。我將leastType和greatestType初始化爲0,並且我的程序似乎在輸入良好和輸入錯誤的情況下運行得很好。我認爲是這樣。謝謝。 –

回答

1

如果jars[0]恰好是最小的五,然後leastType永遠不會初始化幷包含隨機的垃圾。然後嘗試訪問names[leastType]展示未定義的行爲。

同樣,如果jars[0]是最大的,那麼greatestType永遠不會被初始化。

+0

是的,你是對的,我相信這是我的崩潰的原因。我將leastType和greatestType初始化爲0,現在程序似乎運行良好。 –

相關問題