FAQ: Conditionals & Logic - Review

I used a switch for my solution. I think its a pretty clean and simple solution:

#include

int main() {

double weight_on_earth;
int p;
double weight;
std::cout << “What is your weight on earth?\n”;
std::cin >> weight_on_earth;
std::cout << “What planet are you fighting on?\n”;
std::cout << “Enter 1 for Mercury, 2 for Venus, \n3 for Mars, 4 for Jupiter, 5 for Saturn, \n6 for Uranus, or 7 for Neptune\n”;
std::cin >> p;

switch (p) {
case 1:
weight = weight_on_earth * 0.38;
std::cout << “Your weight on Mervury is " << weight << " pounds\n”;
break;
case 2:
weight = weight_on_earth * 0.91;
std::cout << “Your weight on Venus is " << weight << " pounds\n”;
break;
case 3:
weight = weight_on_earth * 0.38;
std::cout << “Your weight on Mars is " << weight << " pounds\n”;
break;
case 4:
weight = weight_on_earth * 2.34;
std::cout << “Your weight on Jupiter is " << weight << " pounds\n”;
break;
case 5:
weight = weight_on_earth * 1.06;
std::cout << “Your weight on Saturn is " << weight << " pounds\n”;
break;
case 6:
weight = weight_on_earth * 0.92;
std::cout << “Your weight on Uranus is " << weight << " pounds\n”;
break;
case 7:
weight = weight_on_earth * 1.19;
std::cout << “Your weight on Neptune is " << weight << " pounds\n”;
break;
default:
std::cout << “Invalid planet entry. Please pick a number between 1 and 7\n”;
break;
}

}

I had an issue because I used switch to process the number of the planet and then accidentally put a space before the “}” at the end of the function. facepalm I spent like 15 minutes writing the code and 30 minutes reeling over this f*** “}”

I tried the new fork option to code on the side and wrote this small program:

#include

int main() {

int x;

std::cout << “Welcome boxer man! Please enter what your earth weight is!\n”;

std::cin >> x;

std::cout << “Thank you. Please confirm your entry of " << x << " by typing Y or N.\n”;

}

It is working on an online compiler but not the fork compiler for some reason. If someone could let me know why I would appreciate it. The error is it automatically calculates a number with no user input:

Welcome boxer man! Please enter what your earth weight is!
Thank you. Please confirm your entry of 32765 by typing Y or N.

#include

int main() {

int Earth_Weight;

int planet_number;

double target_weight;

std::cout<<“What is Earth Weight\n”;

std::cin>>Earth_Weight;

//std::cout << “Number Planet\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::cout << “Enter Planet Number\n”;

std::cout << “enter a number for the planet You wants to fight on\n”;

std::cin>>planet_number;

// logic

switch(planet_number){

case 1:

target_weight = Earth_Weight*0.38;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

case 2:

target_weight = Earth_Weight*0.91;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

case 3:

target_weight = Earth_Weight*0.38;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

case 4:

target_weight = Earth_Weight*2.34;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

case 5:

target_weight = Earth_Weight*1.06;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

case 6:

target_weight = Earth_Weight*0.92;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

case 7:

target_weight = Earth_Weight*1.19;

std::cout<<“weight on the destination planet.” <<target_weight;

break;

default :

std::cout<<“invalid”;

}
}

I don’t know what’s wrong.
Please Help Me

#include

int main() {
double weight = 0.0;
char Planet;

std::cout << “Little Mac’s weight: \n”;
std::cout << “Planet: \n”;

std::cin >> weight;
std::cin >> Planet;

switch(Planet)
{
case “Mercurry”:
std::cout << weight0.38;
break;
case “Venus”:
std::cout << weight
0.91;
break;
case “Mars”:
std::cout << weight0.38;
break;
case “Jupiter”:
std::cout << weight
2.34;
break;
case “Saturn”:
std::cout << weight1.06;
break;
case “Uranus”:
std::cout << weight
0.92;
break;
default:
std::cout << weight*1.19;
break;
}
}

See How do I format code in my posts? for some details on keeping nicer formatting when posting code on the forums.

One potential issue is that that char type can store just a single character. You’ll want a type that can do more than one for your current set-up.

I have a problem. i dont know if i did something wrong or what but my code is doing the math wrong when i run it with the console. Everything else works like a charm but it doesnt multiply right. For example if i try to calculate the weight on jupiter (planet 4), when i put in a weight of 280 it give me a value of 106.4 when it should be 655.2.

#include

double weight;

int x;

int main() {

std::cout<<“Please enter current Earth weight \n”;

std::cin>>weight;

std::cout<<“What planet are you visiting?\n”;

std::cout<<“Mercury = 1, Venus = 2, Mars = 3, \n”;

std::cout<<“Jupiter = 4, Saturn = 5, Uranus = 6, Neptune = 7\n”;

std::cin>>x;

if(x=1){

weight = weight * .38;

}

else if(x=2){

weight = weight * .91;

}

else if(x=3){

weight = weight * .38;

}

else if(x=4){

weight = weight * 2.34;

}

else if(x=5){

weight = weight * 1.06;

}

else if(x=6){

weight = weight * .92;

}

else if(x=7){

weight = weight * 1.19;

}

std::cout<< “Your weight on the planet will be”<<weight<<"\n";

There’s a difference between the assignment operator = and equivalence == which unfortunately you won’t see a warning for.

i send my answers

#include <iostream>
int main () {

  double earthw;
  int x;

  std::cout << "Enter in your weight on Earth (lbs):\n";
  std::cin >> earthw;
  std::cout << "These are the planets available to compete in:\n";
  std::cout << "Mercury - (1) | Venus -- (2) | Mars -- (3)\n";
  std::cout << "Jupiter - (4) | Saturn - (5) | Uranus  (6)\n";
  std::cout << "Neptune - (7) | nil ----- () | nil ---- ()\n";
  std::cout << "Select a planet:\n";
  std::cin >> x;

  switch (x) {
  case 1:
  std::cout << "Your weight on Mercury is " << earthw * 0.38 << " lbs.\n";
  break;
  case 2:
  std::cout << "Your weight on Venus is " << earthw * 0.91 << " lbs.\n";
  break;
  case 3:
  std::cout << "Your weight on Mars is " << earthw * 0.38 << " lbs.\n";
  break;
  case 4:
  std::cout << "Your weight on Jupiter is " << earthw * 2.34 << " lbs.\n";
  break;
  case 5:
  std::cout << "Your weight on Saturn is " << earthw * 1.06 << " lbs.\n";
  break;
  case 6:
  std::cout << "Your weight on Uranus is " << earthw * 0.92 << " lbs.\n";
  break;
  case 7:
  std::cout << "Your weight on Neptune is " << earthw * 1.19 << " lbs.\n";
  break;
  default:
  std::cout << "You entered an invalid selection.\n";
  break;
  }
  
  return 0;
}

Wanted to try and have fun with the planet thing, but i stumbled on a segmentation fault.

std::string strolower(std::string pname) {
std::string s = “”;

//Iterate through each char of a string and lowercase it. Then concatenate
for (int i=0; i < pname.length(); i++) {
char c = pname[i];
c = tolower(c);
s = s + c;
}
}
This is the code for a function i declared outside of main(). Whenever i inputted a string i got the segmentation fault. anyone know why that might happen here?

/*
Little Mac is an interplanetary space boxer, who is trying to win championship belts for various weight categories on other planets within the solar system.
*/
#include <iostream>

int main() {
  
  int earth_weight;
  int planet;
  std::cout << "What is your weight on Earth? ";
  std::cin >> earth_weight;
  std::cout << "Enter number of planet to fight on:  ";
  std::cout << "Options:\n";
  std::cout << "1 - Mercury; 2 - Venus; 3 - Mars; 4 - Jupiter;\n";
  std::cout << "5 - Saturn; 6 - Uranus; 7 - Neptune.\n";
  std::cin >> planet;
  //calculate Little Mac's weight on each planet
  switch(planet) {
    case 1 :
      std::cout << "Your weight on Mercury is: " << earth_weight * 0.38 << "\n";
      break;
    case 2 :
      std::cout << "Your weight on Venus is: " << earth_weight * 0.91 << "\n";
      break;
    case 3 :
      std::cout << "Your weight on Mars is: " << earth_weight * 0.38 << "\n";
      break;
    case 4 :
      std::cout << "Your weight on Jupiter is: " << earth_weight * 2.34 << "\n";
      break;
    case 5 :
      std::cout << "Your weight on Saturn is: " << earth_weight * 1.06 << "\n";
      break;
    case 6 :
      std::cout << "Your weight on Uranus is: " << earth_weight * 0.92 << "\n";
      break;
    case 7 :
      std::cout << "Your weight on Neptune is: " << earth_weight * 1.19 << "\n";
      break;
    default :
      break;
  }
  
}

So I managed to get this to work in a weird way. The answer will be correct if a number b/t 1-7 is used. However, if anything below or above that range is used, the default will only return an answer rounded off to a whole number. Advice?

#include <iostream>

int main() {

double weight;
int x;

std::cout << "What is the items weight? \n";
std::cin >> weight;
std::cout << "Enter a number for the planet you want to calculate: \n";
std::cin >> x;

switch (x) {
    case 1 :
    std::cout << "The weight on Mercury is " << weight * 0.38 << "\n";
    break; 
    case 2 :
    std::cout << "The weight on Venus is " << weight * 0.91 << "\n"; 
    break;
    case 3 :
    std::cout << "The weight on Mars is " << weight * 0.38 << "\n"; 
    break;
    case 4 :
    std::cout << "The weight on Jupiter is " << weight * 2.34 << "\n"; 
    break;
    case 5 :
    std::cout << "The weight on Saturn is " << weight * 1.06 << "\n"; 
    break;
    case 6 :
    std::cout << "The weight on Uranus is " << weight * 0.92 << "\n"; 
    break;
    case 7 :
    std::cout << "The weight on Neptune is " << weight * 1.19 << "\n"; 
    break;   
   default: std::cout << "Sorry, we don't have a weight for this planet\n ";
   std::cout << "Please enter a number between 1 - 7: \n";
   std::cin >> x; 
    std::cout << "\nItem weight: " << weight << "\n";
  break;
  }
 }

I wanted to give the user the option to type in the planet, it seemed more realistic. The issue I noticed was, if the user types in something incorrectly, the program would just end. I found that providing the conditional under a while loop would allow the user another chance to get the input. However, if the user got it wrong twice, it would trigger an infinite loop. It seems that reassigning a true value to and already true value was the issue. To remedy this, I found that adding another conditional, in the final else statement, that checks if the boolean was already true and changes it back to false before true again worked. (Just using while loop, without the final else statement, works as well. However, the user is not given message informing them what went wrong.)

#include <iostream>
using namespace std;

int main() {
  
  double weight;
  string planet;
  bool check = true;
  
  while (check == true) {
    std::cout << "What is your weight on Earth? ";
    std::cin >> weight;
    std::cout << "What planet do you want to fight on? ";
    std::cin >> planet;  
    
    if (planet == "Mercury") {
      double merc_weight = weight * 0.38;
      std::cout << "Your weight would be " << merc_weight << "lbs on Mercury.\n";
      check = false;
    }
    else if (planet == "Venus") {
      double ven_weight = weight * 0.91;
      std::cout << "Your weight would be " << ven_weight << "lbs on Venus.\n";
      check = false;
    }
    else if (planet == "Mars") {
      double mar_weight = weight * 0.38;
      std::cout << "Your weight would be " << mar_weight << "lbs on Mars.\n";
      check = false;
    }
    else if (planet == "Jupiter") {
      double jup_weight = weight * 2.34;
      std::cout << "Your weight would be " << jup_weight << "lbs on Jupiter.\n";
      check = false;
    }
    else if (planet == "Saturn") {
      double sat_weight = weight * 1.06;
      std::cout << "Your weight would be " << sat_weight << "lbs on Saturn.\n";
      check = false;
    }
    else if (planet == "Uranus") {
      double ura_weight = weight * 0.92;
      std::cout << "Your weight would be " << ura_weight << "lbs on Uranus.\n";
      check = false;
    }
    else if (planet == "Neptune") {
      double nep_weight = weight * 1.19;
      std::cout << "Your weight would be " << nep_weight<< "lbs on Neptune.\n";
      check = false;
    }
    else {
      std::cout << "Please enter a valid planet. Remember to capitalize!\n";
      if (check == true){
        check = false;
      }
      check = true;    
      }
  } 
}

No, that shouldn’t be an issue. Before entering the while loop, you have already initialized check as true.

else {
    std::cout << "Please enter a valid planet. Remember to capitalize!\n";
    if (check == true) {
        check = false;
    }
    check = true;    
}

In the else block, the cout statement is all that is needed. The if statement just flips check from true to false and then immediately, check is reset to true again. So, the if statement doesn’t do anything useful and can be omitted. Even the check = true; statement is superfluous. You have already initialized check as true before entering the while loop. The only way that check would be flipped to false is when the entered planet matches one of the if/else-if conditions. In that case, check will be assigned false and eventually the while loop will terminate without entering the else block. So,

else {
    std::cout << "Please enter a valid planet. Remember to capitalize!\n";
}

should be sufficient.

In your code, if the entered planet doesn’t match any valid planet, then the cout message from the else block is printed, and the user is prompted to enter the weight and planet again. I suspect you got stuck in an infiinite loop after the re-prompt because when prompted for the weight, you may have inadvertently entered the planet name instead. Since weight has been declared as a double, the cin will fail because of a failure to convert a string to a double. You will then be stuck in an infinite loop. The prompts and cout string from the else block will keep printing without pausing to allow any input to be entered. You can replicate this behavior by typing in a string instead of a number when prompted for weight.

For a detailed explanation, see: https://www.learncpp.com/cpp-tutorial/stdcin-and-handling-invalid-input/ (Specifically the section "Error case 3: Extraction fails" details the error you are seeing). The webpage also offers solutions on how to deal with failed extractions.

As an aside, the boolean variable check isn’t necessary. You could just use the boolean true as the while condition and use breaks in the valid planet blocks:

  while (true) {
    std::cout << "What is your weight on Earth? ";
    std::cin >> weight;
    std::cout << "What planet do you want to fight on? ";
    std::cin >> planet;  
    
    if (planet == "Mercury") {
      double merc_weight = weight * 0.38;
      std::cout << "Your weight would be " << merc_weight << "lbs on Mercury.\n";
      break; // breaks in these blocks to exit while loop
    }
    else if ...
    }
    else {
      std::cout << "Please enter a valid planet. Remember to capitalize!\n";
      }
  } 

Alternatively, you could remove all the breaks from the valid planet blocks and just a single break after the if/else-if/else blocks. For this to work, you will need a continue; statement in the else block so that if the entered planet is invalid, then the continue will allow you to skip over the break; statement and start another iteration of the while loop:

  while (true) {
    std::cout << "What is your weight on Earth? ";
    std::cin >> weight;
    std::cout << "What planet do you want to fight on? ";
    std::cin >> planet;  
    
    if (planet == "Mercury") {
      double merc_weight = weight * 0.38;
      std::cout << "Your weight would be " << merc_weight << "lbs on Mercury.\n";
    }
    else if ...
    }
    else {
      std::cout << "Please enter a valid planet. Remember to capitalize!\n";
      continue; // If invalid planet, continue will allow skipping over the break
      }
    break; // A single break as the last statement of the while loop
  } 

Here’s a nice solution

// A program that calculates users’ weight on a destination planet
#include

// prompts user to enter data into a variable of any type
template
void prompt_user(const char* string, T& variable) {
std::cout << string;
std::cin >> variable;
}

int main() {

double weight;
int planet;

prompt_user("Hello Little Mac! Please enter your weight on Earth: ", weight);

prompt_user("Please enter the number of the planet you want to fight on: ", planet);

const int NUMBER_OF_PLANETS = 7;
double relative_weights[NUMBER_OF_PLANETS] = {0.38, 0.91, 0.38, 2.34, 1.06, 0.92, 1.19};

std::cout << "The weight on your destination planet is: " << weight * relative_weights[planet - 1] << “.\n”;

return 0;

}

Before you look at my code, note that I learned C# and Java before I took this course, so I had some concepts down that you might not have. Don’t beat yourself up, I still had to google some things lol

#include <iostream>

// Yeah, Yeah, global variables, gross, im not using a class though
// Gravity Constants
const double mercuryGrav = 0.38;
const double venusGrav = 0.91;
const double marsGrav = 0.38;
const double jupiterGrav = 2.34;
const double saturnGrav = 1.06;
const double uranusGrav = 0.92;
const double neptuneGrav = 1.19;

// This is what I call an "I'm Lazy" Function.
void print_options() {

  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";

}

double get_weight_on_planet(int index, double weight) {

  double usedGrav = 0;

  // find out what gravity constant to use
  switch (index) {

    case 1:
      usedGrav = mercuryGrav;
      break;
    case 2:
      usedGrav = venusGrav;
      break;
    case 3:
      usedGrav = marsGrav;
      break;
    case 4:
      usedGrav = jupiterGrav;
      break;
    case 5:
      usedGrav = saturnGrav;
      break;
    case 6:
      usedGrav = uranusGrav;
      break;
    case 7:
      usedGrav = neptuneGrav;
      break;
    default:
      std::cout << "Invalid Planet Number\n";
      break;
  }

  // calculate the weight
  double newWeight = weight * usedGrav;
  return newWeight;

}

double get_earth_weight() {

  double response;
  std::cout << "What's your weight on earth?\nWeight: ";
  std::cin >> response;
  return response;

}

int main() {

  // Get the weight
  double weight = get_earth_weight();
  int planetIndex;
  // ask for planet number
  std::cout << "What planet do you want to fight on? The options are \n";
  // ;) print the numbers with names cause lazy
  print_options();
  std::cout << "Planet Number: ";
  std::cin >> planetIndex;
  // Get the relative weight
  double otherPlanetWeight = get_weight_on_planet(planetIndex, weight);
  // Print the weight
  std::cout << "Your weight on that planet is " << otherPlanetWeight << "\n";

}

I may know some things already, but if you know about Pointers and Templates, why are you taking THIS course? :joy:

You didn’t have to use a while loop there, it would’ve been easier to read by recursively calling an input function like so:

#include <iostream>

// Function to print the menu
void PrintMenu() {

    // Print the menu
    std::cout << "1. Add" << std::endl;
    std::cout << "2. Subtract" << std::endl;
    std::cout << "3. Multiply" << std::endl;
    std::cout << "4. Divide" << std::endl;
    std::cout << "5. Modulus" << std::endl;
    std::cout << "6. Exit" << std::endl;

}

// Function to get input from the user
char Input() {

    // Declare the variable
    int input;

    // Ask the user for input
    PrintMenu();
    std::cout << "Enter an option: \n";

    // Get the input
    std::cin >> input;

    if (input > 6 || input < 1) {
        std::cout << "Invalid input. Please try again.\n";
        Input();
    }

    // Return the input
    return input;

}

int main() {

    // Call the Input function
    int input = Input();
    // Do something with the Input
    // ...

    return 0;

}

It’s much easier to read, but we haven’t gotten to functions, have we? Oops, spoilers! :wink: read this when you’ve done functions.

Here’s what I came up with.

#include <iostream> int main() { // Initialize variables double weight = 0; double planet_weight = 0; int planet = 0; // set up weight and planet input std::cout << "What is your Earth weight?\n"; std::cin >> weight; std::cout << "Enter a number for the planet you are fighting: \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 to determine output switch (planet) { case 1: planet_weight = weight * 0.38; std::cout << "Your weight on Mercury will be: " << planet_weight << "\n"; break; case 2: planet_weight = weight * 0.91; std::cout << "Your weight on Venus will be: " << planet_weight << "\n"; break; case 3: planet_weight = weight * 0.38; std::cout << "Your weight on Mars will be: " << planet_weight << "\n"; break; case 4: planet_weight = weight * 2.34; std::cout << "Your weight on Jupiter will be: " << planet_weight << "\n"; break; case 5: planet_weight = weight * 1.06; std::cout << "Your weight on Saturn will be: " << planet_weight << "\n"; break; case 6: planet_weight = weight * 0.92; std::cout << "Your weight on Uranus will be: " << planet_weight << "\n"; break; case 7: planet_weight = weight * 1.19; std::cout << "Your weight on Neptune will be: " << planet_weight << "\n"; break; default: std::cout << "Wrong number! Try again!\n"; break; } }

Please help I am getting an error and not sure what in my code is wrong, anything would be greatly appreciated.

#include
using namespace std;

int main() {
double weight {};
cout << “How much do you weight?\n”;
cin >> weight;
cout << “Pick a number 1-7 rep the plants in order\n”;
int number {};
switch(number) {
//
case 1:
double mercury = 0.38;
double mercuryWeight = mercury * weight;
cout << "Your weight on Mercury is " << mercuryWeight << “\n”;
break;
//
case 2:
double venus = 0.91;
double venusWeight = venus * weight;
cout << "Your weight on Venus is " << venusWeight << “\n”;
break;
//
case 3:
double mars = 0.38;
double marsWeight = mars * weight;
cout << "Your weight on Mars is " << marsWeight << “\n”;
break;
//
case 4:
double jupiter = 2.34;
double jupiterWeight = jupiter * weight;
cout << "Your weight on Jupiter is " << jupiterWeight << “\n”;
break;
//
case 5:
double saturn = 1.06;
double saturnWeight = saturn * weight;
cout << "Your weight on Saturn is " << saturnWeight << “\n”;
break;
//
case 6:
double uranus = 0.92;
double uranusWeight = uranus * weight;
cout << "Your weight on Uranus is " << uranusWeight << “\n”;
break;
//
case 7:
double neptune = 1.19;
double neptuneWeight = neptune * weight;
cout << "Your weight on Neptune is " << neptuneWeight << “\n”;
break;
//
}

return 0;

}