Using the the following program as an example, create a menu driven program that
ID: 653676 • Letter: U
Question
Using the the following program as an example, create a menu driven program that will do at least 5 metric conversions. You can do inches to centimeters, kilos to pounds, etc. //A menu driven program to find area of shapes #include <iostream> using namespace std; //function prototypes void circle(void); //finds area of circle void square(void); //finds area of square void rectangle(void); //finds area of rectangle void menu(void); //displays choices void doChoice(char choice); //calls function selected void main(void) //display menu and call function selected { char choice; do { menu(); //display choices cin>>choice; doChoice(choice); //calls function selected } while ((choice != 'Q') && (choice !='q')); system("pause"); } //main void doChoice(char choice) //calls function selected { switch (choice) { case 'C': case 'c': circle(); break; case 'R': case 'r': rectangle(); break; case 'S': case 's': square(); break; } //choice } //doChoice void menu(void) //display the choices { cout<<"This will find the area of a shape "; cout<<"Enter the letter of your choice: "; cout<<"C: Circle "; cout<<"R: Rectangle "; cout<<"S: Square "; cout<<"Q: Quit "; cout<<"Please enter your choice:"; } //menu void circle(void) //find area of circle from radius { double radius, area; cout<<"Enter the radius of the circle:"; cin>>radius; area = radius*radius*3.14158; cout<<"The area is "<<area<<" "; } //circle void rectangle(void) //find area of rectangle from length and width { double length, width, area; cout<<"Enter the width of the rectangle:"; cin>>width; cout<<"Enter the length of the rectangle:"; cin>>length; area = width * length; cout<<"The area is "<<area<<" "; } //square void square(void) //find area of square from side { double side, area; cout<<"Enter the side of the square:"; cin>>side; area = side * side; cout<<"The area is "<<area<<" "; } //square
Explanation / Answer
#include<iostream>
002
003
004 using namespace std;
005
006
007 main()
008 {
009
010 int pick_op;
011 double mm,cm,m,km,in,ft,yd,mi;
012 long double mm1,cm1,m1,km1,in1,ft1,yd1,mi1;
013
014 cout<<" METRIC AND ENGLISH CONVERSION ";
015 cout<<" by frank bautista ";
016 cout<<"------------------------------- ";
017 cout<<" CONVERT ";
018 cout<<"------------------------------- ";
019 cout<<" Please Choose Number ";
020 cout<<" [1]----millimeter (mm) ";
021 cout<<" [2]----centimeter (cm) ";
022 cout<<" [3]----meter (m) ";
023 cout<<" [4]----kilometer (km) ";
024 cout<<" [5]----inches (in) ";
025 cout<<" [6]----feet (ft) ";
026 cout<<" [7]----yard (yd) ";
027 cout<<" [8]----mile (mi) ";
028 cout<<" [9]----EXIT ";
029 cout<<" Note: Large values may display with exponent. ";
030 cout<<" CHOICE: ";
031
032 cin>>pick_op;
033 cin.ignore();
034
035
036 switch (pick_op) {
037 //if the user choose menu 1.
038 case 1: //MILLIMETER
039 cout<<" enter MILLIMETER VALUE (mm): ";
040 cin>>mm;
041 cin.ignore();
042 cout<<" ";
043
044 //conversion
045 cm1=mm/10;
046 m1=mm/1000;
047 in1=mm/25.4;
048 ft1=mm/304.8;
049 yd1=mm/914.4;
050
051 //output display
052 system("cls");
053 cout<<"RESULT: ";
054 cout<<mm<<"mm = ";
055 cout<<"------------------------------------------------------------ ";
056 cout<<cm1<<"cm "<<m1<<"m "<<in1<<"in "<<ft1<<"ft "<<yd1<<"yd ";
057 return main();
058 //if the user choose menu 2.
059 case 2: //CENTIMETER
060 cout<<" enter CENTIMETER VALUE (cm): ";
061 cin>>cm;
062 cin.ignore();
063 cout<<" ";
064
065 //conversions
066 mm1=cm*10;
067 m1=cm/100;
068 in1=cm/2.54;
069 ft1=cm/30.48;
070 yd1=cm/.01094;
071
072 //output display
073 system("cls");
074 cout<<"RESULT: ";
075 cout<<cm<<"cm = ";
076 cout<<"------------------------------------------------------------- ";
077 cout<<mm1<<"mm "<<m1<<"m "<<in1<<"in "<<ft1<<"ft "<<yd1<<"yd ";
078 return main();
079 //if the user choose menu 3.
080 case 3: //METERS
081 cout<<" enter METER VALUE (m): ";
082 cin>>m;
083 cin.ignore();
084 cout<<" ";
085
086 //conversions
087 mm1=m*1000;
088 cm1=m*100;
089 mi1=m/.0006;
090 km1=m/1000;
091 in1=m/39.37;
092 ft1=m/3.281;
093 yd1=m/1.09;
094
095
096 //output display
097 system("cls");
098 cout<<"RESULT: ";
099 cout<<m<<"m = ";
100 cout<<"------------------------------------------------------------- ";
101 cout<<mm1<<"mm "<<cm1<<"cm "<<km1<<"km "<<in1<<"in "<<ft1<<"ft "<<mi1<<"mi "<<yd1<<"yd ";
102 return main();
103
104 //if the user choose menu 4.
105 case 4: //KILOMETER
106 cout<<" enter KILOMETER VALUE (km): ";
107 cin>>km;
108 cin.ignore();
109 cout<<" ";
110
111
112 //conversion starts here
113 m1=km*1000;
114 in1=km*39370.1;
115 ft1=km*3280.84;
116 yd1=km*1093.61;
117 mi1=km/.62137;
118
119 //output display
120 system("cls");
121 cout<<"RESULT: ";
122 cout<<km<<"km = ";
123 cout<<"------------------------------------------------------------- ";
124 cout<<m1<<"m "<<in1<<"in "<<mi1<<"mi "<<ft1<<"ft "<<yd1<<"yd ";
125 return main();
126 //if the user choose menu 5.
127 case 5: //INCHES
128 cout<<" enter INCHES VALUE (in): ";
129 cin>>in;
130 cin.ignore();
131 cout<<" ";
132
133
134 //conversion
135 mm1=in*25.4;
136 cm1=in*2.54;
137 m1=in/.0254;
138 ft1=in/.083;
139 yd1=in/.0278;
140
141 //output display
142 system("cls");
143 cout<<"RESULT: ";
144 cout<<in<<"in = ";
145 cout<<"------------------------------------------------------------- ";
146 cout<<mm1<<"mm "<<cm1<<"cm "<<m1<<"m "<<ft1<<"ft "<<yd1<<"yd ";
147 return main();
148 //if the user choose menu 6.
149 case 6: //FEET
150 cout<<" enter FEET VALUE (ft): ";
151 cin>>ft;
152 cin.ignore();
153 cout<<" ";
154
155
156 //conversion
157 mm1=ft*304.8;
158 cm1=ft*3048;
159 m1=ft/.3048;
160 km1=ft/304.8;
161 in1=ft*12;
162 mi1=ft/.00019;
163 yd1=ft/.333;
164
165 //output display
166 system("cls");
167 cout<<"RESULT: ";
168 cout<<ft<<"ft = ";
169 cout<<"------------------------------------------------------------- ";
170 cout<<mm1<<"mm "<<cm1<<"cm "<<in1<<"in "<<m1<<"m "<<km1<<"km "<<mi1<<"mi "<<yd1<<"yd ";
171 return main();
172 //if the user choose menu 7.
173 case 7: //YARD
174 cout<<" enter YARDS VALUE (yd): ";
175 cin>>yd;
176 cin.ignore();
177 cout<<" ";
178
179
180 //conversion
181 mm1=yd*914.4;
182 cm1=yd*91.44;
183 in1=yd*36;
184 m1=yd/.914;
185 km1=yd/914;
186 mi1=yd/.0006;
187 ft1=yd*3;
188
189 //output display
190 system("cls");
191 cout<<"RESULT: ";
192 cout<<yd<<"yd = ";
193 cout<<"------------------------------------------------------------- ";
194 cout<<mm1<<"mm "<<cm1<<"cm "<<m1<<"m "<<in1<<"in "<<km1<<"km "<<ft1<<"ft "<<mi1<<"mi ";
195 return main();
196 //if the user choose menu 8.
197 case 8: //MILES
198 cout<<" enter MILES VALUE (mi): ";
199 cin>>mi;
200 cin.ignore();
201 cout<<" ";
202
203
204 //conversion
205 km1=mi*1.609;
206 ft1=mi*5280;
207 m1=mi*1609.34;
208 yd1=mi*1760;
209
210 //output display
211 system("cls");
212 cout<<"RESULT: ";
213 cout<<mi<<"mi = ";
214 cout<<"------------------------------------------------------------- ";
215 cout<<m1<<"m "<<km1<<"km "<<ft1<<"ft "<<yd1<<"yd ";
216 return main();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.