Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Topic: switch statement C++ The force of gravity is different for each of the ni

ID: 3796237 • Letter: T

Question

Topic: switch statement C++

The force of gravity is different for each of the nine planets in our solar system. For example, on Mercury it is only 0.38 times as strong as on Earth. Thus, if you weigh 100 pounds on Earth, you would weigh only 38 pounds on Mercury. Write a program that allows you to enter your Earth weight and your choice of planet to which you would like your weight converted to. Calculate your weight on the chosen planet. Display your Earth weight, your weight on the desired planet to a tenth of a decimal point, and the name of the planet you chose. Design a menu for the planet choices.

The relative forces of gravity for each planet are:

Jupiter               2.65

Mars                  0.39

Mercury            0.38

Neptune            1.23

Pluto                  0.05

Saturn                1.17

Uranus               1.05

Venus                0.78

Sample Output:

Your weight on earth:                            152.5 lbs.

Planet Chosen:                                        Neptune

Your weight on the chosen planet:       187.6 lbs.

Would you like to calculate another weight (Y or N): y

This program converts your weight on Earth to your weight on the planet of your choice.

Please enter your weight on planet Earth: 165.5

Please enter the number that follows your planet of choice.

MERCURY:   1

VENUS:          2

MARS:            3

JUPITER:       4

SATURN:      5

URANUS:       6

NEPTUNE:     7

PLUTO:          8

Planet Number = ? 2

Your weight on earth:                            165.5 lbs.

Planet Chosen:                                        Venus

Your weight on the chosen planet:       129.1 lbs.

My teacher hasn't taught us how to use a switch statement for C++

Explanation / Answer

#include <iostream>
#include <stdio.h>

int main(int argc, char *argv[]) {
  
   float wt1,wt2; //for inputting and calculating weights
   int poc; //for planet of choice
   char again='y';
  
   std::cout << "This program converts your weight on Earth to your weight on the planet of your choice. ";
   while(again=='y' || again =='Y'){
  
   std::cout << "Please enter your weight on planet Earth:";  
   std::cin >> wt1;
  
   std::cout << "Please enter the number that follows your planet of choice. MERCURY: 1 VENUS: 2 MARS: 3 JUPITER: 4 SATURN: 5 URANUS: 6 NEPTUNE: 7 PLUTO: 8 ";
  

   std::cout << "Planet Number = ?";
  
   std::cin >> poc;   
  
   switch(poc){
       case 1:
           wt2=0.38*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Mercury ";
   printf("Your weight on the chosen planet: %.1f ",wt2);
           break;
      
       case 2:
           wt2=0.78*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Venus ";
   printf("Your weight on the chosen planet: %.1f ",wt2);      
           break;
          
       case 3:
           wt2=0.39*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Mars ";
   printf("Your weight on the chosen planet: %.1f ",wt2);      
           break;
          
       case 4:
           wt2=2.65*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Jupiter ";
   printf("Your weight on the chosen planet: %.1f ",wt2);      
           break;
          
       case 5:
           wt2=1.17*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Saturn ";
   printf("Your weight on the chosen planet: %.1f ",wt2);      
           break;
          
       case 6:
           wt2=1.05*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Uranus ";
   printf("Your weight on the chosen planet: %.1f ",wt2);
           break;
          
       case 7:
           wt2=1.23*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Neptune ";
   printf("Your weight on the chosen planet: %.1f ",wt2);          
           break;
          
       case 8:
           wt2=0.05*wt1;
           std::cout << "Your weight on earth: "<<wt1<<" ";
   std::cout << "Planet Chosen: Pluto ";
   printf("Your weight on the chosen planet: %.1f ",wt2);      
           break;
          
       default:
           std::cout << "Planet Number is invalid";
          
   }
  
   std::cout <<"Would you like to calculate another weight (Y or N):";
   std::cin >>again;
}
}