2016-08-27 3 views
0

私は、ユーザの入力を受け取り、ユーザが購入するユニットの数に基づいて割引を計算するコードを書いています。ここに私の問題がある。私は入力の検証を使用して、0から65535(符号なし整数の最大範囲)の間であることを確認したいが、ユーザがこの範囲外の数値を入力した場合、私はこのプログラムを設定する方法を知りたいオーバーフロー/アンダーフローが発生し、不正な数値がif/else節にヒットする前に変数に格納されます。私はC + +の初心者ですので、親切にしてください。ユーザーが入力したときにこの番号が正しい範囲内にあるかどうかを確認するにはどうすればよいですか。また、ユーザーが数字以外の文字を入力していないことを確認する方法はありますか? はここに私のコードです:C++の入力検証とオーバーフロー

#include<iostream> 
#include<iomanip> 

using namespace std; 

int main() 
{ 
    // display the instructions and inform the user of the discounts available 

    cout << " This software package sells for $99. Discounts are given according to the following list: \n"; 
    cout << "----------------------------------" << endl; 
    cout << " Quantity\t\t Discount" << endl; 
    cout << "----------------------------------" << endl; 
    cout << " 10 - 19\t\t 20%" << endl; 
    cout << " 20 - 49\t\t 30%" << endl; 
    cout << " 50 - 99\t\t 40%" << endl; 
    cout << " 100 or more\t\t 50%" << endl; 
    cout << "----------------------------------" << endl; 
    cout << "\n\n"; 

    const double price = 99.00; 

    unsigned short quantity; // variable to hold the user's quantity. 
           // shouldn't need more than 2 bytes for this (unsigned short) 

    cout << "How many units are sold?: "; 
    cin >> quantity; 

    double discount; // variable to hold the amount discounted 
    double total; // to hold the total sales price 

    cout << fixed << showpoint << setprecision(2); // set the display of numeric values 

    // calculate the discounted prices 

    if (quantity >= 1 && quantity <= 9) { 

     total = quantity * price; // calculate the total without a discount 

     cout << "There is no discount for this order \n"; 
     cout << quantity << " units were sold at $" << price << " a piece for a total of " << total << endl; 
     cout << "\n"; 
    } 
    else if (quantity >= 10 && quantity <= 19) { 

     discount = (quantity * price) * .20; // calculate the discount 
     total = (quantity * price) - discount; // calculate the total 

     cout << "There is a 20% discount \n"; 
     cout << quantity << " units were sold at $" << price << " with a discount of 20% applied to the order. \n"; 
     cout << "The total cost of the sale is $" << total << endl; 
     cout << "\n"; 
    } 
    else if (quantity >= 20 && quantity <= 49) { 

     discount = (quantity * price) * .30; // calculate the discount 
     total = (quantity * price) - discount; // calculate the total 

     cout << "There is a 30% discount \n"; 
     cout << quantity << " units were sold at $" << price << " with a discount of 30% applied to the order. \n"; 
     cout << "The total cost of the sale is $" << total << endl; 
     cout << "\n"; 
    } 
    else if (quantity >= 50 && quantity <= 99) { 

     discount = (quantity * price) * .40; // calculate the discount 
     total = (quantity * price) - discount; // calculate the total 

     cout << "There is a 40% discount \n"; 
     cout << quantity << " units were sold at $" << price << " with a discount of 40% applied to the order. \n"; 
     cout << "The total cost of the sale is $" << total << endl; 
     cout << "\n"; 
    } 
    else if(quantity > 99 && quantity <= 65535) { 

     // the maximum number allowed in a short int is 65535. I is unrealistic that someone would order more 
     // units than that so this else if clause checks to make sure the number of ordered items is below this number 

     discount = (quantity * price) * .50; // calculate the discount 
     total = (quantity * price) - discount; // calculate the total 

     cout << "There is a 50% discount \n"; 
     cout << quantity << " units were sold at $" << price << " with a discount of 50% applied to the order. \n"; 
     cout << "The total cost of the sale is $" << total << endl; 
     cout << "\n"; 
    } 
    else { 

     // the trailing else clause is used to catch any value for quantity that is 0 or below or any quantity 
     // bigger than what a short int can hold. 

     cout << "You entered an invalid quantity.\n"; 
     cout << "Please enter a value greater than 0 or less than 65,535. \n\n"; 
    } 




    system("pause"); 
    return 0; 
} 

0の値が入力されたときに最後のelse句は実行されます。範囲外の値を持つ出力の例を次に示します。

This software package sells for $99. Discounts are given according to the following list: 
---------------------------------- 
Quantity    Discount 
---------------------------------- 
10 - 19     20% 
20 - 49     30% 
50 - 99     40% 
100 or more    50% 
---------------------------------- 


How many units are sold?: 65600 
There is a 50% discount 
52428 units were sold at $99.00 with a discount of 50% applied to the order. 
The total cost of the sale is $2595186.00 

Press any key to continue . . . 
+0

を使用することです。今のところこのC++クラスでは、私たちはちょうど始動していますが、whileループを学習していないのですが、whileループを使わずに解決策を見つける手助けがあればお願いします。 –

+0

'は// 2バイトを超える必要はありませんこれは(署名されていない短い) 'それを賢くしようとしないでください。 ( 'unsigned int'ではなく)' quantity'に通常の 'int'を使うだけで、あなたのチェックは期待どおりに動作します。 –

+0

あなたは 'while'ループを自分で学ぶことができます。彼らはとてもシンプルで、インストラクターが感心すると確信しています。また、[How to Ask](https://stackoverflow.com/help/how-to-ask)を読んで、[最小限の、完全で検証可能な例]を投稿してください(https://stackoverflow.com/help/mcve)。 – Xiobiq

答えて

0

すべての正直なところ、これは悪い質問ではありません。 cin >> quantityから出てくるものをエラーチェックするだけです。

User Input of Integers - Error Handlingのように、これを処理する方法は、cin >> quantityを次のようなエラー処理コードで囲むことです。これは、整数の世話をしません

if (cin >> quantity) { 
    // read succeeded 
} else if (cin.bad()) { 
    // IO error 
} else if (cin.eof()) { 
    // EOF reached (perhaps combined with a format problem) 
} else { 
    // format problem 
} 

がオーバーフローするので、完全なソリューションがquantityintを作り、私は学校で他の言語を研究している

cout << "How many units are sold?: "; 
if (cin >> quantity) { 
    // read succeeded 

    // check for range 
    if (quantity < 0 || quantity > 65535) { 
     cout << "Number needs to be between 0 and 65535" << endl; 
     return -1; 
    } 
} else if (cin.bad()) { 
    // IO error 
    cout << "Couldn't do a read from stdin :(" << endl; 
    return -1; 
} else if (cin.eof()) { 
    // EOF reached (perhaps combined with a format problem) 
    cout << "Stdin gave EOF :(" << endl; 
    return -1; 
} else { 
    // format problem 
    cout << "Encountered incorrect format" << endl; 
    return -1; 
} 
+0

私の質問を嫌ってくれてありがとうございました。私はそれが公正だと思った –

+0

@ HelpMeI'mStupidはいあなたの質問は悪くないが、いくつかの改善を使用することができます。それにもかかわらず、いくつかの間違いにもかかわらず、私はあなたに質問しました。 –