2016-11-20 3 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()'を使って1行分の入力を読み込み、それを 'std :: istringstream'に変換し、' >> 'を使って入力を解析する方が簡単だとは思いませんか?この脆弱なエラー処理を 'std :: cin'で行うのではなく、ああ、すべての数量が同じであれば、 'leastType'と' greatestType'は決して決して設定されません。 'names [leastType]'と 'names [greatestType]'の動作は未定義です。 –

+0

Sam Varshavchik、あなたが正しくありました。私はleastTypeとgreatestTypeを0に初期化し、私のプログラムは良い入力と悪い入力でうまく動作するように見えました。私はそうだったと思う。ありがとうございました。 –

答えて

1

jars[0]は5の最小であることを起こる場合は、leastTypeが初期化されず、ランダムなゴミが含まれていないされています。次に、names[leastType]にアクセスしようとすると、定義されていない動作が発生します。

同様に、jars[0]が最大の場合、greatestTypeは決して初期化されません。

+0

はい、あなたは正しいです、私はこれが私のクラッシュの原因だと信じています。私はleastTypeとgreatestTypeを0に初期化し、プログラムは今は正常に動作しているようです。 –

関連する問題