# FAQ: Conditionals & Logic - Review

Hello, @casperdev.

Welcome to the forums.

In your calculations to determine weight on other planets, you are multiplying the number corresponding to the planet selection by the conversion factor rather than the user’s earth weight.

Ahh, i didn’t realize that!

So just to make sure i understand what you mean, is this the way it should be?

``````#include <iostream>

int main() {

double earth;
int planet;

std::cin >> earth;

std::cout << "1.Venus 2.Mars 3.Jupiter 4.Saturn 5.Uranus 6.Neptune\n";
std::cin >> planet;

switch (planet) {
case 1:
planet = earth * 0.78;
break;
case 2:
planet = earth * 0.39;
break;
case 3:
planet = earth * 2.65;
break;
case 4:
planet = earth * 1.17;
break;
case 5:
planet = earth * 1.05;
break;
case 6:
planet = earth * 1.23;
break;
default:
std::cout << "Invalid\n";
break;

}

std::cout << "Your weight will be: " << earth << "\n";

}
``````

No. `planet` is the variable you’re using to determine the user’s choice. `earth` initially holds the user’s weight, so you could either reassign the relative weight for the chosen planet to `earth`, or use another variable altogether if you had a reason to retain the original weight. In this instance, I would probably opt for the first option, and reassign the relative weight to `earth` like so:

``````earth = earth * 0.78;
// or simply:
earth *= 0.78;
``````

Now the user’s original weight is multiplied by the conversion factor for the planet, and will be printed at the end of the program.

2 Likes

Thank you midlindner!

I understand it now after your explanation i was slightly confused at first

1 Like

Here is my code. It comes with an error that says “error: expected declaration before ‘}’ token” Does anyone know what that means?

``````int main() {

double weight;
int x;

std::cout << "What is your weight on earth?\n";
std::cin >> weight;
std::cout << "Enter you number for the planet:\n";
std::cin >> x;

switch (x) {
case 1:
std::cout<< weight == weight*.78;
break;
case 2:
std::cout<< weight == weight*.39;
break;
case 3:
std::cout<< weight == weight*2.65;
break;
case 4:
std::cout<< weight == weight *1.17;
break;
case 5:
std::cout<< weight == weight * 1.05;
break;
case 6:
std::cout<< weight == weight*1.23;
break;
default:
std::cout<<"Invalid\n";
break;
}

std::cout << "\nYour weight: "<< weight << "\n";

}
``````

Hey guys!

First time coding, sorry if this code is silly, but I’m basically stuck.

I’m not getting any errors from the terminal, and no matter how much I mess with this I can’t seem to make any headway, not even the hints are helping anymore!

Here’s the code:

``````int main() {

double weight;
int planet;

std::cin >> weight;

std::cout << "I have information for the following planets: ";
std::cout << " 1.Venus \n 2.Mars \n 3.Jupiter\n";
std::cout << "4.Saturn \n 5.Uranus \n 6.Neptune\n";

std::cout << "\n\n Which planet are you visiting? ";
std::cin >> planet;

if (planet == 1) {

weight = weight * 0.78;

} else if (planet == 2)

{

weight = weight * 0.39;

} else if (planet == 3) {

weight = weight * 2.65;

} else if (planet == 4) {

weight = weight * 1.17;

} else if (planet == 5) {

weight = weight * 1.05;

} else (planet ==6)
{

weight = weight * 1.23;

}

std::cout << "\nYour weight: " << weight << "\n"

}
``````

My problem is that when the code reaches the point of asking “Which planet are you visiting?”, no matter what number I input I get zero response. The shell just stops. Any assistance would be appreciated

What line is the error being reported? My guess at this time is that you need to add a 0 in front of the decimal point. .39 to 0.39.

`#include <iostream>`

Hello, @kartikgulia527945645.

Welcome to the forums.

You can get your code to compile by placing the statements you are attempting to print with `std::cout` inside of parenthesis `( )`. You will then find that your code is not producing the desired results. Consider the difference between the comparison operator `==` and the assignment operator `=`.

``````std::cout << (weight == weight * .78); //output 0 meaning false
``````

You have these errors in your script.

``````./main.cpp: In function ‘int main()’:
./main.cpp:43:1: error: expected ‘;’ before ‘{’ token
{
^
./main.cpp:51:1: error: expected ‘;’ before ‘}’ token
}``````
1 Like

Hello, @arcslayer80621.

Your code as is doesn’t compile for me. Once I fixed the errors, and compiled successfully, it appears to produce expected results.

An `else` statement cannot test a condition:

Something appears to be missing here:

2 Likes

Thank you!

I ended up just scrapping it all yesterday and opted for using switch and cases. When I went through and rewrote it all I must’ve ended up fixing those hiccups.
Regardless, cheers for the corrections

1 Like

It doesn’t say what line is wrong, just says that is the before the } which is found out is the second to last one. The #include is there but I just didn’t include it when I copy and pasted this. I added the 0 before the decimals and () but its doesn’t help and i tried == vs = but it doesn’t change anything. Here is everything I have but i still don’t know why it isn’t working.\

``````#include <iostream>

int main() {

double weight;
int x;

std::cout << "What is your weight on earth?\n";
std::cin >> weight;
std::cout << "Enter you number for the planet:\n";
std::cin >> x;

switch (x) {
case 1:
std::cout << (weight == weight * 0.78);
break;
case 2:
std::cout << (weight == weight * 0.39);
break;
case 3:
std::cout << (weight == weight * 2.65);
break;
case 4:
std::cout << (weight == weight * 1.17);
break;
case 5:
std::cout << (weight == weight * 1.05);
break;
case 6:
std::cout << (weight == weight * 1.23);
break;
default:
std::cout << "Invalid\n";
break;
}

std::cout << "\nYour weight: "<< weight << "\n";

}
``````

When I run your code I get the following output:

0

The `200` is the original weight I put in.
My original comment referenced above is the reason for the `0`. Instead of assigning the new weight to the original weight variable `weight = weight * 0.78`, your code is comparing the original value of `weight` to see if it is equal to the original value multiplied by, in this case, 0.78. Obviously it isn’t equal, so `false` is returned. C++ uses `0` for false and `1` for true.

Since the original value input by the user is still assigned to `weight`, the last line of your code prints the user input value.

One last suggestion. Since you are printing the converted `weight` at the end, I would consider changing these statements:

``````std::cout << (weight = weight * 0.78); //correct use of assignment operator instead of comparison operator
``````

so that they simply make the reassignment to the `weight` variable, and then `break;`

``````case 1:
weight = weight * 0.78;
break;
//or
weight *= 0.78;
break;
``````
1 Like

hola
cuando intente ejecutar mi codigo me salio un error pero no entiendo bien cual es el error apreciaria mucho si alguien me ayuda porfavor :orar: :orar:

este es mi codigo:

int main() {
using namespace std;

int n;
double peso;
cout<< "introdusca su peso terrestre: ";
cin>>peso>> “\n”;
cout<< “cual planeta quiere visitar?\n 1.Venus 2.Marte 3.Jupiter \n\n 4.Saturno 5.Urano 6.Neptuno \n”;
cout<< "coloque un numero: ";
cin>>n;

switch (n) {

``````case 1:
peso=peso*0.78;
break;

case 2:
peso=peso*0.39;
break;

case 3:
peso=peso*2.65;
break;

case 4:
peso=peso*1.17;
break;

case 5:
peso=peso*1.05;
break;

default:
peso=peso*1.23;
break;
``````

}

cout<< "este es tu peso estimado: ";
cin>>peso;

}

diculpen todo los comandos estan en ingles pero el traductor los puso en español de igual manera estan escritos en ingles y me aparece el error

El código es solo en inglés.

#include

int main() {

double weight;
int planet;

std::cout << "What is your weight on Earth?: ";
std::cin >> weight;

std::cout << “\nI have information for the folowing planets:\n\n”;
std::cout << " 1. Venus 2. Mars 3. Jupiter\n";
std::cout << " 4. Saturn 5. Uranus. 6. Neptune\n";

//ask what planet to do calculations;

std::cout << "what planet you want to fight on: ";
std::cin >> planet;

switch(planet) {
case 1 :
weight = (weight * 0.78);
std::cout << "You will be: " << weight << “lbs. on panet Venus\n”;
break;

case 2 :
weight = (weight * 0.39);
std::cout << "You will be: " << weight << “lbs. on planet Mars\n”;
break;

case 3 :
weight = (weight * 2.65);
std::cout << "You will be: " << weight << “lbs. on planet Jupiter\n”;
break;

case 4 :
weight = (weight * 1.17);
std::cout << "You will be: " << weight << “lbs. on planet Staurn\n”;
break;

case 5 :
weight = (weight * 1.05);
std::cout << "You will be: " << weight << “lbs. on planet Uranus\n”;
break;

case 6 :
weight = (weight * 1.23);
std::cout << "You will be: " << weight << “lbs. on Planet Neptune\n”;
break;

default :
std::cout << “Your are lost \n”;
break;

}

}

Hello, @course1411091525, and welcome to the forums.

Do you have a question? Are you looking for feedback? Did you post a solution to an exercise just to post a solution? Posting of solutions without any explanation is not typically permitted.

hey, i was going through zour codes, really amazing by the way!
so i was wondering what is the meaning of the “rg” you wrote?