I need help with type casting

How can I made the answer display 18 when I enter 0 for fahrenheit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float fahrenheit;
    float celsius;

    cout << "Enter Fahrenheit: ";
    cin >> fahrenheit;

    celsius = 5.0 / 9 * (fahrenheit - 32);

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(0);

    cout << "Celsius: " << static_cast<int>(celsius) << endl;
    return 0;
Last edited on
0 degrees fahrenheit in Celsius is -17.778 isn't it?

closed account (48T7M4Gy)
try http://www.cplusplus.com/reference/cmath/round/
> How can I made the answer display 18 when I enter 0 for fahrenheit

You do mean -18 (rounded to the nearest whole number), don't you?
We can let the stream (std::cout) do that for us.

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
#include <iostream>

// using namespace std; // avoid

int main()
{
    // float fahrenheit;
    // float celsius;

    double fahrenheit; // strongly favour double over float or long double
    double celsius;

    std::cout << "Enter Fahrenheit: ";
    std::cin >> fahrenheit;

    celsius = 5.0 / 9 * (fahrenheit - 32);

    std::cout.setf(std::ios::fixed);
    // cout.setf(ios::showpoint);
    std::cout << std::noshowpoint ; // we do not want the decimal point to be printed
    std::cout.precision(0);

    // cout << "Celsius: " << static_cast<int>(celsius) << endl;
    std::cout << "Celsius: " << celsius << '\n' ;

    // return 0; // implicit
}
Thank you so much all of you. That is just what I needed. I'm super grateful. I solved it. JLBorges that is what I wanted to do. I did it. Thanks.
Topic archived. No new replies allowed.