Site hosted by Angelfire.com: Build your free website today!

Program 16 

 


C++ Dzwonkiewicz

USING THE SWITCH STATEMENT

Write a program that will ask the user to input their weight on Earth.  It will present a menu as shown below and allow for a selection from the list.  It then will calculate your weight on the planet selected.

 

                                                Menu

1)       Mercury

2)       Venus

3)       Mars

4)       Jupiter

5)       Saturn

6)       Uranus

7)       Neptune

8)       Pluto

Select the number of the planet of your choice:___

 

Sample Output:

 

                Enter scale ((P) for pounds and (K) for kilograms)____P

               

                Please enter your weight:___212

 

                                Menu

1) Mercury

2) Venus

3) Mars

4) Jupiter

5) Saturn

6) Uranus

7) Neptune

8) Pluto

                Select the number of the

                Planet of your choice:___5

                                               

                                Your weight on Saturn is….243.8 ponds

 

The switch statement is similar to Pascal’s Case of … end.  It structures your logic so your choices are easier to categorize.  It looks like this…

                                cout<<”enter your age”<<endl;

                                cin>>age;                                             

                if (age<13) group=1;

                if (age>12 && age<21) group=2;

                if (age>20) group=3;

switch(group)

                {

                                case 1:    strcpy(status, “Juvenile”);

                                                break;

                                case 2:    strcpy(status, “Teenager”);

                                                break;

                                case 3:    strcpy(status, “Adult”);

                                                break;

                                default:   strcpy(status, “Bad Input”);

                }

The case operator must be an integer or a character.  Each choice must have a break else it drops to the next option and does it without ‘thinking.’  Your multipliers are (.37, .88, .38, 2.64, 1.15, 1.12, .04).  These are given here in the same order as listed in your menu.