About fixing the error

Hi, I just began to learn C++. I wonder what’s wrong with the following code:
It is the optional exercise in here:
https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-conditionals-and-logic/exercises/review

#include <iostream>

int main() {

  

double earthWeight;

std::cout<<"Your Weight in Earth is? \n";

std::cin>>earthWeight;

string your_planet;

std::cout<<"Which planet do you want to go to? \n";

std::cin>>your_planet;

double planetWeight;

if (your_planet=='Mercury'){

  planetWeight=0.38*earthWeight;

  std::cout<<"Your weight in Mercury is "<<planetWeight<<"\n";

}  

                

}

A couple things here. First you have your_planet defined as a string. However string is part of the std namespace so that needs to be included (the same is with std::cout).

Second is your if. In C/C++ there is a bit of a difference between ' and ".
Single quotes are for a variable of type char or a single character, while double quotes are either a char* in C or a std::string in C++. I’m gonna guess you want to use double quotes in your code.

1 Like

How to include std namespace ? What did you mean when you said:" the same is with std::cout?

When you use cout you include the namespace its a part of std::cout the same is done with string, std::string.

This can be avoided by using the line using namespace std, but this is typically considered bad practice as it clutters up the local namespace. Depending on were you are, you’ll learn more about namespaces a little later.