FAQ: Variables - Challenge: Temperature (Part 2)

This community-built FAQ covers the “Challenge: Temperature (Part 2)” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Challenge: Temperature (Part 2)

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can I chain text after std::cin >>

2 Likes

Hi! For some reason, every time I do g++ temperature.cpp in the terminal, it says this error, g++ temperature.cpp temperature.cpp: In function ‘int main()’: temperature.cpp:10:3: error: expected ‘;’ bef
std::cin >> tempf; PLease help me thanks!

2 Likes

There is a bug or something, that any temp in F I input it gives me the same temp in C. And I didnt change anything in the code that was premade.

alin…88,

You didn’t share your code. Here’s mine:

#include <iostream>

int main() {
  
  double tempf;
  double tempc;
  
  // Ask the user
  std::cout << "Enter the termperature in Fahrenheit: ";
  std::cin >> tempf;
  
  tempc = (tempf - 32) / 1.8;
  
  std::cout << "The temp is " << tempc << " degrees Celsius.\n";
  
}
1 Like

I am having the same problem, here is my code.

#include <iostream>

int main() {
  
  double tempf;
  double tempc;
  

  
  tempc = ((tempf - 32)) / 1.8;
  
  
  std::cout << "Enter the temperature in Fahrenheit: ";
  std::cin >> tempf;
  
  std::cout << "The temp is " << tempc << " degrees Celsius.\n";
  
}

I always get -17.7778 degrees Celsius no matter the input.

Hello, @nikonino6790225288.

Welcome to the forums.

Unless we alter the control flow which you’ll learn more about as you progress, code is executed from top to bottom in our program. In your program, a value is assigned to tempc here:

So, what is the value? Well, tempf has been declared, but no value assigned: double tempf;. Therefore, we have (0 - 32) / 1.8 which equals: -17.7778.

Then, you ask for the user to input a temperature in Fahrenheit, and assign that user input to the variable: tempf. After that you don’t use that value to re-calculate the value assigned to tempc, so tempc still has -17.7778 assigned to it.

You then print your output, using the value still assigned to tempc. What would happen if you get the user input first, and then calculate the value for tempc? :wink:

2 Likes

Thanks, this was exactly what I needed.

1 Like

Hello,
I am strugling when I compile and execute the code:

#include <iostream>

int main() {
  
  double tempf;
  double tempc;
  
  // Ask the user
  std::cin>>"Enter temperature in Fahrenheit:";
  std::cin>> tempf;
  
  
  tempc = (tempf - 32) / 1.8;
  
  std::cout << "The temp is " << tempc << " degrees Celsius.\n";
  
}

The error: *^~~~~~~~*
/usr/include/c++/7/istream:120:7: note: no known conversion for argu
ment 1 from ‘const char [33]’ to ‘std::basic_istream::_istream
type& ()(std::basic_istream::istream_type&) {aka std::basic_i*
stream& ()(std::basic_istream&)}’*
/usr/include/c++/7/istream:124:7: note: candidate: std::basic_istream<
_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>:
:operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& ()(std::*
basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Tr
*aits = std::char_traits; std::basic_istream<_CharT, _Traits>::
*
istream_type = std::basic_istream; std::basic_istream<_CharT, _T
raits>::__ios_type = std::basic_ios]

  •   operator>>(__ios_type& (*__pf)(__ios_type&))*
    
  •   ^~~~~~~~*
    

/usr/include/c++/7/istream:124:7: note: no known conversion for argu
ment 1 from ‘const char [33]’ to ‘std::basic_istream::__ios_type
& ()(std::basic_istream::__ios_type&) {aka std::basic_ios*
& ()(std::basic_ios&)}’*
/usr/include/c++/7/istream:131:7: note: candidate: std::basic_istream<
_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>:
:operator>>(std::ios_base& ()(std::ios_base&)) [with _CharT = char; _*
Traits = std::char_traits; std::basic_istream<_CharT, _Traits>::
__istream_type = std::basic_istream]

  •   operator>>(ios_base& (*__pf)(ios_base&))*
    
  •   ^~~~~~~~*
    

/usr/include/c++/7/istream:131:7: note: no known conversion for argu
ment 1 from ‘const char [33]’ to ‘std::ios_base& ()(std::ios_base&)’*
/usr/include/c++/7/istream:214:7: note: candidate: std::basic_istream<
_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>:
:operator>>(float&) [with _CharT = char; _Traits = std::char_traits<ch
ar>; std::basic_istream<_CharT, _Traits>::_istream_type = std::basic
istream]
:anguished: Any suggestion?

Scroll up and read your first error message

Or for that matter tell g++ to only print out one error.

You should probably be adding -Wall (turn on all warnings) and -Werror (warnings are now errors) as well.

Better yet, get your editor to run g++ or clang++ or whatever as you type:

I wrote:
std::cout << “Enter the temperature in Fahrenheit: \n”;
std::cin >> tempf >> “\n”;
And something went wrong, but I don’t know why.
The correct is only:
std::cout << "Enter the temperature in Fahrenheit: ";
std::cin >> tempf;
I thought I had to use the “\n” at the end of each phrase.

Not sure if it is a bug or it is the compiler, or maybe this is how all C++ code is, but

When I write std::cout << “something " << var << " else.\n”;

If I miss a dot in between the else\n without deleting the entire line back to the missed dot, and just skip to it and enter the dot that I missed, the compiler fails to see the code is correct. I am wondering if this is a Codecademy thing, because the compiler fails to understand I have corrected the mistake, or do all compilers act like this, and I will get errors in my code if I skip to mistakes instead of deleting the entire line back to the mistake.

I honestly hope it is a codecadamy thing,

I used a word as an input for the tempf. I somehow got a number as an output. How is this possible?

hahahaha this had me dying.

What does that mean?

I think mine is a full bug exercises because in part one temperature is compiler shows files does not exist why does it shows this message in exercise block letters it shows the same message I again had to rewrite the whole file which took about 2 hours . Why does the compiler says temperature.cpp does not exist??? :unamused: :unamused: :unamused:

how does std::cin >> tempf now which input from the user to store if it is multiple

There seems to be an error in compiling, I have cross checked that my code is correct with the given solution and here’s what my terminal looks like:

g++ temperature.cpp
g+±7: error: temperature.cpp: No such file or directory
g+±7: fatal error: no input files compilation terminated

1 Like

Hi, every time i try to compile this it tells me temperature.cpp doesn’t exist. can someone explain?

1 Like

2.07322e-317 in bash
everything works fine but this pops up after compiling and executing temperature.cpp