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
|
#include <iostream>
const double POUNDS_PER_KILOGRAM = 2.2046;
void read_in_kilograms_and_grams(double& kilograms, double& grams);
double calc_total_kilograms(double kilograms, double grams);
double translate_kilograms_to_pounds(double total_kilograms);
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces);
void output_pounds_ounces(double kilograms, double grams, double pounds, double ounces);
int main()
{
using namespace std;
double pounds, ounces, kilograms, grams, total_pounds, total_kilograms;
char ans;
do
{
read_in_kilograms_and_grams(kilograms, grams);
total_kilograms = calc_total_kilograms(kilograms, grams);
total_pounds = translate_kilograms_to_pounds(total_kilograms);
calc_pounds_ounces(total_pounds, pounds, ounces);
output_pounds_ounces(kilograms, grams, pounds, ounces);
cout << "Would you like to run the program again? Y/N :";
cin >> ans;
cout << endl;
} while ((ans == 'Y') || (ans == 'y'));
return 0;
}
void read_in_kilograms_and_grams(double& kilograms, double& grams)
{
using namespace std;
cout << "Enter kilograms:";
cin >> kilograms;
cout << "Enter grams:";
cin >> grams;
}
double calc_total_kilograms(double kilograms, double grams)
{
using namespace std;
double gramKilograms;
gramKilograms = grams / 1000;
return kilograms + gramKilograms;
}
double translate_kilograms_to_pounds(double total_kilograms)
{
using namespace std;
return total_kilograms * POUNDS_PER_KILOGRAM;
}
void calc_pounds_ounces(double total_pounds, double& pounds, double& ounces)
{
using namespace std;
pounds = static_cast<int>(total_pounds) / 1;
ounces = (total_pounds - pounds) * 16;
//////////////////////////////////////////
if (ounces >= 16) // This is the code I used to try to get the program
{ // to output 1 pound, 0 ounces
ounces -= 16; //
pounds += 1; //
} //
}
void output_pounds_ounces(double kilograms, double grams, double pounds, double ounces)
{
using namespace std;
cout << kilograms << " kilograms, "
<< grams << " grams is equal to\n"
<< pounds << " pounds, "
<< ounces << " ounces\n"
<< endl << endl;
}
|