943,472 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 31960
  • C++ RSS
Mar 30th, 2004
1

Pounds to Grams Conversion

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ElectroBoy is offline Offline
6 posts
since Mar 2004
Mar 30th, 2004
1

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.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 30th, 2004
0

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ElectroBoy is offline Offline
6 posts
since Mar 2004
Mar 30th, 2004
0

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Mar 30th, 2004
0

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 :-(
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ElectroBoy is offline Offline
6 posts
since Mar 2004
Mar 31st, 2004
0

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:

C++ Syntax (Toggle Plain Text)
  1. int x = 5; // set value of x to 5
  2. int y = x + 3; // set value of y to 5+3 = 8
  3. x = 2; // change value of x to 2
  4. y = x + 3; // set value of y to 2+3 = 5
  5. cin >> y; // get keyboard input and set it to y's value
  6. x = y; // set x value to current value of y
  7. y = x + 5; // set y to current value of x+5 = user's input value + 5
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Apr 1st, 2004
0

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Apr 6th, 2004
0

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
ElectroBoy is offline Offline
6 posts
since Mar 2004
Apr 6th, 2004
1

Re: Pounds to Grams Conversion

lots to learn here
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Newbie wants to access a database
Next Thread in C++ Forum Timeline: compiler





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC