2011-01-19 15 views
0

これまでの私のプログラムでは、私の質問はif文を各cout/cinコードの後に​​含める必要があるのか​​、それを一般化する方法があるのでしょうか? :あなたがしたい場合if文for C++

if(!check(watts, hours_per_day)) 
{ 
    return 1; 
} 

:次に、あなたの主な機能であなたは1と2つのif文を置き換えることができます

bool check(int watts, int hours_per_day) 
{ 
    if (watts< 0) 
    { 
     cout << "Error- negative watts detected " << endl; 
     return false; 
    } 

    if (hours_per_day< 0) 
    { 
     cout << "Error - negative hours/day detected " << endl; 
     return false; 
    } 
} 

#include <iostream> 
using namespace std; 

int main() 
{ 
    double watts, hours_per_day, watt_hours, dollars_per_wh, result; 

    dollars_per_wh= .00008; 

    cout << " How many Watts for the Air conditioner? "; 
    cin >> watts; 
    cout << " How many hours/day do you run the Air Conditioner? "; 
    cin >> hours_per_day; 

    if (watts< 0) 
    { 
     cout << "Error- negative watts detected " << endl; 
     return 1; 
    } 

    if (hours_per_day< 0) 
    { 
     cout << "Error - negative hours/day detected " << endl; 
     return 1; 
    } 

    cout << "How many Watts for the Television? " ; 
    cin >> watts; 
    cout << "How many hours/day do you run the Television? " ; 
    cin >> hours_per_day; 

    if (watts< 0) 
    { 
     cout << "Error- negative watts detected " << endl; 
     return 1; 
    } 

    if (hours_per_day< 0) 
    { 
     cout << "Error - negative hours/day detected " << endl; 
     return 1; 
    } 

    cout << "How many Watts for the Washer? " ; 
    cin >> watts; 
    cout << "How many hours/day do you run the Washer? " ; 
    cin >> hours_per_day; 

    if (watts< 0) 
    { 
     cout << "Error- negative watts detected " << endl; 
     return 1; 
    } 

    if (hours_per_day< 0) 
    { 
     cout << "Error - negative hours/day detected " << endl; 
     return 1; 
    } 

    return 0 ; 
} 
+3

重要なチェックがありません。入力が実際に成功したことを確認するために入力を検証していません。たとえば、ワット数を尋ねられたら "abcdef"を入力してみてください。入力操作後にストリームの状態をチェックする必要があります(例えば 'if(!(cin >>ワット)){/ *エラー* /}を処理する 'かそれに類するものを使用します)。これらすべての一般化についてはもちろん、それらの検証ブロックを別々の関数にリファクタリングすることができます。ブロックごとに、共通するものを新しい関数に移動し、共通でないものを引数として渡します。 –

+0

"...一般化する方法はありますか?"はい、それはまさに関数の主な目的の一つです。以下にいくつかの例がありますので、私は別の例を追加しません。これはAC、TV、洗濯機の各ケースに適用できる関数を書く方法を学ぶ良いプロジェクトのように見えます。 –

答えて

2

あなたは、2つのパラメータを取る関数を書くことができますすべての入力を最初に収集してから評価し、次にワットとhours_per_dayの配列を使用することができます。次に、配列を実行して各エントリを確認することができます。

1

はい、あなたは別の関数にそれらを引き出すことができます。

void cinNonNegative(double &x) 
{ 
    cin >> x; 

    if (x< 0) 
    { 
     cout << "Error- negative value detected " << endl; 
     exit(1); 
    } 
} 

int main() 
{ 
    ... 
    cout << " How many Watts for the Air conditioner? "; 
    cinNonNegative(watts); 
    cout << " How many hours/day do you run the Air Conditioner? "; 
    cinNonNegative(hours_per_day); 
    ... 
} 

そして、あなたは、エラーメッセージについてより具体的にしたい場合(たとえば、代わりに「負の値」の「負ワット」)することができます変数の名前(例えば「ワット」)のために別のパラメータをcinNonNegativeに追加してください。

+1

あなたのコードでは、入力の抽出が成功したことを確認するためのチェックがまだありません。 –

+0

Jamesが推奨するチェックを実装するには、 'if(!(cin >> x)|| x <0)...'を入力するだけです。 –

+0

@James McNellis:私のコードには、OPのコードにはそのチェックがないので、そのチェックはありません。あなたは正しい、その小切手は良い習慣だが、それは求められたものではなく、私は自分の答えを短くしたい。 – Beta

0

次のソリューションは、あなたに機能できます:

  • はあなたが
  • 受信されるべき値に名前を付けることができます関数が成功した場合/
  • を失敗したと言うブール値を返しますが、あなたが設定することができます最小値と最大値

必要に応じて、他の種類の入力を得るための整数やその他の関数を得るための他のカスタム関数を構築することができます。この方法で、すべての入力テストを1つの場所に集中できます。

#include <iostream> 
using namespace std; 

bool getint(int &result, const char *name, int minValue, int maxValue) 
{ 
    bool success = false; 
    int value = 0; 
    cout << "Please enter " << name << ": "; 
    if (!(cin >> value)) 
    { 
     cout << "Error: bad input detected" << endl; 
    } 
    else if (value < minValue) 
    { 
     cout << "Error: " << name << " is less than " << minValue << endl; 
    } 
    else if (value > maxValue) 
    { 
     cout << "Error: " << name << " is more than " << maxValue << endl; 
    } 
    else 
    { 
     success = true; 
     result = value; 
    } 
    return success; 
} 


int main() 
{ 
    int watts; 
    getint(watts, "Watts for the AC", 0, 10000); 

    return 0; 
} 
+0

すべてのその検証、それからあなたはメインでそれを使用していない! :-) –

+0

この関数は重要なことです:-)メインは疑問のために明らかに不完全です... –