![]() |
| ||
| Pounds to Grams Conversion 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? |
| ||
| Re: Pounds to Grams Conversion 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. |
| ||
| Re: Pounds to Grams Conversion 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; } Thanks for your help Infamous. I can't express in words how much help you have been. |
| ||
| Re: Pounds to Grams Conversion 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. :-/ |
| ||
| Re: Pounds to Grams Conversion 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 :-( |
| ||
| Re: Pounds to Grams Conversion 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 |
| ||
| Re: Pounds to Grams Conversion 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. |
| ||
| Re: Pounds to Grams Conversion 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. |
| ||
| Re: Pounds to Grams Conversion lots to learn here :D |
| All times are GMT -4. The time now is 10:00 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC