Assignment statement

Hi guys,

I just started my degree and would appreciate some help from you.

Here is one of the Exercises in my book,I am not sure if its done correct according to assignment statement or not. Many thanks

Lorraine inherited her grandmother's old cookbook, but all the oven temperatures are given in Fahrenheit,
and her oven is only calibrated in Celsius. Write a program to help her..

1
2
3
4
5
6
7
  float fahrenheit,celsius;
  cout<<"fahrenheit to celsius"<<endl;
  cout<<"Enter the temperature in Fahrenheit: ";
  cin>>fahrenheit;
  celsius=(fahrenheit-32)*5/9;
  cout<<"celsius: "<<celsius<<endl;
  return 0;
Last edited on
You need to put this code inside int main() and you need to include the required libraries for your program to run.

For this code you will need:
#include <iostream>

also, write this under your includes:

using namespace std;

some people will hate me for that but don't worry about it.

One more thing, your console may shut down quickly before you could see anything, so we have to use system("PAUSE") before return 0.

People will hate me for that but you're a beginner and I think it will do the work for now.

so it will be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){
float fahrenheit,celsius;
cout<<"fahrenheit to celsius"<<endl;
cout<<"Enter the temperature in Fahrenheit: ";
cin>>fahrenheit;
celsius=(fahrenheit-32)*5/9;
cout<<"celsius: "<<celsius<<endl;
system("PAUSE");
return 0;
}
Last edited on
You need to put this code inside

Pretty sure he has already done everything you said in your post, but just didn't show it here >.>. He is simply asking if his solution is done correctly and if it can be done better ^^

@OP

Looks good. I would avoid using namespace std though, use std:: instead. You can google that to find out why :)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
	float fahrenheit, celsius;
	std::cout << "fahrenheit to celsius" << std::endl;
	std::cout << "Enter the temperature in Fahrenheit: ";
	std::cin >> fahrenheit;
	celsius = (fahrenheit - 32) * 5.0 / 9.0;
	std::cout << "celsius: " << celsius << std::endl;
	
	return 0;
}
Last edited on
Suggestion:
test the code.

Change this line:
celsius=(fahrenheit-32)*5/9;
to
celsius=5/9*(fahrenheit-32);
and test again.
Topic archived. No new replies allowed.