1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/************************************************************************************
Purpose: The purpose of this program was to take the program from 4b and
add functions to convert the values entered into another type of measurement.
The program asks the user which type of conversion they would like to convert, and then converts it, finally outputting the final conversions to the screen.
************************************************************************************/
#include <iostream>
using namespace std;
void convert_lengths();
void convert_weights();
int round(double x);
//rounds off decimal places
void weight_to_metric();
double input_the_lbsandoz();
double convert_the_lbsandoz();
void output_the_kgandg();
//conversion functions for us to metric weight
void weight_to_us();
double input_the_kgandg();
double convert_the_kgandg();
void output_the_lbsandoz();
//conversion functions for metric to us weight
void length_to_metric();
double input_the_ftandin();
double convert_the_ftandin();
void output_the_mandcm();
//conversion functions for us to metric length
void length_to_us();
double input_the_mandcm();
double convert_the_msandcm();
void output_the_ftandin();
//conversion functions for metric to us length
int main() {
int ans;
do {
cout << "This program converts lengths and weights." << endl;
cout << "Press 1 to convert lengths." << endl;
cout << "Press 2 to convert weights." << endl;
cout << "Press 0 to end the program." << endl;
cout << "Enter: ";
cin >> ans;
if (ans == 1) {
convert_lengths();
}
else if (ans == 2) {
convert_weights();
}
else if (ans == 0)
cout << "Program terminated." << endl;
else
cout << "Please enter a vaild choice." << endl;
} while (ans > 2 || ans < 0);
return 0;
}
void convert_lengths() {
int answer;
do {
cout << endl;
cout << "You have chosen to convert lengths." << endl;
cout << "If you would like to convert ft./in. to m/cm press 1." << endl;
cout << "If you would like to convert m/cm to ft./in. press 2." << endl;
cout << "If you would like to stop converting lengths press 0." << endl;
cout << "Enter: ";
cin >> answer;
if (answer == 1)
length_to_metric(); //conversion function ft/in to m/cm
else if (answer == 2)
length_to_us(); //conversion function m/cm to ft/in
else if (answer == 0)
main();
else
cout << "Please enter a valid choice." << endl;
} while (answer < 0 || answer > 2);
}
void convert_weights() {
int ans;
do {
cout << endl;
cout << "If you would like to convert lbs./oz. to kg/g press 1." << endl;
cout << "If you would like to convert kg/g to lbs./oz. press 2." << endl;
cout << "If you would like to stop converting weights press 0." << endl;
cout << "Enter: ";
cin >> ans;
if (ans == 1)
weight_to_metric(); //weight to metric function
else if (ans == 2)
weight_to_us(); //weight to us function
else if (ans == 0)
main();
else
cout << "Please enter a valid choice." << endl;
} while (ans < 0 || ans > 2);
}
void length_to_metric() {
cout << "You have chosen to convert from us to metric lengths." << endl;
output_the_mandcm();
}
void length_to_us() {
cout << "You have chosen to convert from metric to us." << endl;
output_the_ftandin();
}
void weight_to_metric() {
cout << "You have chosen to convert from us to metric weights." << endl;
output_the_kgandg();
}
void weight_to_us() {
cout << "You have chosen to convert from metric to us." << endl;
output_the_lbsandoz();
}
double input_the_ftandin() {
int feet, inch;
double totalin;
cout << "Please enter the number of feet: ";
cin >> feet;
cout << "Please enter the number of inches: ";
cin >> inch;
totalin = inch + (feet * 12);
return totalin;
}
double convert_the_ftandin() {
double totalcm, totalmeters, totalin = input_the_ftandin();
totalcm = totalin * 2.54;
totalmeters = totalcm / 100;
return totalmeters;
}
void output_the_mandcm() {
double totalmeters = convert_the_ftandin(), totalcm;
totalcm = round(totalmeters * 100) % 100;
totalmeters = floor(totalmeters);
cout << "The total meters are: " << totalmeters << endl;
cout << "The total centimeters are: " << totalcm << endl;
}
double input_the_mandcm() {
double meters, cm;
cout << "Please enter the number of meters: ";
cin >> meters;
cout << "Please enter the number of cm: ";
cin >> cm;
meters = meters + (cm / 100);
return meters;
}
double convert_the_mandcm() {
double totalfeet, totalmeters = input_the_mandcm();
totalfeet = totalmeters / .3048;
return totalfeet;
}
void output_the_ftandin() {
double totalinches, totalfeet = convert_the_mandcm();
totalinches = totalfeet * 12;
totalinches = round(totalinches) % 12;
cout << "The total feet are: " << floor(totalfeet) << endl;
cout << "The total inches are: " << totalinches << endl;
}
double input_the_lbsandoz() {
double lbs, oz;
cout << "Please enter the number of lbs: ";
cin >> lbs;
cout << "Please enter the number of oz.: ";
cin >> oz;
lbs = lbs + (oz / 16);
return lbs;
}
double convert_the_lbsandoz() {
double totallbs = input_the_lbsandoz(), totalkg, totalgrams;
totalkg = totallbs / 2.2046;
totalgrams = totalkg * 1000;
return totalgrams;
}
void output_the_kgandg() {
double totalkg, totalgrams = convert_the_lbsandoz();
totalkg = floor(totalgrams / 1000);
totalgrams = round(totalgrams) % 1000;
cout << "The total kilograms are: " << totalkg << endl;
cout << "The total grams are: " << totalgrams << endl;
}
double input_the_kgandg() {
double kg, g;
cout << "Please enter the number of Kg: ";
cin >> kg;
cout << " Please enter the number of grams: ";
cin >> g;
kg = kg + (g / 1000);
return kg;
}
double convert_the_kgandg() {
double totallbs, totalkg = input_the_kgandg();
totallbs = 2.2046 * totalkg;
return totallbs;
}
void output_the_lbsandoz() {
double totallbs = convert_the_kgandg(), totaloz;
totaloz = totallbs * 16;
totaloz = round(totaloz) % 16;
totallbs = floor(totallbs);
cout << "The total pounds are: " << totallbs << endl;
cout << "The total ounces are: " << totaloz << endl;
}
int round(double x) {
if(x - floor(x) < 0.5)
return floor(x);
else
return ceil(x);
}
|