-2
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main ()
{
//Declare variables
    double pounds, grams, kilograms;
//Declare constants
    const double LB2GRM = 453.592;
//Give title to program
    cout << "Pound to kilograms converter" << endl;
//Prompt the user to enter a weight
    cout << "Please enter a weight in pounds: " << endl;
    cin >> pounds;
//Displaying weight with two decimal points
    cout << setiosflags(ios::showpoint) << setprecision(2);
//Round off weight
    static_cast<double>(static_cast<double>(pounds +.5));
//Formula for conerversion
    double fmod(pounds * LB2GRM);
    cin >> grams;
//Show results
    cout << pounds << " pounds are equal to " << kilograms << " kgs and " << grams << " grams" << endl;

return 0;
}

How to convert grams to kilograms? I've got the first part figured out, just not sure how to complete it? Would I just input grams to kg constant?

Nora
  • 11
  • 1
  • 9
  • 2
    Reading [the documentation](http://en.cppreference.com/w/cpp/numeric/math/fmod) for fmod will surely prove useful in explaining the error message (Spoiler it takes 2 arguments) – Borgleader Oct 15 '15 at 02:07
  • How can the program possibly know how many grams are in a kilogram? – Beta Oct 15 '15 at 02:08
  • Thanks @Borgleader :) – Nora Oct 15 '15 at 02:13
  • 3
    Shadowing every statement with a comment that says the same thing as the statement is possibly overkill. Comments should really be reserved for things that are not obvious. – Galik Oct 15 '15 at 02:17
  • When you call a function like `fmod()` you need to assign the result to something. Putting `double` before it is a declaration, that's not how you call it. – Barmar Oct 15 '15 at 02:22
  • It forms a good habit for more lengthy lines of code in the future. So just call me Captain Overkill. – Nora Oct 15 '15 at 02:23

1 Answers1

0

You're never assigning anything to kilograms before you print it. You should assign that from the formula.

grams = pounds * LB2GRM;
// Divide grams by 1000 to get the kg part
kilograms = floor(grams / 1000));
// The remaining grams are the modulus of 1000
grams = fmod(grams, 1000.0);

Also, this statement doesn't do anything:

static_cast<double>(static_cast<double>(pounds +.5));

First of all, casting something to the same type has no effect. Second, you're not assigning the result of the cast to anything (casting doesn't modify its argument, it just returns the converted value). I suspect what you want is:

pounds = static_cast<double>(static_cast<int>(pounds +.5));

But a simpler way to do this is with the floor() function:

pounds = floor(pounds + .5);

Casting a double to int will remove the fraction.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Oh I had no idea I was just trying to round off the pounds. Back to the fmod..using 'cin >>' won't assign the result? And would I have to call it from outside of main? – Nora Oct 15 '15 at 02:35
  • `cin >>` is for getting input from the user. – Barmar Oct 15 '15 at 02:40
  • Like when you do `cin >> pounds` so the user can enter the number of pounds. – Barmar Oct 15 '15 at 02:40
  • It sounds like you need to go back to your textbook and brush up on the basics. You're very confused about how to use functions. – Barmar Oct 15 '15 at 02:42
  • When you call a function like `fmod()`, you do it as `result = fmod(x, y);` – Barmar Oct 15 '15 at 02:43
  • I'm not sure why you're using `fmod()`. Are you trying to turn `1234 grams` into `1 kg and 234 grams`? – Barmar Oct 15 '15 at 02:44
  • I guess that is what you intended, I've updated the answer to show how to do it – Barmar Oct 15 '15 at 02:47
  • I'm fairly new to this as I barely started learning this semester so I am in basics lol. And fmod() is for determining the weight in grams. – Nora Oct 15 '15 at 02:49