Write a console program using Microsoft Visual C++ to calculate how many Btu\'s
ID: 3649093 • Letter: W
Question
Write a console program using Microsoft Visual C++ to calculate how many Btu's of heat are delivered to a house given the number of gallons of oil burned and the efficiency of the house's oil furnace. Assume that a barrel of oil (42 gallons) has an energy equivalent of 5,800,000 Btu. Use a loop in this program to allow the user to run this calculation multiple times before exiting. The program should exit only when the user enters the "#" character. For one test, use an efficiency of 65% and 100 gallons of oil.Explanation / Answer
#include <iostream>
#include <iomanip> //Need this for formatting
using namespace std;
const double BTU_PER_GALLON = 138095.2381; //Assumed from your 5.8 MBTU for 42 Gallons
int main()
{
string input = ""; //will hold input in string format
do //do at least one time
{
cout << "Input number of gallons: "; //ask for input
cin >> input; //get input
if(input == "#") //if input is equal to ending string
break; //break away from loop
try //enter try statement
{
double convertedValue = atof(input.c_str()); //try to convert string to double
double calculated = convertedValue * BTU_PER_GALLON; //perform calculation
cout << "Calculated BTUs: " << fixed << setprecision(2) << calculated << " BTU" << endl; //Output calculated answer with format
}
catch(exception ex) //If try statement fails
{
cout << "Invalid input, please only input numbers" << endl; //report invalid input
}
}while(input != "#"); //Run loop if input is not a #
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.