Please help me. I just need an explanation and its really simple! I promise! Here is the assignment and COMPLETED WORKING CODE!
// Description : Write a program to read in an object's mass (in pounds) and convert it to both
// kilograms and grams. The program should prompt the user to enter a mass in pounds,
// then display the kilogram and gram equivalents in both the float and double formats.
// Output your answers three times using first 1, then 5, and finally, 7 places of
// decimal precision.
// Use the conversion factors: One pound is equal to 0.4536 kilograms and one pound is
// equal to 453.59237 grams. Notice that we require more precision for the gram equivalent
// than for the kilogram equivalent.
// Functional Desciption :
#include <iostream>
#include <iomanip>
using namespace std;
// Constants
const double KILO = 0.4536; // 1 pounds equivalent in kilograms
const double GRAM = 453.59237; // 1 pounds equivalent in grams
// Variables
float massPounds; // Weight in pounds
int main()
{
cout << "This program will convert an objects mass in pounds to grams and kilograms." << endl;
cout << "Please input your objects mass in pounds : ";
cin >> massPounds;
double massGram = (GRAM*massPounds); // Weight in grams
double massKilo = (KILO*massPounds); // Weight in kilo
cout << fixed;
cout << "Your objects weight in Kilograms with 1 points of precision is :" << setprecision (1) << MassKilo << endl;
cout << "Your objects weight in Kilograms with 5 points of precision is :" << setprecision (5) << MassKilo << endl;
cout << "Your objects weight in Kilograms with 7 points of precision is :" << setprecision (7) << MassKilo << endl;
cout << "Your objects weight in Grams with 1 points of precision is :" << setprecision (1) << massGram << endl;
cout << "Your objects weight in Grams with 5 points of precision is :" << setprecision (5) << massGram << endl;
cout << "Your objects weight in Grams with 7 points of precision is :" << setprecision (7) << massGram << endl;
return 0;
}
--------------------
My question is why will this code ONLY work when I do the massGram and massKilo assignment/computation AFTER the CIN command. Why can't I assign it earlier? Can anyone help me?