 # 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…

1 Like

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:
‘name’

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

You get:
‘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!

2 Likes

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

}

}

1 Like

Here’s the code I used:

#include

int main() {

double eweight;
std::cout << “What is your earth weight?”;
std::cin >> eweight;
std::cout << "My earth weight is " << eweight << “.\n”;

int planet;
double pweight;
double mweight = 0.38;
double vweight = 0.91;
double maweight = 0.38;
double jweight = 2.34;
double sweight = 1.06;
double uweight = 0.92;
double nweight = 1.19;
std::cout << "Enter a number for the planet you want to fight on: ";
switch (planet) {
case 1:
std::cin >> “Mercury”;
pweight = eweight * mweight;
std::cout << pweight;
std::cout << “.\n”;
break;
case 2:
std::cin >> “Venus”;
pweight = eweight * vweight;
std::cout << pweight;
std::cout << “.\n”;
break;
case 3:
std::cin >> “Mars”;
pweight = eweight * maweight;
std::cout << pweight;
std::cout << “.\n”;
break;
case 4:
std::cin >> “Jupiter”;
pweight = eweight * jweight;
std::cout << pweight;
std::cout << “.\n”;
break;
case 5:
std::cin >> “Saturn”;
pweight = eweight * sweight;
std::cout << pweight;
std::cout << “.\n”;
break;
case 6:
std::cin >> “Uranus”;
pweight = eweight * uweight;
std::cout << pweight;
std::cout << “.\n”;
break;
case 7:
std::cin >> “Neptune”;
pweight = eweight * nweight;
std::cout << pweight;
std::cout << “.\n”;
break;
}
std::cin >> planet;

}

rg is simply the name of the variable that they have made. You can name your variables anything you want!

Hi there
i am a newbie to programming, so thanks in advance for your patience. I would be grateful for some help. I was feeling quite proud to have got something that seems to work for the interplanetary fighting problem. my issue is i don’t understand why the output always seems to add a dollar sign after the answer. code is copied below so thanks for any answers.
Tx
Si

#include

int main() {

double earthweight = 0;

std::cout << "What is your earth weight? ";

std::cin >> earthweight;

int planetnumber = 0;

std::cin >> planetnumber;

switch (planetnumber) {

``````case 1:

std::cout << earthweight * 0.38;

break;

case 2:

std::cout << earthweight * 0.91;

break;

case 3:

std::cout << earthweight * 0.38;

break;

case 4:

std::cout << earthweight * 2.34;

break;

case 5:

std::cout << earthweight * 1.06;

break;

case 6:

std::cout << earthweight * 0.92;

break;

case 7:

std::cout << earthweight * 1.19;

break;

default:

std::cout << "Sorry, we don't have a weight for this planet ";

break;
``````

}

}

Hello, @simplysi3724934560, and welcome to the forums.

The `\$` is the prompt for the console. If you don’t want it to show up on the same line as your output, you have a couple of options:

``````std::cout << earthweight * 0.38 << std::endl; //std::endl moves the cursor to the next line
//or
std::cout << earthweight * 0.38 << "\n"; // \n will also move the cursor to the next line
``````

Thanks @midlindner, much appreciated both for the reply and the welcome!
Best
Simon

1 Like

I was writing my code ( probably inefficient) but it keeps outputting dollar signs and the end of the number, why is that?

#include

int main() {

double (weight);

std::cout << “What is your earth weight?\n”;

std::cin >> weight;

int (planet);

std::cout << “What planet in 1-7?\n”;

std::cin >> planet;

switch (planet){

case 1:

std::cout << (weight * 0.35);

break;

case 2:

std::cout << (weight * 0.91);

break;

case 3:

std::cout << (weight * 0.38);

break;

case 4:

std::cout << (weight * 2.34);

break;

case 5:

std::cout << (weight * 1.06);

break;

case 6:

std::cout << (weight * 0.92);

break;

case 7:

std::cout << (weight * 1.19);

break;

}

}

I see no dollar signs outputted.

Not sure how you got that far since you have errors. You have not `include` anything so all your `cin` and `cout` will not work without `iostream`. Once you have done so your program will not work if you choose anything that is not 1 through 7. It will at best abort an at worse crash. However, if you add the correct include and put in a number in the range of 1 to 7 you will get results. This is what I got by inputting 100 as the weight and 1 as the planet.

``````100
What planet in 1 - 7 ?
1
35
``````

I would suggest you take a look at your `cout` messages if what you see outputted is not desirable.

Here is the code I’ve written for my program, please let me know if I can make any sort of improvement! Any constructive criticism is welcomed!

``````#include <iostream>

int main() {
double weight;
int number;

std::cout << "Enter your earth weight: "; "\n\n";
std:: cin >> weight;

std::cout << "Following is the list of available planets: \n\n" << " 1. Mercury\n 2. Venus\n 3. Mars\n 4. Jupiter\n 5. Saturn\n 6. Uranus\n 7. Neptune\n\n";

std::cout << "Enter a number to choose a planet you want to fight on: ";
std::cin >> number;
switch (number) {
case 1:
weight = weight*0.38;
std::cout << "The planet chosen is Mercury and your expected weight is " << weight << " kg.\n";
break;
case 2:
weight = weight*0.91;
std::cout << "The planet chosen is Venus and your expected weight is " << weight << " kg.\n";
break;
case 3:
weight = weight*0.38;
std::cout << "The planet chosen is Mars and your expected weight is " << weight << " kg.\n";
break;
case 4:
weight = weight*2.34;
std::cout << "The planet chosen is Jupiter and your expected weight is " << weight << " kg.\n";
break;
case 5:
weight = weight*1.19;
std::cout << "The planet chosen is Saturn and your expected weight is " << weight << " kg.\n";
break;
case 6:
weight = weight*0.92;
std::cout << "The planet chosen is Uranus and your expected weight is " << weight << " kg.\n";
break;
case 7:
weight = weight*1.19;
std::cout << "The planet chosen is Neptune and your expected weight is " << weight << " kg.\n";
break;
default:
std::cout << "Invalid\n";
break;
}
system("pause");

}
``````

Feel free to express your opinions! 