Write a C++ program that asks the user to enter his or her weight and the name o
ID: 3606285 • Letter: W
Question
Write a C++ program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. The program should output an error message if the user doesn't type a correct planet name. The prompt and the error message should make it clear to the user how a planet name must be entered. Be sure to use proper formatting and appropriate comments in your code. The output should be labeled clearly and formatted neatly. [100 Points] 2. Mercury Venus Earth Moon Mars Jupiter Saturn Uranus Neptune 0.4155 0.8975 1.0 0.166 0.3507 2.5374 1.0677 0.8947 1.1794 0.0899 PlutoExplanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
#include<map>
using namespace std;
int main()
{
std::string Planet_name;
double weight,final_weight;
std::map<string,double> mymap; //creating Map of key value pair with key as planet name and value as weight factor
std::map<string,double>::iterator it; //iterator for map
//creating map with Name and weight factor
mymap.insert(std::pair<string, double>("Mercury", 0.4155));
mymap.insert(std::pair<string, double>("Venus", 0.8975));
mymap.insert(std::pair<string, double>("Earth", 1.0));
mymap.insert(std::pair<string, double>("Moon", 0.166));
mymap.insert(std::pair<string, double>("Mars", 0.3507));
mymap.insert(std::pair<string, double>("Jupiter", 2.5374));
mymap.insert(std::pair<string, double>("Saturn", 1.0677));
mymap.insert(std::pair<string, double>("Uranus", 0.8947));
mymap.insert(std::pair<string, double>("Neptune", 1.1794));
mymap.insert(std::pair<string, double>("Pluto", 0.0899));
//Asking user input
cout<<"Please enter weight in Kg: ";
cin>>weight;
//do while() loop for asking user to enter planet name correctly
do{
cout<<"Choose amongst below list "<<endl;
cout<<"Mercury Venus Earth Moon Mars Jupiter Saturn Uranus Neptune Pluto "<<endl;
cout<<"Please enter Planet Name : ";
cin>>Planet_name;
it = mymap.find(Planet_name); //finding planet name in map
if (it != mymap.end()) //if string is in map
{
final_weight = it->second * weight; //multiplying weight with factor
cout<<"Weight on "<<Planet_name<<" is "<<final_weight<<" Kg"<<endl;
break;
}
else //repeating do while() loop
{
cout<<"Incorrect Planet name "<<endl;
}
}while(1);
return 0;
}
PLEASE REFER BELOW OUTPUT
khushal@khushal-ubuntu:~/Desktop/Chegg$ g++ -g Planet.cpp
khushal@khushal-ubuntu:~/Desktop/Chegg$ ./a.out
Please enter weight in Kg: 67.8
Choose amongst below list
Mercury
Venus
Earth
Moon
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto
Please enter Planet Name : neptune
Incorrect Planet name
Choose amongst below list
Mercury
Venus
Earth
Moon
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto
Please enter Planet Name : Neptune
Weight on Neptune is 79.9633 Kg
khushal@khushal-ubuntu:~/Desktop/Chegg$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.