-
converting script
OK first off let me say this is a class assignment and I don't expect anyone to do my work for me. Now here is my problem. My assignment is to write a program that takes pounds and ounces and converts them to kilograms and grams. This assignment is not out of my class book so I'm a lil confused. I am looking for either a tutorial or some pointers on how to do this. My script asks first for pounds, then for ounces - no problem there. My thoughts as to how to convert them into kilograms / grams is take pounds*16+ounces. Then i should have X ounces. I then want to take X ounces*28.375(this is how many grams there are per ounce) and I will have the total number of grams converted. However if x ouces equals 786237 I don't know how to output "X lb X oz is equivalent to X Kg Xg"
If someone can point me to a page that will explain this (to a non-tech savvy person) I would appreciate it!
-
kg = 786237 / 1000;
grams_remaining = 786237 % 1000;
The modulus of the first by the second is the remainder from their division.
-
no luck
OK I enetered in the formulas you gave me and I got 6 errors!
Here is the code, maybe you can tell me what's wrong with it?
#include <iostream>
using namespace std;
int lbs(int lbs);
int oz(int oz);
float kg(int kg);
float g(int g);
float totalg(int totalg);
void main(void)
{
cout << "Enter pounds ";
cin >> lbs;
cout << endl;
cout << "Enter ounce: ";
cin >> oz;
cout << endl;
cout <<endl;
cout << lbs << "lb" << oz << "oz is equivalent to" << kg << "Kg" << g << "g";
return 0;
}
float g(int g)
{
return totalg % 1000;
}
float kg(int kg)
{
return totalg / 1000;
}
float totalg(int totalg)
{
return lbs * 16 + oz;
}
-
Ok. For starters...
Code:
int lbs(int lbs);
int oz(int oz);
float kg(int kg);
float g(int g);
float totalg(int totalg);
These are all declaring functions.
You can't cin >> lbs because lbs is a function.
EDIT:
Also just noticed in the body of main(), you aren't calling any of the functions that actually do the work.
-
only 1 error now
OK I re-wrote it and I only get 1 error this time. Here is the revised version:
#include <iostream>
using namespace std;
int lbs, oz;
float totaloz, totalg, kg, g;
int main(void)
{
cout << "Enter pounds ";
cin >> lbs;
cout << endl;
cout << "Enter ounce: ";
cin >> oz;
cout << endl;
cout <<endl;
totaloz = lbs * 16 + oz;
totalg = totaloz * 28.375;
kg = totalg / 1000;
g = totalg % 1000;
cout << lbs << "lb " << oz << "oz is equivalent to " << kg << "Kg " << g << "g";
return 0;
}
and here is the error I get:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
F:\Cpp1.cpp(24) : error C2296: '%' : illegal, left operand has type 'float'
Error executing cl.exe.
-
Or if you really want it...
float fmod( float x, float y)
{
return x - ( (x/y) * y);
}