This is waht i came up with for this question. Someone mentioned that i am calculating the average before i've read any values in. What am i doing here wrong im confused
The heating system in a school should be switched on if the average temperature is less than 17 degrees celcius. The average temperature is found from the temperatures in the art english and music departments. You are required to write a C++ program that allows the user to input 3 temperatures. The program calculates and displays the average temperature and then displays "heating should be on " or " heating should be off" as appropriate.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int tempArt;
int tempEnglish;
int tempMusic;
double avg = (tempArt + tempEnglish + tempMusic) / 3.0;
do
{
cout<<"Please enter Art Temperature and press Enter.";
cin >> tempArt;
cout << "Please enter English Temperature and press Enter.";
cin >> tempEnglish;
cout << "Please enter Music Temperature and press Enter.";
cin >> tempMusic;
if(avg <= 17)
{
cout << "heating should be on: " << avg << endl;
}
elseif (avg >= 17)
{
cout << "heating should be off: " << avg<< endl;
}
cout << "Do you want to continue enter Y or N to stop: ";
cin >> yesno;
yesno = toupper(yesno);
}while (yesno == 'Y');
// system ("pause");
return 0;
}
C++ is not like a spreadsheet, where you write a formula on a cell and the value of the cell will update whenever you update values in the cells that the formula references.
You do set the value of 'avg' on line 12. The value of that variable will not be changed after that.
However, on line 12 you don't yet have the temperature values, so the value stored in 'avg' is undetermined.
You do have the necessary values for calculating average after line 24.
You do need to have the average calculated before line 28.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int tempArt;
int tempEnglish;
int tempMusic;
double avg;
do
{
cout<<"Please enter Art Temperature and press Enter.";
cin >> tempArt;
cout << "Please enter English Temperature and press Enter.";
cin >> tempEnglish;
cout << "Please enter Music Temperature and press Enter.";
cin >> tempMusic;
avg = (tempArt + tempEnglish + tempMusic) / 3.0;
if(avg <= 17)
{
cout << "heating should be on: " << avg << endl;
}
elseif (avg >= 17)
{
cout << "heating should be off: " << avg<< endl;
}
// system ("pause");
return 0;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int tempArt;
int tempEnglish;
int tempMusic;
int yesno;
double avg;
do
{
cout<<"Please enter Art Temperature and press Enter.";
cin >> tempArt;
cout << "Please enter English Temperature and press Enter.";
cin >> tempEnglish;
cout << "Please enter Music Temperature and press Enter.";
cin >> tempMusic;
avg = (tempArt + tempEnglish + tempMusic) / 3.0;
if(avg <= 17)
{
cout << "heating should be on: " << avg << endl;
}
elseif (avg >= 17)
{
cout << "heating should be off: " << avg<< endl;
}
cout << "Do you want to continue enter Y or N to stop: ";
cin >> yesno;
yesno = toupper(yesno);
}while (yesno == 'Y');
// system ("pause");
return 0;
}