if (condition) {
...
} else if (condition) {
...
}
Using the if… else if, write a menu-driven program that permits the user to select one of the metric conversions: Lengths Weights Volumes Areas Use the following conversions:
For Length: Inches to centimeter (1 inch = 2.54 cm) Yards to meters (1 yard = 0.9144 m) Miles to kilometers (1 mile = 1.609344 km)
For Weights: Ounces to grams (1 ounce = 28.349523 g) Pounds to kilograms (1 pound = 0.453592 kg) Tons to kilograms (1 ton = 907.18474 kg)
For Volumes: Pints to liters (1 pint = 0.473163 l) Quarts to liters (1 quart = 0.946326 l) Gallons to liters (1 gallon = 3.785306 l)
For Areas: Square inches to square millimeters (1 sq. in. = 645.16 sq.mm) Square feet to square meters (1 sq. foot = 0.09290304 sq.m) Acres to square meters (1 Acre = 4.04686x10^3 sq.m)
The question is how can I write a Programming using if ....else if.?
2 answers
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int Length= 1, Weight= 2, Volume=3;
int cm, m, km, mm2, m2, pt, qt, gal,g, kg;
int Choice;
long double inch= 2.54, yard= 0.9144, mile = 1.609344;
long double ounce= 28.349523, pound= 0.453592, ton = 907.18474;
long double pints= 0.473163, quart= 0.946326, gallon = 3.785306;
long double in2= 645.16, ft2= 0.09290304, Acre = (4.04686e3);
double a, b, c;
char MS= 'x';
cout<<"Metric System"<<endl;
cout<<"(L) Length"<<endl;
cout<<"(W) Weight"<<endl;
cout<<"(V) Volume"<<endl;
cout<<"(A) Area"<<endl;
cout<<"Which Metric System will you use? ";
cin>> MS;
Length= 'L'==MS;
Weight= 'W'==MS;
Volume= 'V'==MS;
if (Length)
{cout<<"LENGTHS"<<endl;cout<<"1) Iches to Centimeter"<<endl;
cout<<"2) Yards to Meters"<<endl;
cout<<"3) Miles to Kilometers"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Inches to Centimeters"<<endl;
cout<<"Enter value in inches: "; cin>>cm;cout<<endl;
a=cm*inch;
cout<<cm<<" Inches = "<<a<<" Centimeters"<<endl;
}
else if (Choice==2)
{cout<<"Convert Yards to Meters"<<endl;
cout<<"Enter value in yards: ";cin>>m;cout<<endl;
b=m*yard;
cout<<m<<" yards = "<<b<<" meters"<<endl;
}
else
{cout<<"Convert Miles to Kilometers"<<endl;
cout<<"Enter value in miles: ";cin>>km;
c=km*mile;
cout<<km<<" mile = "<<c<<" kilometers"<<endl;
}
}
else if (Weight)
{cout<<"WEIGHTS"<<endl;
cout<<"1) Ounces to Grams"<<endl;
cout<<"2) Pounds to Kilograms"<<endl;
cout<<"3) Tons to Kilograms"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Ounces to Grams"<<endl;
cout<<"Enter value in ounces: "; cin>>g; cout<<endl;
a=g*ounce;
cout<<g<<" ounces = "<<a<<" grams"<<endl;
}
else if (Choice==2)
{cout<<"Convert Pounds to Kilograms"<<endl;
cout<<"Enter value in pounds ";cin>>kg; cout<<endl;
b=kg*pound;
cout<<kg<<" pounds = "<<b<<" Kilograms"<<endl;
}
else
{cout<<"Convert Tons to Kilograms"<<endl;
cout<<"Enter value in tons: "; cin>>kg; cout<<endl;
c=kg*ton;
cout<<kg<<" tons = "<<c<<" Kilograms"<<endl;
}
}
else if (Volume)
{cout<<"VOLUMES"<<endl;
cout<<"1) Pints to Liters"<<endl;
cout<<"2) Quarts to Liters"<<endl;
cout<<"3) Gallons to Liters"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Pints to Liters"<<endl;
cout<<"Enter value in pints: "; cin>>pt; cout<<endl;
a=pt*pints;
cout<<pt<<" pints = "<<a<<" Liters"<<endl;
}
else if (Choice==2)
{cout<<"Convert Quarts to Liters"<<endl;
cout<<"Enter value in quarts: ";cin>>qt; cout<<endl;
b=qt*quart;
cout<<qt<<" quarts = "<<b<<" liters"<<endl;
}
else
{cout<<"Convert Gallons to Liters"<<endl;
cout<<"Enter value in Gallons: ";cin>>gal; cout<<endl;
c=gal*gallon;
cout<<gal<<" gallons = "<<c<<" liters"<<endl;
}
}
else
{cout<<"AREAS"<<endl;
cout<<"1) Square inches to square millimeters"<<endl;
cout<<"2) Square feet to square meters"<<endl;
cout<<"3) Acres to square meters"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Square inches to square millimeters"<<endl;
cout<<"Enter value in Square inches: "; cin>>mm2; cout<<endl;
a=mm2*in2;
cout<<mm2<<" Square Inches = "<<a<<" Square millimeters"<<endl;
}
else if (Choice==2)
{cout<<"Convert Square feet to square meters"<<endl;
cout<<"Enter value in Square feet: ";cin>>m2;cout<<endl;
b=m2*ft2;
cout<<m2<<" square feet = "<<b<<" square meters"<<endl;
}
else
{cout<<"Convert Acres to square meters"<<endl;
cout<<"Enter value in Acres: ";cin>>m2;
c=m2*Acre;
cout<<m2<<" acres = "<<c<<" square meters"<<endl;
}
}
return 0;
}
#include <cmath>
using namespace std;
int main ()
{
int Length= 1, Weight= 2, Volume=3;
int cm, m, km, mm2, m2, pt, qt, gal,g, kg;
int Choice;
long double inch= 2.54, yard= 0.9144, mile = 1.609344;
long double ounce= 28.349523, pound= 0.453592, ton = 907.18474;
long double pints= 0.473163, quart= 0.946326, gallon = 3.785306;
long double in2= 645.16, ft2= 0.09290304, Acre = (4.04686e3);
double a, b, c;
char MS= 'x';
cout<<"Metric System"<<endl;
cout<<"(L) Length"<<endl;
cout<<"(W) Weight"<<endl;
cout<<"(V) Volume"<<endl;
cout<<"(A) Area"<<endl;
cout<<"Which Metric System will you use? ";
cin>> MS;
Length= 'L'==MS;
Weight= 'W'==MS;
Volume= 'V'==MS;
if (Length)
{cout<<"LENGTHS"<<endl;cout<<"1) Iches to Centimeter"<<endl;
cout<<"2) Yards to Meters"<<endl;
cout<<"3) Miles to Kilometers"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Inches to Centimeters"<<endl;
cout<<"Enter value in inches: "; cin>>cm;cout<<endl;
a=cm*inch;
cout<<cm<<" Inches = "<<a<<" Centimeters"<<endl;
}
else if (Choice==2)
{cout<<"Convert Yards to Meters"<<endl;
cout<<"Enter value in yards: ";cin>>m;cout<<endl;
b=m*yard;
cout<<m<<" yards = "<<b<<" meters"<<endl;
}
else
{cout<<"Convert Miles to Kilometers"<<endl;
cout<<"Enter value in miles: ";cin>>km;
c=km*mile;
cout<<km<<" mile = "<<c<<" kilometers"<<endl;
}
}
else if (Weight)
{cout<<"WEIGHTS"<<endl;
cout<<"1) Ounces to Grams"<<endl;
cout<<"2) Pounds to Kilograms"<<endl;
cout<<"3) Tons to Kilograms"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Ounces to Grams"<<endl;
cout<<"Enter value in ounces: "; cin>>g; cout<<endl;
a=g*ounce;
cout<<g<<" ounces = "<<a<<" grams"<<endl;
}
else if (Choice==2)
{cout<<"Convert Pounds to Kilograms"<<endl;
cout<<"Enter value in pounds ";cin>>kg; cout<<endl;
b=kg*pound;
cout<<kg<<" pounds = "<<b<<" Kilograms"<<endl;
}
else
{cout<<"Convert Tons to Kilograms"<<endl;
cout<<"Enter value in tons: "; cin>>kg; cout<<endl;
c=kg*ton;
cout<<kg<<" tons = "<<c<<" Kilograms"<<endl;
}
}
else if (Volume)
{cout<<"VOLUMES"<<endl;
cout<<"1) Pints to Liters"<<endl;
cout<<"2) Quarts to Liters"<<endl;
cout<<"3) Gallons to Liters"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Pints to Liters"<<endl;
cout<<"Enter value in pints: "; cin>>pt; cout<<endl;
a=pt*pints;
cout<<pt<<" pints = "<<a<<" Liters"<<endl;
}
else if (Choice==2)
{cout<<"Convert Quarts to Liters"<<endl;
cout<<"Enter value in quarts: ";cin>>qt; cout<<endl;
b=qt*quart;
cout<<qt<<" quarts = "<<b<<" liters"<<endl;
}
else
{cout<<"Convert Gallons to Liters"<<endl;
cout<<"Enter value in Gallons: ";cin>>gal; cout<<endl;
c=gal*gallon;
cout<<gal<<" gallons = "<<c<<" liters"<<endl;
}
}
else
{cout<<"AREAS"<<endl;
cout<<"1) Square inches to square millimeters"<<endl;
cout<<"2) Square feet to square meters"<<endl;
cout<<"3) Acres to square meters"<<endl;
cout<<"Enter your choice: ";
cin>>Choice;
cout<<endl;
if (Choice==1)
{cout<<"Convert Square inches to square millimeters"<<endl;
cout<<"Enter value in Square inches: "; cin>>mm2; cout<<endl;
a=mm2*in2;
cout<<mm2<<" Square Inches = "<<a<<" Square millimeters"<<endl;
}
else if (Choice==2)
{cout<<"Convert Square feet to square meters"<<endl;
cout<<"Enter value in Square feet: ";cin>>m2;cout<<endl;
b=m2*ft2;
cout<<m2<<" square feet = "<<b<<" square meters"<<endl;
}
else
{cout<<"Convert Acres to square meters"<<endl;
cout<<"Enter value in Acres: ";cin>>m2;
c=m2*Acre;
cout<<m2<<" acres = "<<c<<" square meters"<<endl;
}
}
return 0;
}