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.

#include <iostream> int main() { // declaring for variables earthweight, # of each planet, and destination planet weight. double ew, dp; int np; // declaration of relative gravity on each planet, respectively. double rg1, rg2, rg3, rg4, rg5, rg6, rg7; rg1 = 0.38; rg2 = 0.91; rg3 = 0.38; rg4 = 2.34; rg5 = 1.06; rg6 = 0.92; rg7 = 1.19; std::cout << "What is Little Mac's weight on Earth? \n"; std::cout << "Please enter in kg: \n"; std::cin >> ew; std::cout << "In which planet does he want to fight? \n"; std::cout << "Enter the planet's #: \n"; std::cin >> np; switch (np) { case 1: std::cout << "Entering Mercury...\n Converting Little Mac's weight from Earth \n"; dp = ew * 0.38; std::cout << "Little Mac's weight on Mercury is: "; std::cout << dp << "kg."; break; case 2: std::cout << "Entering Venus...\n Converting Little Mac's weight from Earth \n"; dp = ew * 0.91; std::cout << "Little Mac's weight on Venus is: "; std::cout << dp << "kg."; break; case 3: std::cout << "Entering Mars...\n Converting Little Mac's weight from Earth \n"; dp = ew * 0.38; std::cout << "Little Mac's weight on Mars is: "; std::cout << dp << "kg."; break; case 4: std::cout << "Entering Jupiter...\n Converting Little Mac's weight from Earth \n"; dp = ew * 2.34; std::cout << "Little Mac's weight on Jupiter is: "; std::cout << dp << "kg."; break; case 5: std::cout << "Entering Saturn...\n Converting Little Mac's weight from Earth \n"; dp = ew * 1.06; std::cout << "Little Mac's weight on Saturn is: "; std::cout << dp << "kg."; break; case 6: std::cout << "Entering Uranus...\n Converting Little Mac's weight from Earth \n"; dp = ew * 0.92; std::cout << "Little Mac's weight on Uranus is: "; std::cout << dp << "kg."; break; case 7: std::cout << "Entering Neptune...\n Converting Little Mac's weight from Earth \n"; dp = ew * 1.19; std::cout << "Little Mac's weight on Neptune is: "; std::cout << dp << "kg."; break; default: std::cout <<"The planet you entered is not included in our planetary jump system."; } return 0; }

This is my code.

  1. #include

  2. int main() {

  3. int weight;

  4. int planet;

  5. double size;

  6. std:cout << “weight on earth.”;

  7. std::cin >> weight;

  8. std::cout << “What planet would you like to visit, please enter the number.”;

  9. std::cin >> planet;

  10. std::cout << “1 Mercury” << “2 Venus” << “3 Mars” << “4 Jupiter” << “5 Saturn” << “6 Uranus” << “7 Neptune \n”;

  11. if (planet == 1) {

  12. std::cout << "Mercury\n";
    
  13. size = weight * 0.38;
    
  14. std::cout << size; 
    
  15. }

  16. else if (planet == 2) {

  17. std::cout << "Venus\n";
    
  18. size = weight * 0.91;
    
  19. std::cout << size;
    
  20. }

  21. else if (planet == 3) {

  22. std::cout << "Mars\n";
    
  23. size = weight * 0.38;
    
  24. std::cout << size;
    
  25. }

  26. else if (planet == 4) {

  27. std::cout << "Jupiter\n";
    
  28. size = weight * 2.34;
    
  29. std::cout << size;
    
  30. }

  31. else if (planet == 5) {

  32. std::cout << "Saturn\n";
    
  33. size = weight * 1.06;
    
  34. std::cout << size; 
    
  35. }

  36. else if (planet == 6) {

  37. std::cout << "Uranus\n";
    
  38. size = weight * 0.92;
    
  39. std::cout << size; 
    
  40. }

  41. else {

  42. std::cout << "Neptune\n";
    
  43. size = weight * 1.19;
    
  44. std::cout << size; 
    
  45. }

  46. }

I did the optional exercise for the review section
I think i took out all of the mistakes i have made but when i try to compile it, it gives me a fatal error,
Please help.

To preserve code formatting in forum posts: see: [How to] Format code in posts

There might be more issues, but one issue is on line 8 where you wrote    std:cout    instead of    std::cout

there are a lot of issues but that one was stopping it from compiling, thank you. The link leads me to an unavailable page.

1 Like

The link was working when I posted it, but it seems that Codecademy is tinkering with the forums. For the meantime, a copy of that thread seems to be available at: How do I format code in my posts?

your fatal error may be at line 1 # include should be #include iostream enclosed in <>

here is a similar code `

#include <iostream>

int main() {
// declared variables
double weight;
int planet;
double size;

// takes the weight on earth assigns to weight variable
std::cout << "weight on earth.";
std::cin >> weight;

// provides a list of planets to select and takes 
// the user input to assign to which planet 
std::cout << "1 Mercury\n2 Venus\n3 Mars\n4 Jupiter\n5 Saturn\n6 Uranus\n7 Neptune \nWhat planet would you like to visit, please enter the number.";
std::cin >> planet;


// checks if the user input is 1 
if (planet = 1) {
size = weight * 0.38;
std::cout << "Your wieght on Mercury\n" << size; 

}
// checks if the user input is 2 
else if (planet = 2) {
size = weight * 0.91;
std::cout << "Your wieght on Venus\n" << size;

}
// checks if the user input is 3
else if (planet = 3) {
size = weight * 0.38;
std::cout << "Your wieght on Mars\n" << size;
}
// checks if the user input is 4
else if (planet = 4) {
size = weight * 2.34;
std::cout << "Your wieght on Jupiter\n" << size;
}
// checks if the user input is 5
else if (planet = 5) {
size = weight * 1.06;
std::cout << "Your wieght on Saturn\n" << size;
}
// checks if the user input is 6
else if (planet = 6) {
size = weight * 0.92;
std::cout << "Your wieght on Uranus\n" << size;
}
// checks if the user input is 7
else {
size = weight * 1.19;
std::cout << "Your wieght on neptune\n" << size;

} }````

This is my incredibly over-complicated first attempt, however it works and is ready to be compiled. Any tips or advice on best practice would be appreciated! :grinning:

//Converts your earth weight to your weight on other planets. Created 12/08/2024 


#include <iostream>

int main() {
  //Initialize and declare all nessicary variables for calculations
  const int mercury = 1;
  const double mercuryGrav = 0.38;
  const int venus = 2;
  const double venusGrav = 0.91;
  const int mars = 3;
  const double marsGrav = 0.38;
  const int jupiter = 4;
  const double jupiterGrav = 2.34;
  const int saturn = 5;
  const double saturnGrav = 1.06;
  const int uranus = 6;
  const double uranusGrav = 0.92;
  const int neptune = 7;
  const double neptuneGrav = 1.19;
  double earthWeight;
  int planet;
  double planetWeight;

  //Initial user inputs
  std::cout << "What is your weight on earth? \n";        
  std::cin >> earthWeight;
  std::cout << "(1.) Mercury (2.) Venus (3.) Mars (4.) Jupiter (5.) Saturn (6.) Uranus (7.) Nepture\n";
  std::cout << "Enter a number for the planet to visit: ";
  std::cin >> planet;

  //Switch statement takes planet number input and looks for necesary calculation to make
  switch(planet) {
    case 1:
      planetWeight = earthWeight * mercuryGrav;
      std::cout << "Your weight on Mercury is " << planetWeight << ".\n";
      break;
    case 2:
      planetWeight = earthWeight * venusGrav;
      std::cout << "Your weight on Venus is " << planetWeight << ".\n";
      break;
    case 3:
      planetWeight = earthWeight * marsGrav;
      std::cout << "Your weight on Mars is " << planetWeight << ".\n";
      break;
    case 4:
      planetWeight = earthWeight * jupiterGrav;
      std::cout << "Your weight on Jupiter is " << planetWeight << ".\n";
      break;
    case 5:
      planetWeight = earthWeight * saturnGrav;
      std::cout << "Your weight on Saturn is " << planetWeight << ".\n";
      break;
    case 6:
      planetWeight = earthWeight * uranusGrav;
      std::cout << "Your weight on Uranus is " << planetWeight << ".\n";
      break;
    case 7:
      planetWeight = earthWeight * neptuneGrav;
      std::cout << "Your weight on Neptune is " << planetWeight << ".\n";
      break;
    default:
      std::cout << "Invalid.\n";  
  }
  //Pause to keep window open
  std::cout << "Press Enter to exit...";
  std::cin.ignore();    //To ignore anu leftover newline character
  std::cin.get();       //To wait for the Enter key

  return 0;
}