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;
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?

uhhh.. u obviously didnt write that code. massPounds is a variable, this is where u store the user's input. cin gets input from the user, and stores it in the massPounds variable. so, u wouldnt want to use the variable until after you have taken it in from the user.

Look at the assignment it is a week one beginners assignment. I am in chapter three of my book and I did write the code. Thanks for the kind words of encouragement and support.
My lack of understanding refers to why I cannot create an equation using the value derived from the CIN command above the CIN command. At the point I am at now it seems it almost makes more sense not to declare the two variables and to just write the lines as so :

cout << "Your objects weight in Grams with 1 points of precision is :" << setprecision (1) << (KILO*massPounds) << endl;
cout << "Your objects weight in Grams with 5 points of precision is :" << setprecision (5) << (KILO*massPounds) << endl;
cout << "Your objects weight in Grams with 7 points of precision is :" << setprecision (7) << (KILO*massPounds) << endl;
return 0;
}

First we ask the user to input massPounds
The user inputs massPounds
we multiply massPounds by some constants
we print out the result of the multiplication

We can't print out the product when someone multiplies a constant by a value input by the user BEFORE the value is input by the user. infamous is referring to the fact that someone who was confused by this point of logic couldn't have written the code with the ability demonstrated in your program. :-/

Why can't I declare :

double massGram = (GRAM*massPounds); // Weight in grams
double massKilo = (KILO*massPounds); // Weight in kilo

along with the massPounds? Why can't this global variable reference a CIN command if the output for it is not being used until AFTER the information is obtained.

Its no different then what I have now except for a: it won't work and b: it declares the variable as global rather then local. Why will this code only work with LOCAL variables declared AFTER the CIN command.

You people have some serious issues with paranoia. You can call me names or attack my efforts all you want. What you see is working code with proper syntax because I have put alot of effort into the assignment. My question comes from not understanding why I cannot use either global variables or local variables defined ABOVE the CIN command when they are not being USED until AFTER the CIN command. A good approach to helping new coders, since you maintain that is one of your goals, would be to provide an answer rather then an attack :-(

Because C++ statements are executed *in order*. When you start the program, where you have double massGram= that is where you are saying what the value for massGram is. But you are multiplying something by massPounds which doesn't have a value yet at this point. Then, after the value for massGram is calculated, the user enters a value for massPounds. But that doesn't change the value for massGram because massGram has already been calculated.

The following is a demonstration:

``````int x = 5; // set value of x to 5
int y = x + 3; // set value of y to 5+3 = 8
x = 2; // change value of x to 2
y = x + 3; // set value of y to 2+3 = 5
cin >> y; // get keyboard input and set it to y's value
x = y; // set x value to current value of y
y = x + 5; // set y to current value of x+5 = user's input value + 5``````

I would like to apologize if anything said was inappropriate or taken the wrong way, ElectroBoy. We are all just here to help. Please let me know if my explanation cleared up any of your confusion.

It did help. Plus I got further in the text. A large part of my confusion came from having examples in the book showing functions that appeared to execute BEFORE the main () that used variables that had values assigned to them inside of the main (). I understand alot more then I did before.

lots to learn here :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.