FAQ: Conditionals & Logic - Review

Thank you for the reply and help! I will make these changes and keep them in mind for the future.

This took me a while to do i was stuck when i instantly started it so i look at the forums to get a concept and was progressing well. But where i was mainly stuck was trying to get the output i wanted the code would go through but the output was completely wrong. Out of desperation i used chat gpt and put my code there to see what i did wrong and it seems i initialized the wrong variable and forgot the closing bracket after the return statement for default. For future reference what can i do to analyze my mistakes better and how do i know which variable to initialize in the switch statement.
Here’s my code:
#include

int main() {
//Asking for user input
int weight;
std::cout << “How much do you weigh on earth?\n”;
std::cin >> weight;
//Select a Planet
int planet;
std::cout << “Pick a planet 1-7\n”;
std::cout << “1.Mercury\n”;
std::cout << “2.Venus\n”;
std::cout << “3.Mars\n”;
std::cout << “4.Jupiter\n”;
std::cout << “5.Saturn\n”;
std::cout << “6.Uranus\n”;
std::cout << “7.Neptune\n”;
std::cin >> planet;
//Switch statement
int planetWeight;
switch(planet){
case 1 :
planetWeight = weight * 0.38;
std::cout << “Mercury\n”;
break;
case 2 :
planetWeight = weight * 0.91;
std::cout << “Venus\n”;
break;
case 3 :
planetWeight = weight * 0.38;
std::cout << “Mars\n”;
break;
case 4 :
planetWeight = weight * 2.34;
std::cout << “Jupiter\n”;
break;
case 5 :
planetWeight = weight * 1.06;
std::cout << “Saturn\n”;
break;
case 6 :
planetWeight = weight * 0.92;
std::cout << “Uranus\n”;
break;
case 7 :
planetWeight = weight * 1.19;
std::cout << “Neptune\n”;
break;
default :
std::cout << “Unknown\n”;
return 0;

}
//Output of the planet weight
std::cout << “You weigh " << planetWeight << " pounds on this planet.”;

return 0;

}

// I found this was a really fun way to write the program using switch. I feel like adding little outputs in there to interact with a user made the code feel more fun when it executed.

#include

int main() {

double weight ;
int x;

std::cout << “Please enter weight:”;
std::cin >> weight;

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

std::cout << “Please enter a number for the planet you are fighting on:”;
std::cin >> x;

switch(x){
case 1:
std::cout << "Mercury Weight is "<< (weight*.38) << “\n”;
break;
case 2:
std::cout << "Venus Weight is "<< (weight*.91) << “\n”;
break;
case 3:
std::cout << "Mars Weight is "<< (weight*.38) << “\n”;
break;
case 4:
std::cout << "Jupiter Weight is "<< (weight2.34) << “\n”;
break;
case 5:
std::cout << "Saturn Weight is "<< (weight
1.06) << “\n”;
break;
case 6:
std::cout << "Uranus Weight is "<< (weight*.92) << “\n”;
break;
case 7:
std::cout << "Neptune Weight is "<< (weight*.38) << “\n”;
break;
default :
std::cout << “Unknown\n”;
break;
}

}

This is the way I did it:

int main() {

double weight;

double PW;

int planet;

std::cout << “How many kilos do you weight on earth?\n”;

std::cin >> weight;

std::cout << “Which planet you prefer from the list below\n”;

std::cout << “1 Mercury\n”;

std::cout << “2 Venus\n”;

std::cout << “3 Mars\n”;

std::cout << “4 Jupiter\n”;

std::cout << “5 Saturn\n”;

std::cout << “6 Uranus\n”;

std::cout << “7 Neptune\n”;

std::cin >> planet;

switch (planet) {

case 1:

std::cout << “Mercury\n”;

PW = weight * 0.38;

break;

case 2:

std::cout << “Venus\n”;

PW = weight * 0.91;

break;

case 3:

std::cout << “Mars\n”;

PW = weight * 0.38;

break;

case 4:

std::cout << “Jupiter\n”;

PW = weight * 2.34;

break;

case 5:

std::cout << “Saturn\n”;

PW = weight * 1.06;

break;

case 6:

std::cout << “Uranus\n”;

PW = weight * 0.92;

break;

case 7:

std::cout << “Neptune\n”;

PW = weight * 1.19;

break;

default :

std::cout << “Unknown\n”;

break;

}

std::cout << “Your weight on that planet is: \n” << PW << " kilos\n";

}

Please format your code properly in codeblocks or backticks (cpp...)

From your code, I can find many issues:

  • Line 1 (#include ???) (An invalid statement because you did not import anything into the directive)
  • Line 6 (cart PN) (cart is not a datatype, also variable names should not have spaces in between so you should name it as cartPN with the datatype specified in front or PN and just change cart to your specific datatype)
  • Line 8 (wait???) (not a valid keyword in C++, assuming it was a comment so put it inside comment quotes or blocks)
  • Line 17, 21, 55, 57 (std::cout>> is wrong for obvious reasons)
  • Line 32, 33, 35, 36, 38, 39, 41, 42, 44, 45, 47, 48 (== is a comparison operatior, not an assignment operator)

And that’s just the syntax of your code, not dwelling into the logical part yet

In C++, “\n” is the escape sequence for a newline character, which is used to move the cursor to the beginning of the next line when printing text to the console or a file. Also, one can omit \n from the code and just use endl (remember to add the corresponding directives and not use both \n and endl in the same line of code or string) As such:

using std::cout;
using std::endl;

cout << .... << endl;

It looks like the issue in your code lies in the use of the assignment operator (= ) instead of the equality operator (== ) in your if and else if conditions. In C++, the single equals sign is used for assignment, whereas the double equals sign is used for comparison.