FAQ: Conditionals & Logic - Review

hallo!!
i have been wanting to ask, please what´s the use of the double “\n\n” at the end of this?
std::cout << “\nI have information for the following planets:\n\n”;
std::cout << " 1. Venus 2. Mars 3. Jupiter\n";
std::cout << " 4. Saturn 5. Uranus 6. Neptune\n\n";

please can someone tell me what i did wrong in this?

#include

int main() {
double x;
std::cout << "What is your current earth weight: “;
std::cin >> weight;
std::cout << " 1. Venus 2. Mars 3. Jupiter 4. Saturn 5. Uranus 6. Neptune\n”;
std::cout << "Enter a number from ( 1 - 6 ) in the planet you wish to fight in: ";
std::cin >> number;

switch (weight) {
case 1:
planet weight = weight * 0.78;
planet name = venus;
break;
case 2:
planet weight = weight * 0.39;
planet name = mars;
break;
case 3:
planet weight = weight * 2.65;
planet name = jupiter;
break;
case 4:
planet weight = weight * 1.17;
planet name = saturn;
break;
case 5:
planet weight = weight * 1.05;
planet name = uranus;
break;
case 6:
planet weight = weight * 1.23;
planet name = neptune;
break;

}

std::cout << " your weight is " << weight << “\n”;

}

  std::cout << "What is your current earth weight: “;

Missing the end of that output.

  std::cin > > weight;

weight is undefined.

  std::cin >> number;

number is undefined

  switch (weight)

expression must have integral or enum type. Besides, you are doing a switch on number that represents a planet, not weight.

    planet weight = weight * 0.78;

Not sure what your expecting or trying to do but your planet weight is undefined, and you can’t have a space in a variable name. Look at your variables again and figure out which one you meant to use and exclude planet from it.

I initially tried using cases but it wasn’t working, so switched to else statements. For some reason, it doesn’t seem to be accepting the user input for which planet to fight on (only returns weight for Venus).

#include

int main() {

double Earthweight = 0.0;

std::cout<<“What is your weight on earth (kg)?\n”;

std::cin>>Earthweight;

std::cout<<"What planet do you want to fight on?\n Enter 1 for Venus, 2 for Mars, 3 for Jupiter, 4 for Saturn, 5 for Uranus, or 6 for Neptune\n ";

int Planet = 0;

std::cin>> Planet;

double Planetweight = 0.0;

if (Planet=1){

Planetweight = (0.78)*Earthweight;

std::cout<<"Your weight would be "<<Planetweight<<" kg\n";

}

else if (Planet = 2){

Planetweight = (0.39)*Earthweight;

std::cout<<"Your weight would be "<<Planetweight<<" kg\n";

}

else if (Planet = 3){

Planetweight = (2.65)*Earthweight;

std::cout<<"Your weight would be "<<Planetweight<<" kg\n";

}

else if (Planet = 4){

Planetweight = 1.17*Earthweight;

std::cout<<"Your weight would be "<<Planetweight<<" kg\n";

}

else if (Planet = 5){

Planetweight = 1.05*Earthweight;

std::cout<<"Your weight would be "<<Planetweight<<" kg\n";

}

else if (Planet = 6){

Planetweight = 1.23*Earthweight;

std::cout<<"Your weight would be "<<Planetweight<<" kg\n";

}

else{

std::cout<<"You have not entered a valid number\n";

}

}

Any help appreciated

Why \n is used? Sometime it is used in front and sometime in the back, why? Somebody please describe it…

Here is my solution to the optional space.cpp project:

#include <iostream>
int main () {
  double weight, venus, mars, jupiter, saturn, uranus, neptune;
  int planet;
  double fight_weight;
  
  std::cout << "Enter your weight in kg: ";
  std::cin >> weight;

  std::cout << "Enter planet number to fight on: \n" << "1. Venus\n2. Mars\n3. Jupiter\n4. Saturn\n5. Uranus\n6. Neptune\n";
  std::cin >> planet;

  switch (planet) {
    case 1:
     fight_weight = weight * 0.78;
     std::cout << "You need to be: " << fight_weight << "kg on Earth to fight on Venus.\n";
     break;
    case 2:
     fight_weight = weight * 0.39;
     std::cout << "You need to be: " << fight_weight << "kg on Earth to fight on Mars.\n";
     break;
    case 3:
     fight_weight = weight * 2.65;
     std::cout << "You need to be: " << fight_weight << "kg on Earth to fight on Jupiter.\n";
     break;
    case 4: 
     fight_weight = weight * 1.17;
     std::cout << "You need to be: " << fight_weight << "kg on Earth to fight on Saturn\n";
     break;
    case 5:
     fight_weight = weight * 1.05;
     std::cout << "You need to be: " << fight_weight << "kg on Earth to fight in Uranus.\n";
     break;
    case 6:
     fight_weight = weight * 1.23;
     std::cout << "You need to be: " << fight_weight << "kg on Earth to fight on Neptune\n";
     break;
    default:
     std::cout << "That is not a valid number!";
     break;
  }
}

\n just creates a new line, so for example this:

std::cout << “What is your name?\n”;
std::cin >> name;
std::cout << "Your name is " << name << endl;

Would output:
What is your name?
‘name’
Your name is ‘name’

If you did:
std::cout << “What is your name?\n”;
std::cin >> name;
std::cout << "\nYour name is " << name << endl;

You get:
What is your name?
‘name’

Your name is ‘name’

So it just all comes down to where you want the new line to start.
Also endl works as well, but must be chained as in the examples.

Hope this helps!

1 Like

Hello everyone, I am having a little trouble with understanding a couple things from this exercise. My questions is, for example in the hint, the first planet is venus which has a relative gravity of 0.91 yet when using the calculation in the if statemente they use 0.78, isn’t it supposed to be earth weight * relative gravity?

Is there something im im missing? Everyone above does seem to be using 0.78 and cant figure out why.

# Planet Relative Gravity
1 Mercury 0.38
2 Venus 0.91
3 Mars 0.38
4 Jupiter 2.34
5 Saturn 1.06
6 Uranus 0.92
7 Neptune 1.19

Why is this happening? I’m following Stephanie’s advice and compiling and running the code regularly throughout the process, and I’m right at the beginning.

How to compile and run C programs avoiding “bash: ./a.out: Permission denied”
This may not work because this is what you would do on your local machine.

chmod +x ./a.out

I would in another screenshot output ls and pwd to verify your in the correct directory/root.

I would just try to get it to output with a.out.

This may be a dumb question, but why don’t the relative gravity variables on the table of conversions match what should be written in the program?

//My code seems a little different to everyone else’s but still does the trick or am I wrong?

#include

int main() {

double weight;

int planet;

std::cout<<“How fat are you? (kg)\n”;

std::cin>>weight;

std::cout<<“Enter a number for the planet you want to kick ■■■ in\n”;

std::cin>>planet;

switch (planet) {

case 1 :

std::cout<<"You will be "<<weight*0.38<<"kg\n";

break;

case 2 :

std::cout<<"You will be "<<weight*0.91<<"kg\n";

break;

case 3 :

std::cout<<"You will be "<<weight*0.38<<"kg\n";

break;

case 4 :

std::cout<<"You will be "<<weight*2.34<<"kg\n";

break;

case 5 :

std::cout<<"You will be "<<weight*1.06<<"kg\n";

break;

case 6 :

std::cout<<"You will be "<<weight*0.92<<"kg\n";

break;

case 7 :

std::cout<<"You will be "<<weight*1.19<<"kg\n";

break;

default :

std::cout<<"invalid!\n";

break;

}

}