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_开发者_如何学JAVAwh= .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;
}
I guess you could do the following with triple variables:
#include <iostream>
using namespace std;
int main ()
{
double watts_ac, watts_tv, watts_wa;
double hours_per_day_ac, hours_per_day_tv, hours_per_day_wa;
double watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts_ac;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day_ac;
cout << "How many Watts for the Television? " ;
cin >> watts_tv;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day_tv;
cout << "How many Watts for the Washer? " ;
cin >> watts_wa;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day_wa;
watt_hours = (watts_ac * hours_per_day_ac + watts_tv * hours_per_day_tv + watts_wa * hours_per_day_wa) * 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}
or just aggregate data:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day_ac, 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;
watts_hours = watts*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;
watts_hours += watts*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;
watts_hours += watts*hours_per_day;
watt_hours *= 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}
精彩评论