There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
I did the optional exercise for the review section.
I used a switch statement to calculate the weight on different planets, as seen below(and it works). However when I want to add the statement "your weight in this planet is " std::cout << "Your weight in this planet is " +(weight * rg2); I get an error.
The error is: "invalid operand of char + binary operator
Why is that?
int main() {
double weight;
std::cout << "What is your earth weight\n";
std::cin >> weight;
int planet;
std::cout << "Enter number (1-6) for planet to fight on\n";
std::cin >> planet;
switch (planet){
case 1:
{double rg1 = 0.78;
std::cout << (weight * rg1);}
break;
case 2:
{double rg2 = 0.39;
std::cout << (weight * rg2);}
break;
case 3:
{double rg3 = 2.65;
std::cout << (weight * rg3);}
break;
case 4:
{double rg4 = 1.17;
std::cout << (weight * rg4);}
break;
case 5:
{double rg5 = 1.05;
std::cout << (weight * rg5);}
break;
case 6:
{double rg6 = 1.23;
std::cout << (weight *rg6);}
break;
}
Solved
I got it!!!
I was concatenating using “+” instead of " <<" for the standard output!!!
Now my output is how I wanted it “your weight on this planet is ***”
It took me way longer than it should have to figure out why declaring a string failed when compiling…
Strings were mentioned in the earlier lessons, but no examples or details on how to use them were given.
After some googling I found out you have to have “using namespace std;” or use std::string instead of just string when declaring.
Once I finally figured that out, this was my final program.
#include <iostream>
using namespace std;
int main() {
double earthWeight;
int planetNumber;
double planetWeight;
string planetName;
std::cout << "Please enter your weight on Earth: ";
std::cin >> earthWeight;
std::cout << "Please select a planet from the following list:\n\n";
std::cout << " 1: Venus\n";
std::cout << " 2: Mars\n";
std::cout << " 3: Jupiter\n";
std::cout << " 4: Saturn\n";
std::cout << " 5: Uranus\n";
std::cout << " 6: Neptune\n\n";
std::cout << "Planet Number: ";
std::cin >> planetNumber;
switch (planetNumber){
case 1 :
planetWeight = earthWeight * 0.78;
planetName = "Venus";
break;
case 2 :
planetWeight = earthWeight * 0.39;
planetName = "Mars";
break;
case 3 :
planetWeight = earthWeight * 2.65;
planetName = "Jupiter";
break;
case 4 :
planetWeight = earthWeight * 1.17;
planetName = "Saturn";
break;
case 5 :
planetWeight = earthWeight * 1.05;
planetName = "Uranus";
break;
case 6 :
planetWeight = earthWeight * 1.23;
planetName = "Neptune";
break;
default :
planetName = "Unknown";
break;
}
if (planetName == "Unknown"){
std::cout << "Invalid planet input.\n";
}else{
std::cout << "Your weight on " << planetName << " would be " << planetWeight << "\n";
}
}
int main() {
double w = 150;
int p = 8;
std::cout << “What is your earth weight?\n”;
std::cin >> w;
std::cout << “Which plant number will you fight on?\n”;
std::cout << “1. Venus 2. Mars 3. Jupiter 4. Saturn 5. Uranus 6. Neptune \n\n”;
std::cin >> p;
switch § {
case 1 :
std::cout << "Your weight is "<< w0.78 <<“lbs,\n”;
break;
case 2 :
std::cout << "Your weight is " << w0.39 <<“lbs,\n”;
break;
case 3 :
std::cout << "Your weight is " << w2.65 <<“lbs,\n”;
break;
case 4 :
std::cout << "Your weight is "<< w1.17 <<“lbs,\n”;
break;
default :
std::cout << "Your weight is "<< w*1.05 <<“lbs,\n”;
break;
}
Tried to do a fancy work… but I failed…
Here is my code: #include
int main() {
double EW;
int num;
cart PN;
double FW;
wait
//Variables I defined
std::cout << “Hello warrior!\n”;
std::cout << “This is a programme to help you with calculate the weight of you in different planet only if you answer the following question.\n”;
//Greatings and intros.
std::cout << “1.What is your weight on EARTH?\n”;
std::cin >> EW;
if (EW <= 0){
std::cout >> “Please renter your weight.\n”;
return;
//In case someone enter a impossible number of weight.
} else {
std::cout >> “Great!”;
}
std::cout << “2.Which planet you want to fight on? Please enter the number of the planet. Here is the number of each planet:\n\n”;
std::cout << " 1. Venus 2. Mars 3. Jupiter\n";
std::cout << " 4. Saturn 5. Uranus 6. Neptune\n\n";
//A tip.
std::cin >> num;
//Enter the number of planet.
switch (num) {
case 1:
FW == EW* 0.78;
PN == “Venus”;
case 2:
FW == EW* 0.39;
PN == “Mars”;
case 3:
FW == EW* 2.56;
PM == “Jupiter”;
case 4:
FW == EW* 1.17;
PN == “Saturn”;
case 5:
FW == EW* 1.05;
PN == “Uranus”;
case 6:
FW == EW* 1.23;
PN == “Neptune”;
default:
std::cout << “We only know about thouse 6 planets right now.”;
return;
//In case user enter a number which is not 1, 2, 3, 4, 5, or 6.
}
std::cout << “So your wight on”;
std::cout >> PN;
std::cout << “is:”;
std::cout >> FW;
std::cout << “\n THX for using this programme!GH & HF!”;
//Final statement
}
I would appreciate if someone can help me!
Compiled and executed using
g++ space.cpp
./a.out
And typed the following code, however the code didn’t run and code academy let me pass?
Any help would be greatly appreciated:
int main() {
double eweight;
std::cout <<“What is your earth weight?\n”;
std::cout <<"My earth weight is ";
std::cin >> eweight;
int num;
std::cout <<“What number planet do you want to fight on?\n”;
/*#, Planet, Relative gravity
1 Venus 0.78
2 Mars 0.39
3 Jupiter 2.65
4 Saturn 1.17
5 Uranus 1.05
6 Neptue 1.23
*/
std::cin >> num;
double pweight;
switch(num){
case 1:
pweight=eweight * 0.78;
break;
case 2:
pweight=eweight * 0.39;
break;
case 3:
pweight=eweight * 2.65;
break;
case 4:
pweight=eweight * 1.17;
break;
case 5:
pweight=eweight * 1.05;
break;
case 6:
pweight=eweight * 1.23;
break;
default:
std::cout <<“Invalid number please try again\n”;
}
std::cout <<“Your planet weight is “<<pweight<<”\n”;
Since the main function is a interger(int). Therefore you must return a interger. So imagine if the main function was a string (int main > string main). You have to return a string like
return "hello"
or if it was a bool function (int main > bool main). Then the return would be either true or false
I assumed that he already knew what planet was which based off the number. This is what i did
#include <iostream>
using namespace std;
// W = M x G
int main() {
float earth_weight, mass, gravity, weight;
short planet;
cout<<"What is your earth weight?\n";
cin>> earth_weight;
cout<<"Enter the number of the planet that you are fighting in? \n";
cin>>planet;
//calculating his mass from his earth weight
mass = earth_weight/0.98;
//calculating his weight on the planet he inputed
switch(planet){
case 1 :
weight = mass * 0.78;
break;
case 2:
weight = mass * 0.39;
break;
case 3:
weight = mass * 2.65;
break;
case 4:
weight = mass * 1.17;
break;
case 5:
weight = mass * 1.05;
break;
case 6:
weight = mass * 1.23;
break;
}
cout<<"Your weight on planet #"<<planet<<"is "<<weight<<endl;
}
Hi Netpro,
Thanks for sharing your work! Looks neat.
Yeah, I encountered the same problem previously, and learnt that “string” is not a variable that is really at the bottom of the heart of C++ like char, int, double, etc. Rather, string is within std, the standard library, thus it is necessary to add the “std::” bit. I read it’s because string is essentially a ‘string’ of characters (chars).
Anyway just wanted to add few words from my recent learning as well. Happy coding.
//This is an app that converts one's earth weight to a chosen planet weight
#include <iostream>
int main (){
int planet_num, whilecheck=0, ifcheck=0;
double earth_weight, venus=0.78, mars=0.39, jupiter=2.65, saturn=1.17, uranus=1.05, neptune=1.23;
std::cout << "What is your weight on Earth?: ";
std::cin >> earth_weight;
// This while loop checks the earth_weight input and make sure its not negative. Giving 5 chances.
while (earth_weight < 0 && whilecheck < 5) {
std::cout << "You can't have negative value here. Please re-enter your weight : ";
std::cin >> earth_weight;
whilecheck = whilecheck +1;
}
// If the user has put in a negative value for more than 5 times, the code is forced to break, by returning 0.
if (whilecheck > 4) {
std::cout << "Stop putting in negatvie number!\n";
return 0;
}
std::cout << "Also, on what planet do you want to fight?\n" << "1-Venus, 2-Mars, 3-Jupiter, 4-Saturn, 5-Uranus, 6-Neptune: ";
std::cin >> planet_num;
// Similar input validity check
while (planet_num != 1 && planet_num != 2 && planet_num != 3 && planet_num != 4 && planet_num != 5 && planet_num !=6 && ifcheck < 5) {
std::cout << "Please choose a valid option : ";
std::cin >> planet_num;
ifcheck = ifcheck +1;
}
if (whilecheck > 4 || ifcheck >4) {
std::cout << "Stop putting in invalid inputs!\n";
return 0;
}
switch(planet_num){
case 1:
std::cout << "You have chosen Venus! " << "You will weigh " << earth_weight*venus << "kg there!\n";
break;
case 2:
std::cout << "You have chosen Mars! " << "You will weigh " << earth_weight*mars << "kg there!\n";
break;
case 3:
std::cout << "You have chosen Jupiter! " << "You will weigh " << earth_weight*jupiter << "kg there!\n";
break;
case 4:
std::cout << "You have chosen Saturn! " << "You will weigh " << earth_weight*saturn << "kg there!\n";
break;
case 5:
std::cout << "You have chosen Uranus! " << "You will weigh " << earth_weight*uranus << "kg there!\n";
break;
case 6:
std::cout << "You have chosen Neptune! " << "You will weigh " << earth_weight*neptune << "kg there!\n";
break;
default: //default does not have 'break' as it really doesn't matter. but it may be a good practise to include one?
std::cout << "Sorry, something went wrong... Stay on the Earth maybe...\n";
}
}
#include <algorithm>
#include <locale>
#include <iostream>
using namespace std;
int main() {
//It should ask him what his earth weight is.
cout << "How much do you weigh on Earth?\n";
double weight;
cin >> weight;
//Ask him to enter a the planet he wants to fight on.
string planet;
cout << "Which planet do you want to fight on?\n";
cin >> planet;
// convert string to back to lower case
for_each(planet.begin(), planet.end(), [](char & c) {
c = tolower(c);
});
//It should then compute his weight on the destination planet.
if (planet == "venus")
weight *= 0.78;
else if (planet == "mars")
weight *= 0.39;
else if (planet == "jupiter")
weight *= 2.65;
else if (planet == "saturn")
weight *= 1.17;
else
cout << "Planet unknown.\n";
cout << "Your weight will be " << weight << ".\n";
}
I struggled a little at the start with the switch, so i copied my code into a notebook and tried re-writing with if statements. Which did work, but i was determined not to move on in the lessons until i figured what i was doing wrong with the switch statements, in the end it was me putting the wrong variables into the switch statements… so here’s the final code i did, and it works!
#include <iostream>
int main() {
double earth;
int planet;
std::cout << "Please enter your earth weight: ";
std::cin >> earth;
std::cout << "Please Chose Your Planet You Want To Visit!\n";
std::cout << "1.Venus 2.Mars 3.Jupiter 4.Saturn 5.Uranus 6.Neptune\n";
std::cin >> planet;
switch (planet) {
case 1:
earth = planet * 0.78;
break;
case 2:
earth = planet * 0.39;
break;
case 3:
earth = planet * 2.65;
break;
case 4:
earth = planet * 1.17;
break;
case 5:
earth = planet * 1.05;
break;
case 6:
earth = planet * 1.23;
break;
default:
std::cout << "Invalid\n";
break;
}
std::cout << "Your weight will be: " << earth << "\n";
}