2011-01-19 88 views
0

我的程序到目前爲止,我的問題是我必須包括每個cout/cin代碼之後的if語句還是有一種方法來推廣它? :if語句爲C++

#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 >> watts)){/ * handle error * /}`或類似的東西)之後檢查流的狀態。至於所有這一切的概括,當然你可以將這些驗證塊重構爲一個單獨的函數;對於每個塊,將共同的東西移動到一個新的函數中,並傳遞那些不常見的東西作爲參數。 – 2011-01-19 06:45:59

+0

「......有沒有辦法將其推廣?」是的,這正是功能的主要目的之一。以下已經有幾個例子,所以我不會再添加其他例子。但是這看起來像是一個很好的項目,可以學習如何編寫可應用於每個AC,TV和洗衣機箱的功能。 – 2011-01-19 07:36:40

回答

2

您可以編寫一個函數,有兩個參數:

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; 
    } 
} 

然後在你的主要功能可以更換兩個if語句有一個:如果你想

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

首先收集所有的輸入,然後評估它們,然後可以使用瓦特和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

您的代碼仍然缺少檢查以確保輸入提取成功。 – 2011-01-19 07:16:07

0

以下解決方案爲您提供了一個功能:

  • 返回一個布爾值,說如果函數成功/失敗
  • 允許你命名應該接收值
  • 允許您設置最小值和最大值

如果需要,您可以構建其他自定義函數來獲取整數或用於獲取其他類型輸入的其他函數。這樣你可以將所有的輸入測試集中到一個地方。

#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; 
}