Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why compilation is ok but exceed runtime?? thanks

+5 votes
asked Apr 12 by Sebastian Bartela (170 points)
#include <iostream>

using namespace std;

int main()
{
    int f;
    cout << "Temperatura w skali Fahrenheita: ";
    cin >> f;
    cout << f << " stopni F to " << (f-32)*5/9;
    cout << " stopni C" << endl;
    return 0;
}

2 Answers

+2 votes
answered Apr 13 by Peter Minarik (101,340 points)
This runs fine for me.

Sometimes there could be interruptions to the Online GDB service, and then you'd have no output. You can try to run again and that usually solves the issue. Sometimes reloading the page helps (but save your work before!!!).
+1 vote
answered Apr 15 by Aditya Dwivedi (220 points)
The problem with your code is in this line:

(f-32)*5/9

Since f is declared as int, the expression (f-32)*5/9 performs integer division. In C++, when you divide two integers, the result is also an integer (it truncates the decimal part). So 5/9 = 0 (not 0.555...).

This means your Celsius value will always be 0, regardless of the Fahrenheit input!

Fix: Use floating-point division by making at least one operand a double/float:

(f-32)*5.0/9.0

Or better yet, change your variable to double and use proper formatting:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double f;
    cout << "Temperatura w skali Fahrenheita: ";
    cin >> f;
    cout << fixed << setprecision(2);
    cout << f << " stopni F to " << (f-32)*5.0/9.0 << " stopni C" << endl;
    return 0;
}

This way the division will work correctly and give accurate Celsius values!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...