-
C++ HELP! PLEASE!
hi, i'm just starting out in c++
i wrote the code for this really simple problem :"A metric ton is 35,273.92 ounces. Write a C++ program to read the weight of a box of cereal in ounces then output this weight in metric tons, along with the number of boxes to yield a metric ton of cereal. "
but for some reason the **** program doesn't divide!!
say you plug in 56 for the weight of the box, it gives me 0 tons and 36306508 boxes!!
but if i put the number in my calculator with what i want the program to do, i get totally different numbers. can anyone look at my code and tell me where i'm going wrong. it's driving me crazy!
<program>
#include <iostream>
using namespace std;
int main()
{
int const ouncesInTon = 35273.93;
int ounces_cereal,boxes;
float ton_cereal;
cout << "Enter the weight in ounces of one cereal box and press enter.\n";
cin >> ounces_cereal;
ton_cereal = ounces_cereal / ouncesInTon;
cout << "If you have ";
cout << ounces_cereal << " ounces of cereal then\n";
cout << "You have " ;
cout<< ton_cereal;
cout << " tons of cereal.\n";
cout << "Therefore one box has " << ounces_cereal;
cout << " ounces it will take ";
boxes = (ouncesInTon / ounces_cereal);
cout << boxes;
cout << " boxes to make 1 ton.\n";
return 0;
}
</program>
THANKS for you help!
-
<program>
#include <iostream>
using namespace std;
int main()
{
int const ouncesInTon = 35273.93;
int ounces_cereal,boxes;
float ton_cereal;
cout << "Enter the weight in ounces of one cereal box and press enter.\n";
cin >> ounces_cereal;
ton_cereal = ounces_cereal / ouncesInTon;
cout << "If you have ";
cout << ounces_cereal << " ounces of cereal then\n";
cout << "You have " ;
cout<< ton_cereal;
cout << " tons of cereal.\n";
cout << "Therefore one box has " << ounces_cereal;
cout << " ounces it will take ";
boxes = (ouncesInTon / ounces_cereal);
cout << boxes;
cout << " boxes to make 1 ton.\n";
return 0;
}
</program>
THANKS for you help!
Been a while since I played with C++ but shouldn't line 12 read
ton_cereal = ( ounces_cereal / ouncesInTon) ;
Just a thought try it.
Also just thinking about it, that function will always return 0 unless the box weighs more then a tonne, you are assigning a Int to a return that will always be a decimal value. an INT is a Whole number, no decimals. Try using Float. (Can't remember the line but I know how it is you'll never get a decimal value.)
You can also try expressing this as a fraction. (1/Number of Boxes) you can also do that to have it execute faster, do one complicated devide, and use one inversion, Inversions are much faster then divide. 3DNow actually has an enhancement to do that very thing to save doing long divide operations)
((1/first number)x(second number)) is the same as second number / first number. but executes a lot faster.
Just something to keep in mind.
Last edited by Wizzard~Of~Ozz; 10-03-2001 at 09:22 PM.
-
nope didnt work. but thanks for the try
line 10 on_cereal = ( ounces_cereal / ouncesInTon );
and line 18:boxes = (ouncesInTon / ounces_cereal);
ok ouncesInTon is a constant = 35273.93
and we put ounces_cereal as 56.
we should get for ton_cereal =.001587574733 (can c++ not display that or something)
and boxes =629.891607143 boxes..
but instead
the program says
ton_cereal=0
and boxes=37275404.
-
I believe it's
double ton_cereal;
Could you try this for me, I don't have compiler installed
#include <iostream>
using namespace std;
int main()
{
int const ouncesInTon = 35273.93;
int ounces_cereal,boxes;
double ton_cereal;
cout << "Enter the weight in ounces of one cereal box and press enter.\n";
cin >> ounces_cereal;
ton_cereal = (1/ounces_cereal) * ouncesInTon));
boxes = (1/ton_cereal);
cout << "If you have ";
cout << ounces_cereal << " ounces of cereal then\n";
cout << "You have " ;
cout<< ton_cereal;
cout << " tons of cereal.\n";
cout << "Therefore one box has " << ounces_cereal;
cout << " ounces it will take ";
cout << boxes;
cout << " boxes to make 1 ton.\n";
return 0;
}
Last edited by Wizzard~Of~Ozz; 10-03-2001 at 09:35 PM.
-
didn't work if that's what you meant...man how am i going to survive a quarter of this class if i cant do a simple program like this! 
edit: tried it, got 0 and 0 for the answers.
Last edited by Pshawn5; 10-03-2001 at 09:39 PM.
-
Ok, I installed VC++ and here's what I changed
#include <iostream.h>
int main()
{
double const ouncesInTon = 35273.93;
double ton_cereal,ounces_cereal,boxes;
cout << "Enter the weight in ounces of one cereal box and press enter.\n";
cin >> ounces_cereal;
ton_cereal = 1/ounces_cereal * ouncesInTon;
boxes = 1/ton_cereal;
cout << "If you have ";
cout << ounces_cereal << " ounces of cereal then\n";
cout << "You have " ;
cout<< ton_cereal;
cout << " tons of cereal.\n";
cout << "Therefore one box has " << ounces_cereal;
cout << " ounces it will take ";
cout << boxes;
cout << " boxes to make 1 ton.\n";
return 0;
}
That executes and returns
C:\Documents and Settings>text1
Enter the weight in ounces of one cereal box and press enter.
56
If you have 56 ounces of cereal then
You have 629.892 tons of cereal.
Therefore one box has 56 ounces it will take 0.00158757 boxes to make 1 ton.
Hehehehe, just noticed the weight is a bit off, **** transposition
#include <iostream.h>
int main()
{
double const ouncesInTon = 35273.93;
double ton_cereal,ounces_cereal,boxes;
cout << "Enter the weight in ounces of one cereal box and press enter.\n";
cin >> ounces_cereal;
ton_cereal = 1/ ouncesInTon * ounces_cereal;
boxes = 1/ton_cereal;
cout << "If you have ";
cout << ounces_cereal << " ounces of cereal then\n";
cout << "You have " ;
cout<< ton_cereal;
cout << " tons of cereal.\n";
cout << "Therefore one box has " << ounces_cereal;
cout << " ounces it will take ";
cout << boxes;
cout << " boxes to make 1 ton.\n";
return 0;
}
Last edited by Wizzard~Of~Ozz; 10-03-2001 at 09:58 PM.
-
"i am not worthy! i am not worthy!"
you are the wizard of oz!!
thanks man!
double eh? i gotta learn how to use it now..
and thanks for going through all that trouble to help a newbie!
-
no probs and BTW if you make boxes an int it will round down.
When I first started horsing around with it I messed up everything, my first program which had to disply on the screen "My name is fred" had a error (No ; )
You can change the 1/x * y to y/x they are the same thing, but I remember reading about the speed issue. also I got an error with
using namespace std;
Wasn't sure if it was needed or something that you had and I didn't, so I deleted it
-
once you got me numbers that worked, i just fooled around and fixed all that stuff.. thanks once again, i'm sure i'll be having more questions, since i got 4 more problems like this to do!
-
Ok, cleaned it up a bit
#include <iostream.h>
int main()
{
double const ouncesInTon = 35273.93;
int boxes;
float ton_cereal,ounces_cereal;
cout << "What is the weight in ounces of one cereal box?";
cin >> ounces_cereal;
ton_cereal = ounces_cereal / ouncesInTon ; // Precise weight of box in tons
boxes = ((ouncesInTon / ounces_cereal) +.9999); // Caclulates # of boxes and adds .9999 for acurate rounding
cout << "If you have " << ounces_cereal << " ounces of cereal then";
cout << " you have " << ton_cereal << " tons of cereal.\n";
cout << "Therefore one box with a weight of " << ounces_cereal << " ounces ";
cout << "will take " << boxes <<" boxes to make 1 ton.\n";
return 0;
}
This way it won't err with a cereal box weight of 35274 or higher. According to original it will take 0 boxes 
Also corrected some grammar.
Cleaned up the output a bit. reduced uneeded doubles to floats (This will generate warning about converting doubles to floats and ints.) 2 warnings no errors.
Last edited by Wizzard~Of~Ozz; 10-04-2001 at 12:05 AM.
-
You started something bad, I haven't played with this in a while. quite interesting. I keep playing with your code. Got it now to recognize 0 ounces and catch it and display a diff message, anyways off to bed, or I'll be playing all night.
-
wow,so differnet from my code(right now) and the code we started with.
so neat...
anyways with your help, i was able to finish 3 other programs where i just used double and float instead of the stupid int!
w00t!
btw do u have aim or icq? so i can just ask you there for questions.
oh btw thanks for ure changing source to a condensed version,but i'll stick with what i got,
i don't want the teacher thinking i'm some expert,when i'm just a regular n00b.
-
Senior Member
Wizard
In regards to the (1/x)*y = y/x
Most good compilers will make that switch for you. Also they will change x<=y to x<y+1 which also take less computations.
Banti
-
Thanks Banti, I tried both and timed it and couldn't see a difference. guess that explains why =] (Looped it through the calc 30000 times)
Thanks for explaination the "Information Super Highway" aka. "Shopping super Highway" had no expplaination but I remember reading about 3dnw doing it.
Last edited by Wizzard~Of~Ozz; 10-04-2001 at 05:11 PM.
-
pshawn, I'm afraid I'll be of little help, like I said I just play with this, I haven't taken any courses so in 1 month you'll be ahead of me. If you have a prob, just add me as a buddy on here and pm me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks