Kilograms to Pounds

Here are some problems to demonstrate simple math in C++. Use what you've learned already about the BeIDE to compile these.

Problem:

The equation to convert weight in kilograms to weight in pounds is P = 2.2K, where P is pounds and K is Kilograms. Write an interactive program which prompts the user to enter a weight in Kilograms, calculates and displays the equivalent weight in pounds.

Solution:


#include <iostream>

int main()
{
   float kilos;
   float pounds;
   
   cout << "Enter weight in kilograms: ";
   cin >> kilos;
   
   pounds = 2.2 * kilos;
   
   cout << "That is " 
        << pounds 
        << " pounds." 
        << endl;
   
   return 0;
}

Okay, new things here.

   float kilos;
   float pounds;

Another form of number type in C++ other than int (integer) is float, which means a real number, one with a floating decimal point.

   pounds = 2.2 * kilos;

Here we are just calculating the float value of kilos (the number the user entered) and multiplying it by 2.2. The result then gets placed into the memory space labeled pounds.

   cout << "That is " 
        << pounds 
        << " pounds." 
        << endl;

I purposely entered this data like this to show that you can make your statements look easier to read without affecting the actual code. The compiler will ignore whitespace characters; it will read the above block as one statement. It does not care about when you type the spacebar or enter key, and only when it sees ";" will it know that the end of the statement has arrived.

One item that pleasantly surprised me about the BeOS is handling of the display of float values. On most other platforms (all others, that I am aware of), you must go into some detail with the iomanip library to get a float value to display nicely. If not, you end up with a number like 1.946e+009 or some other difficult to read format.

Not in BeOS. It (as far as I can tell) automatically adjusts the precision and showpoint values for you. If you wish them to be anything other, you must then use iomanip to override this default. One thing to keep in mind - if you're doing any programming where the precision of a float value is important, you'll want to handle the display properties yourself, as I am not sure if the OS will lose any accuracy in the variable when doing this.

Also, if you are using floats as dollar amounts, be careful. For example, if you have two floats that, after some math manipulation has been done, have the values of 14.234 and 18.673, and you add them, the actual value that is produced internally is 32.907, which would be displayed as 32.91 if you had setprecision(2) set. But, the user would expect 14.23 + 18.67 = 32.90, which could lead to problems. Be aware of disturbances that setprecision and setwidth can cause.

For those that want a quick and dirty example of what I'm talking about, here's the lines that you would have to add to your code to make a float look "right" in, say, that other OS (or if you want to override the defaults in BeOS).

First add this to the beginning of the file:

#include <iomanip>

Then, you must add these function calls prior to printing the number you want specifically formatted:

cout << setiosflags(ios::fixed | ios::showpoint)
     << setw(21) << setprecision(2)
     << [enterTheVariableHere] << endl;

First of all note that this is one long and continuous cout statement, ending only after the "endl;".

setiosflags(ios::fixed | ios::showpoint) - indicates that the variable is displayed as a fixed point value with a decimal point. The default justification is right justification, or you may use ios::left, which can be used to specify values should be left justified. You get the idea.

setw(21) - (set width) indicates a field of 21 character positions is to be used for displaying the value.

setprecision(2) - indicates to display a precision of 2 decimal places to the right of the decimal (3.90 in lieu of 3.9).

There are many other items in iomanip, as you can probably imagine, and it is not my place to get into them here.

Now, on to another math problem!