if c语句

时间:2011-01-19 06:43:05

标签: c++ visual-c++

我的程序到目前为止,我的问题是我必须在每个cout / cin代码之后包含if语句,还是有一种方法来概括它? :

#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 个答案:

答案 0 :(得分: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;
   }
}

然后在您的main函数中,您可以将两个if语句替换为:

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

如果您想先收集所有输入然后评估它们,那么可以使用一个数组作为瓦特和hours_per_day。然后,您可以遍历数组并检查每个条目。

答案 1 :(得分: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添加另一个参数作为变量名称(例如“瓦特”)。

答案 2 :(得分: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;
}