This is what i came up with

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include <iostream>
#include <cmath>
using namespace 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;
        }

        else if (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.


PS. I predict a compiler error on line 39.
like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #include <iostream>
#include <cmath>
using namespace 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;
        }

        else if (avg >= 17)
        {
            cout << "heating should be off: " << avg<< endl;
        }


    // system ("pause");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

 #include <iostream>
#include <cmath>
using namespace 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;
        }

        else if (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;
}
Close. Are you sure that the yesno must be an integer? Aren't Y and N characters?
you are right
Topic archived. No new replies allowed.