#include <iostream>
#include <conio.h>
int main()
{
int C; //let C be Celsius
int F; //let F be Fahrenheit
cout << "Enter the temperature in Celsius: ";
cin >> C;
cout << "Enter the temperature in Fahrenheit: ";
cin >> F;
fahrenheit = (C * 9 / 5) + 32
celsius = (F - 32) * 5 / 9
cout << "The computed Fahrenheit value from temperature in Celsius: " << fahrenheit << endl;
cout << "The computed Celsius value from temperature in Fahrenheit: " << celcius << endl;
return 0;
getch();
}
-
Read a good [C++ programming book](https://stroustrup.com/programming.html) then the documentation of your C++ compiler (e.g. [GCC](http://gcc.gnu.org/) ...) and debugger (e.g. [GDB](https://www.gnu.org/software/gdb/)...) Notice that `#include
` is non-standard. See [this C++ reference](https://en.cppreference.com/w/cpp). Enable all warnings and debug info (e.g. with GCC compiler using `g++ -Wall -Wextra -g`) – Basile Starynkevitch Oct 28 '20 at 05:52 -
2It's clear that there are gaps in what you were taught by your instructors. This is not your fault. It is also clear that this is homework. SO is not a place to get your homework done for you. "My code doesn't work" and posting just the code is not an appropriate question. Neither is one that can be fixed by simply looking up proper examples of code. – Casey Oct 28 '20 at 05:56
1 Answers
You really need to say what 'not working' means. There are many problems with this code.
But looking at the code the main problem seems to be that you are using integer division.
The value of 9/5
is 1
, not 1.8
. In C++ when you divide one integer by another the result is always another integer. Instead if you want a fraction you should use 9.0/5.0
.
In C++ is always important that you pick the correct types for your variables. Temperature is a quantity that varies continuously, so using integers for temperatures is wrong. Use float
or double
instead.
You've failed to declare two of your variables celcius
and fahrenheit
.
You are also missing semi colons at the end of some of your statements.
Standard library objects such as cin
, cout
etc. exist in the std
namespace should be referenced with qualified names, std::cin
etc.
Finally if you are going to use getch
to pause your program, it must be before your return
not afterwards.
Here's your program with these corrections.
#include <iostream>
#include <conio.h>
int main()
{
double C; //let C be Celsius
double F; //let F be Fahrenheit
std::cout << "Enter the temperature in Celsius: ";
std::cin >> C;
std::cout << "Enter the temperature in Fahrenheit: ";
std::cin >> F;
double fahrenheit = (C * 9.0 / 5.0) + 32.0;
double celsius = (F - 32.0) * 5.0 / 9.0;
std::cout << "The computed Fahrenheit value from temperature in Celsius: " << fahrenheit << std::endl;
std::cout << "The computed Celsius value from temperature in Fahrenheit: " << celcius << std::endl;
getch();
return 0;
}
As you can see there are many problems with your code. You can't program by getting things roughly correct. It has to be exactly correct or it will not work.

- 85,011
- 4
- 57
- 81
-
Your "fix" still has the underlying problem. `cout`, `cin`, and `endl` don't exist. You need to prepend them with the proper scope. – Casey Oct 28 '20 at 05:58
-