FAQ: Variables - Challenge: Temperature (Part 1)

This is the solution, so there is no reason that should fail.

#include <iostream>

int main() {
  
  double tempf = 83.0;
  double tempc;
  
  tempc = (tempf - 32) / 1.8;
  
  std::cout << "The temp is " << tempc << " degrees Celsius.\n";
  
}
  double tempc;
  double tempK = tempc - 273.15;

Something is rather amiss, here.

No, I just skipped it. I compiled locally and it was fine, so moving on and making a note here. I suspect its in there test coverage.

Hello, @psmilliorn.

Check the spelling of ‘degrees’ in your code (line 6). :wink:

2 Likes

Degress lol. Ok, I concede.

1 Like

For this project, I know it asks you to declare the variable tempc before setting tempc= (tempf-32)/1.8, but in real life, you could just do double tempc= (tempf-32)/1.8; right? Or do you first have to declare this variable and then later initialize it?

Hello, @chip2439329534.

Welcome to the forums.

Yes. You could declare and initialize the variable on a single line.

1 Like

hello!
please i would like to know how this commands work, like for the temperature conversion, “double” was written. my question is that is it necessary to write to the double and also whats the command use?

The codecademy has an article you can review on data types. This explains why you must include double in the variable declaration.

For details on double, take a look at the description of double on this page:
https://docs.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=vs-2019

hello there, i know this is late but i think there are still people who are looking for the solution. i tried naming the compiled file “g++ temperature.cpp -o temperature” then execute it and it did the work for me, i hope this help.

SOLVED: The solution is very specific (as it should be, I suppose). Make sure you are not outputting your own file name when compiling the code. For example:
$ g++ temperature.cpp -o temperature //correct
not
$g++ temperature.cpp -o temp //incorrect


#include <iostream>

int main() {

  double tempf = 80.0;

  double tempc;

  tempc = (tempf-32)/1.8;

  std::cout << "The temp is " << tempc << " degrees Celsius.\n";

}
/*
Error: Did you type the commands correctly?
Every step is green with the exception to the Compile and Execute the Program in which I typed in the terminal:
*/
$g++ temperature.cpp -o temp
$./temp
The temp is 26.6667 degrees Celsius.

Does anyone know why this isn’t working?
#include

int main() {

double tempf = 50;

double tempc = (tempf-32)/1.8;

std::cout << “The temp is " << tempc << " degrees Celsius. /n”;

}
Bash:
$ g++ temperature.cpp
temperature.cpp: In function ‘int main()’ :
temperature.cpp:13:1: error: expected ‘;’ before ‘}’ token
}
^

It actually says
#include
not just
#include

Figured it out, it hadn’t been a complete template

#include

int main() {

int tempf = 43;
int tempc;

tempc = (tempf - 32)/1.8;

std::cout << “The temp is " << tempc << " degrees Celsius.\n”;

}

When I removed “double” it ran successfully. Why do we have to use double? When I use double this error pops up:
“”" temperature.cpp:5:14: error: two or more data types in declaration of ‘tempf’
int double tempf = 43; “”"

Can anyone explain?

EDIT:: I FOUND OUT THE REASON

I was using two types i.e. int and double. Should’ve just used double tempf or double tempc. Figured it out lol.

3 posts were split to a new topic: Dont know why but the compiler is crazy

I’m actually here to brag about my solution. I have been writing in C++ at a beginner lever for about a year now. I love the function libraries and suggest anyone who is trying to learn it be interested also. So originally when I printed out my result I would get 19.7777 or something like that. But I added the rounding function to make my Celsius nice and neat.

#include <iostream>
#include <cmath>
int main() {
  double tempf = 67;
  double tempc;
  tempc = (tempf - 32) / 1.8;
  std::cout << "The temp is " << round(tempc) << " degrees Celsius.\n"; 
  }

gives a nice result of a solid 19c!

1 Like


I don’t know what wrong with my code

this lesson is broken. I do everything the lesson tells me to do. The part where it tells you to google the actual temp of New York, doesn’t work. if i do the calculation from the hint the terminal accepts it and lets me move on. this should be fixed as its making someone think they have to use real life temps.

Hello !

Could please explain me whats the difference between,

tempc = (tempf-32)/1.8; and tempc = (tempf - 32) / 1.8; - Why are we using spaces while writing something ? is it some healthy habit to have or its some meaning behind it ?