473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ function logic problem!

I'm trying to convert someones cereal weight from ounces to metric tons, and how many boxes it would take to make a metric ton. I suck at converting weights and I wanted to know if the logic in my function is correct and is converting the weights like it should be. If it is not correct can someone please tell me the syntax and function to do this. Thank You!

#include <iostream>
using namespace std;

int main ()
{
double Cereal_Weight, Cereal_Total, Total_Boxes;

cout << "Welcome to the cereal converter program\n";
cout << "Enter the weight in ounces of the cereal box\n";
cin >> Cereal_Weight;

Cereal_Total = Cereal_Weight / 35273.92;
Total_Boxes = Cereal_Weight * 35273.92;

cout << "Your cereal weighs " << Cereal_Total << " metric tons\n";
cout << "It would take " << Total_Boxes << " to fill a metric ton\n";

cin.sync();
cin.get();
return 0;
}
Jul 27 '07 #1
11 3199
seforo
60
If you can do it mathematically you can program it simply. Write a functional algorithm first or a pseudocode and then simply code it. Bearing in mind that
1 ounce = 2.83495231e-5 metric tons
Jul 27 '07 #2
If you can do it mathematically you can program it simply. Write a functional algorithm first or a pseudocode and then simply code it. Bearing in mind that
1 ounce = 2.83495231e-5 metric tons
I don't know if I know how to do it mathematically, like I said I'm not good at converting weights and I haven't done it for years. I've tried googling it but all I get is converter's that convert it for me, so if someone can tell me if my logic in my program is correct: 35,273.92 ounces in a metric ton / by the number entered by the user.

Cereal_Total = Cereal_Weight / 35,273.92;

Is the logic correct if not can you please tell me what the function should look like so i can convert this and move on. Thank You!
Jul 27 '07 #3
Anyone that could help would be great thank you!
Jul 28 '07 #4
JosAH
11,448 Expert 8TB
Anyone that could help would be great thank you!
Oh come on, if you put 35,273.92 ounces together you get 1 metric ton, so
if you put 35,273.92*x ounces together you get 1*x metric ton, and if you put
x ounces together on a pile you get 1*x/35,273.92 ton which is x/35,273.92
metric ton.

So if you know the number of ounces 'x', you know the number of metric tons:
x/35,273.92.

kind regards,

Jos
Jul 28 '07 #5
Oh come on, if you put 35,273.92 ounces together you get 1 metric ton, so
if you put 35,273.92*x ounces together you get 1*x metric ton, and if you put
x ounces together on a pile you get 1*x/35,273.92 ton which is x/35,273.92
metric ton.

So if you know the number of ounces 'x', you know the number of metric tons:
x/35,273.92.

kind regards,

Jos
#include <iostream>
using namespace std;

int main()
{
double Cereal_Weight, Cereal_Total, Total_Boxes;
char answer;
do
{
cout << "Welcome to the cereal converter program.\n";
cout << "Enter the weight in ounces of the cereal box.\n";
cin >> Cereal_Weight;

Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000);
Total_Boxes = 35273.92 / Cereal_Weight;

cout << "Your cereal weighs " << Cereal_Total << " metric tons,\n";
cout << "It would take " << Total_Boxes << " cereal boxes to fill a metric ton.\n\n";
cout << "Would you like to convert another cereal weight.\n"
<< "Press y for yes, n for no,\n"
<< "and press return: ";
cin >> answer;
}while (answer == 'y' || answer == 'Y');

cout << "Goodbye!\n";

cin.sync();
cin.get();
return 0;
}

This way is funnier thou! Now since I figured that out, why is the output of the conversion truncated. It comes out to 0.000283495 when it should be:
0.00028349523125. See I wasn't sure if the logic was correct for one I don't know the metric system, and two if I don't know the metric system then htf do I know if I did it right. So I tried long double in place of double still got same result but wouldn't matter anyway because double goes to 10^308 anyway. So back to the point htf do I get the second number and not the first.Thank You!
Jul 28 '07 #6
JosAH
11,448 Expert 8TB
#include <iostream>
using namespace std;

int main()
{
double Cereal_Weight, Cereal_Total, Total_Boxes;
char answer;
do
{
cout << "Welcome to the cereal converter program.\n";
cout << "Enter the weight in ounces of the cereal box.\n";
cin >> Cereal_Weight;

Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000);
Total_Boxes = 35273.92 / Cereal_Weight;

cout << "Your cereal weighs " << Cereal_Total << " metric tons,\n";
cout << "It would take " << Total_Boxes << " cereal boxes to fill a metric ton.\n\n";
cout << "Would you like to convert another cereal weight.\n"
<< "Press y for yes, n for no,\n"
<< "and press return: ";
cin >> answer;
}while (answer == 'y' || answer == 'Y');

cout << "Goodbye!\n";

cin.sync();
cin.get();
return 0;
}

This way is funnier thou! Now since I figured that out, why is the output of the conversion truncated. It comes out to 0.000283495 when it should be:
0.00028349523125. See I wasn't sure if the logic was correct for one I don't know the metric system, and two if I don't know the metric system then htf do I know if I did it right. So I tried long double in place of double still got same result but wouldn't matter anyway because double goes to 10^308 anyway. So back to the point htf do I get the second number and not the first.Thank You!
You should rename your variables so you won't get confused so easily;
here are your variables:
Expand|Select|Wrap|Line Numbers
  1. double cereal_ounce; // number of ounces of cereals
  2. double cereal_ton; // number of metric tons of cereals
  3.  
Then you should build an ounce_to_ton converter (see my previous reply):

Expand|Select|Wrap|Line Numbers
  1. double ounce_to_ton(double x) { return x/35273.92; }
  2.  
You can use it as follows:

Expand|Select|Wrap|Line Numbers
  1. double cereal_ounce= // some value measured in ounces
  2. double cereal_ton= ounce_to_ton(cereal_ounce);
  3.  
I don't know where those boxes come from in your program; About the number
of digits in the fraction printed: that's how many digits are printed by default.

kind regards,

Jos
Jul 28 '07 #7
You should rename your variables so you won't get confused so easily;
here are your variables:
Expand|Select|Wrap|Line Numbers
  1. double cereal_ounce; // number of ounces of cereals
  2. double cereal_ton; // number of metric tons of cereals
  3.  
Then you should build an ounce_to_ton converter (see my previous reply):

Expand|Select|Wrap|Line Numbers
  1. double ounce_to_ton(double x) { return x/35273.92; }
  2.  
You can use it as follows:

Expand|Select|Wrap|Line Numbers
  1. double cereal_ounce= // some value measured in ounces
  2. double cereal_ton= ounce_to_ton(cereal_ounce);
  3.  
I don't know where those boxes come from in your program; About the number
of digits in the fraction printed: that's how many digits are printed by default.

kind regards,

Jos
Boxes = how many boxes of X weight in ounces it takes to fill a metric ton(so how many boxes it would take to weigh a metric ton if the box weighed ten ounces) but thats right so im not worried. And on my last reply I forgot to add that that number I get is when I replace 10 with x as u say so 10 / 35273.92 that is the number I want to print out longer.
Jul 28 '07 #8
JosAH
11,448 Expert 8TB
Boxes = how many boxes of X weight in ounces it takes to fill a metric ton(so how many boxes it would take to weigh a metric ton if the box weighed ten ounces) but thats right so im not worried. And on my last reply I forgot to add that that number I get is when I replace 10 with x as u say so 10 / 35273.92 that is the number I want to print out longer.
So you solved everything now except for those number of digits printed? Good.
If you want, say, 20 decimals of the fraction printed (which is way too much
because only at most 15 fraction decimals are significant) do this before you
print anything:

Expand|Select|Wrap|Line Numbers
  1. cout.setf(0,ios::floatfield);
  2. cout.precision(20);
  3.  
kind regards,

Jos

ps. I'm not sure whether or not that first line is needed; I don't have my docs
with me; give it a try.
Jul 28 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
To paradox6996:

Get a book on ddimensional analysis.

Do you remember stuff like:

A
---- x B
B

and the answer was A because the B's divided each other out? You can do the same thing with units of measure. To get kilometers from miles:

miles x kilometers
-----------
miles

You start with miles and multilpy by kilometers per mile. The miles divide each outher out and you are left with kilometers.

If you do it the other way:

miles x miles
----------
kilometers

the miles do not divide out so this is the wrong way.

If you get your dimensions to work out, the calculation is easy.
Jul 28 '07 #10
ok sweet this worked perfect:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(14);

Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

but is their a way to make the precision work for the above line, and not for this one:

Total_Boxes = 35273.92 / Cereal_Weight.

Oh and for anyone that wanted to know doing this:

Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

is the same as doing this x * 2.83495231(1 ounce in metric tons) * 10^-5, but since you can't use scientific notation in c++ I think. you gotta do it as above.
Jul 28 '07 #11
JosAH
11,448 Expert 8TB
ok sweet this worked perfect:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(14);

Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

but is their a way to make the precision work for the above line, and not for this one:

Total_Boxes = 35273.92 / Cereal_Weight.

Oh and for anyone that wanted to know doing this:

Cereal_Total = ((Cereal_Weight * 2.83495231) / 100000)

is the same as doing this x * 2.83495231(1 ounce in metric tons) * 10^-5, but since you can't use scientific notation in c++ I think. you gotta do it as above.
*ahem*, you can use scientific number notation: 2.83495231e-5
The 'e' (or 'E') represents 'times 10 raised to the power'.

kind regards,

Jos
Jul 29 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jenkins | last post by:
I've loaded various thoughts I've collected into mysql. I want to write an app, all in one file, that has the following features: 1. On initial entry, selects and displays the thoughts ( might...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
2
by: DSL | last post by:
I'm pulling my little hair that's left out trying to figure out how I can call a function from within a function. Here's the deal... and yes I know I should be using PHP! I have a password login...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
17
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
11
by: Daniel T. | last post by:
The function below does exactly what I want it to (there is a main to test it as well.) However, I'm curious about ideas of making it better. Anyone interested in critiquing it? void formatText(...
6
by: dlists.cad | last post by:
Hi. I am looking for a way to check if some given set of (*args, **kwds) conforms to the argument specification of a given function, without calling that function. For example, given the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.