Quick question about using float...

In my intro C++ class we were assigned to write this simple function program that states the weather and gives you the averages for each day and the week.

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>
using namespace std;

int main() {
    int tue_hi = 86;
    int tue_lo = 68;
    int wed_hi = 86;
    int wed_lo = 61;
    int thr_hi = 74;
    int thr_lo = 58;
    int fri_hi = 76;
    int fri_lo = 59;
    int sat_hi = 77;
    int sat_lo = 61;

    int tue_av = (tue_lo + tue_hi)/2;
    int wed_av = (wed_lo + wed_hi)/2;
    int thr_av = (thr_lo + thr_hi)/2;
    int fri_av = (fri_lo + fri_hi)/2;
    int sat_av = (sat_lo + sat_hi)/2;

    int wk_lo = (tue_lo + wed_lo + thr_lo + fri_lo + sat_lo)/5;
    int wk_hi = (tue_hi + wed_hi + thr_hi + fri_lo + sat_lo)/5;

cout << "The predicted low tempterature value for Tuesday is " <<tue_lo << endl;
cout << "The predicted high tempterature value for Tuesday is " <<tue_hi << endl;
cout << "The predicted average temperature value for Tuesday is " <<tue_av << endl;

cout << "\nThe predicted low tempterature value for Wednesday is " <<wed_lo << endl;
cout << "The predicted high tempterature value for Wednesday is " <<wed_hi << endl;
cout << "The predicted average temperature value for Wednesday is " <<wed_av <<endl;

cout << "\nThe predicted low tempterature value for Thursday is " <<thr_lo << endl;
cout << "The predicted high tempterature value for Thursday is " <<thr_hi << endl;
cout << "The predicted average temperature value for Thursday is " <<thr_av <<endl;

cout << "\nThe predicted low tempterature value for Friday is " <<fri_lo << endl;
cout << "The predicted high tempterature value for Friday is " <<fri_hi << endl;
cout << "The predicted average temperature value for Friday is " <<fri_av <<endl;

cout << "\nThe predicted low tempterature value for Saturday is " <<sat_lo << endl;
cout << "The predicted high tempterature value for Saturday is " <<sat_hi << endl;
cout << "The predicted average temperature value for Saturday is " <<sat_av <<endl;

cout << "\nThe average low temperature for the week is " <<wk_lo << endl;
cout << "The average high temperature for the week is " <<wk_hi << endl;
}



Now the main problem is that I need decimal places to show up. On the paper we were given it says that the predicted average temperature for Wednesday should be 73.5

I tried replacing int with float. and then used (float). Nothing seems to be working. Does anybody have any ideas on what I'm doing wrong. I don't think I even understand Float fully. I googled around with no prevail!

Thanks so much in advanced!
An int plus an int will give you back an int.

wed_lo + wed_hi will give you the integer value 147.

An int divided by an int will give you an int.

(wed_lo + wed_hi)/2; will give you the value 73.

Changing that after the calculation to a float will not magically get back the lost precision, and you'll just have a float with value 73.

You need to be operating on float values from the start. Changing all your int types to float will do it.

Here's a start, to demonstrate:

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
#include <iostream>
using namespace std;
 
int main() {
    int tue_hi = 86;
    int tue_lo = 68;
    float wed_hi = 86;
    float wed_lo = 61;
    int thr_hi = 74;
    int thr_lo = 58;
    int fri_hi = 76;
    int fri_lo = 59;
    int sat_hi = 77;
    int sat_lo = 61;
 
    int tue_av = (tue_lo + tue_hi)/2;
    float wed_av = (wed_lo + wed_hi)/2;
    int thr_av = (thr_lo + thr_hi)/2;
    int fri_av = (fri_lo + fri_hi)/2;
    int sat_av = (sat_lo + sat_hi)/2;
 
    int wk_lo = (tue_lo + wed_lo + thr_lo + fri_lo + sat_lo)/5;
    int wk_hi = (tue_hi + wed_hi + thr_hi + fri_lo + sat_lo)/5;
 
cout << "The predicted low tempterature value for Tuesday is " <<tue_lo << endl;
cout << "The predicted high tempterature value for Tuesday is " <<tue_hi << endl;
cout << "The predicted average temperature value for Tuesday is " <<tue_av << endl;
 
cout << "\nThe predicted low tempterature value for Wednesday is " <<wed_lo << endl;
cout << "The predicted high tempterature value for Wednesday is " <<wed_hi << endl;
cout << "The predicted average temperature value for Wednesday is " <<wed_av <<endl;
 
cout << "\nThe predicted low tempterature value for Thursday is " <<thr_lo << endl;
cout << "The predicted high tempterature value for Thursday is " <<thr_hi << endl;
cout << "The predicted average temperature value for Thursday is " <<thr_av <<endl;
 
cout << "\nThe predicted low tempterature value for Friday is " <<fri_lo << endl;
cout << "The predicted high tempterature value for Friday is " <<fri_hi << endl;
cout << "The predicted average temperature value for Friday is " <<fri_av <<endl;
 
cout << "\nThe predicted low tempterature value for Saturday is " <<sat_lo << endl;
cout << "The predicted high tempterature value for Saturday is " <<sat_hi << endl;
cout << "The predicted average temperature value for Saturday is " <<sat_av <<endl;
 
cout << "\nThe average low temperature for the week is " <<wk_lo << endl;
cout << "The average high temperature for the week is " <<wk_hi << endl;
return 0;
}
Last edited on
That's very strange because I swear I tried that. I replaced ever int with float and it wasn't working. I must have done something wrong because upon doing that it works perfectly.

Thanks!
If the input values (temperatures) are supposed to be of type int and the results of type float (ie. real numbers) than you have to perform real division instead of integer division:

1. real division
float tue_av = (tue_lo + tue_hi)/2.0f;

2. integer division
int tue_av = (tue_lo + tue_hi)/2;

note: The result of integer division is always an int (ie. number without decimal part), e.g. 5/2=2.
Topic archived. No new replies allowed.