开发者

if statements for c++

开发者 https://www.devze.com 2023-02-05 05:02 出处:网络
My program so far, my question is do i have to include the if statements after each cout/cin code or is there a way to generalize it? :

My program so far, my question is do i have to include the if statements after each cout/cin code or is there a way to generalize it? :

#include <iostream>
using namespace std;

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

   dollars_per_wh= .00008;

   cout << " H开发者_如何学运维ow 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 ;
}


You can write a function that takes two parameters:

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

Then in your main function you can replace the two if statements with one:

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

If you want to collect all the inputs first and then evaluate them, then maybe use an array for watts and hours_per_day. Then you can run through the array and check each entry.


Yes, you can pull them out into a separate function:

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

And if you want to be more specific about the error message (e.g. "negative watts" instead of "negative value") you can add another parameter to cinNonNegative for the name of the variable (e.g. "watts").


The following solution gives you a function that:

  • Returns a boolean that says if the function succeeded/failed
  • Allows you to name the value that should be received
  • Allows you to set minimal and maximal values

If needed, you can build other custom functions for getting integers, or other functions for getting any other kind of input. This way you can concentrate all the input tests into a single place.

#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

精彩评论

暂无评论...
验证码 换一张
取 消