Question about adding three C ++ variables

advertisements

Hey so im making a program to figure out a energy cost my program so far is, but I want the watt-hours to be for all three appliances not just one how do i do that?:

#include <iostream>
using namespace std;

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

   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;

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

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

   watt_hours = watts * hours_per_day * 30;
   cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;


You will have to use a stand-alone watts variable and hours_per_day for each appliance.

Also consider using loops for doing such, it makes code more clearer and less sized;

#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::cin;
using std::endl;

static const double dollars_per_wh = 0.00008;

int main(int argc, char ** argv)
{
    int num_apps = 3;
    std::vector<std::string> v_apps;
    v_apps.push_back("Air conditioner");
    v_apps.push_back("Television");
    v_apps.push_back("Washer");

    double watt, hours, watt_hours = 0;
    for(register int i = 0; i < num_apps; ++i)
    {
        cout << "How many watts for the " << v_apps[i].c_str() << "? ";
        cin >> watt;
        cout << "How many hours/day do you run the " << v_apps[i].c_str() << "? ";
        cin >> hours;
        watt_hours = watt_hours + ((watt * hours) * 30);
    }
    cout << "You have used " << watt_hours << " Watt-Hours of electricity." << endl;
    return 0;
}