Welcome back !
I see you have taken a different approach, that is OK, nothing like trying different things. The thing is, we were 95% of the way there last time.
Any way for this code, there are a few things:
The use of ints won't be any good because of the integer division, you need to use float or double to get the expected answers. The division needs to have a check for divide by zero, this is not as straight forward as it sounds, when using floats or doubles. This is my post to someone else yesterday.
EPSILON is the biggest number such that 1 + EPSILON still equals 1. There is a FLT_EPSILON for floats and DBL_EPSILON for doubles. The EPSILON's need to be scaled according to the number you have. If your number is 1000 say, then you need 1000 * DBL_EPSILON.
The thing to understand is the 'distance' between representable numbers over different ranges. If we consider a float that equals 1, the next greatest number that can represented is about 1 + 1E-8, so the 'distance' is 1E-8. If the float is 1E8, the next greater number is 1.0000001E8, so the distance is 1.0. At 1E9 the distance is 10, at 1E10 distance is 100. So when testing for equality you really need to test that your number is between the number below and the number above. That's where the EPSILON's come in. Similar for for less than and greater than.
The reason that inexact representation occurs, is because of the binary fractions that are used. A binary fraction of 1.1101 is 1 + 0.5 + 0.25 + 0.0625. USing this method it is possible to represent most of the numbers, but not all.
The other consideration is the precision of the answer that you require. Say you wish to use a prcision of 0.01, you can do this:
double MyPrecision = 0.01;
double MyDouble = 0.1; //not exact is about 0.0999...97 to 16 sig figures
double MyNum = 10 * MyDouble ; // not exact 0.999...97
if (MyNum > (1.0 - MyPrecision) && MyNum < 1.0 +MyPrecision ) //this is true
cout << "MyNum equals 1.0 0.01 precision" << endl;
cout << "MyNum is not equal to 1.0 0.01 precision" << endl;
This is OK but the 0.01 suffers from the same problem as other floating point numbers, so in some situations you might still need to use DBL_EPSILON as well.
Generally floating point is a pain in the A**, but you can get used to it.
Google some more there is heaps of stuff about this topic.
The other way is to use an absolute function like this: (from iHutch105)
|You could include the math header and do something like this. It's not perfect, but it'll probably suit your needs.|
if (fabs(price - added) < 0.00001)
This method is better because there is only one test. You could substitute DBL_EPSILON for the 0.00001.
A small thing - this code only runs once, and it finishs if there is invalid input.
|Maybe I should make a function for this :|
Yes, functions are good for reusing code.
Previously we had
I would rather have 20 using statements at the top of the code rather than 200 std:: everywhere, but that is my preference.
With your references, have you initialsed them with another variable? That should have been a compile error, unless i am missing something.
There are no checks on the input of the numbers, last time we had scanf which did check the input.
So have a go with these suggestions, get it working, but could I persuade you to have another go with the code / design that I suggested last time?
Now for something else entirely.
I use linux, just about to install fedora core 17. Linux is great, it comes with over 2000 apps and is entirely free, very secure (no virus protection needed). I use KDevelop with Qt for GUI apps, this is just as good as if not better than Visual Studio.
Linux is also great if you are a student, I remember being "poor as a church mouse" when I was a student. All you need is a spare hard drive to install it on, and you are away! If you have a PC you can get removable hard-drive bays, so that you can chop & change as much as you want. You can still have windows with a multiboot menu. Files can be saved in MS format .
There are various distributions, fedora, ubuntoo, debian, suse are some of the common ones. You can create a separate partition and test a different linux while still being able to boot your main linux. I am having fedora as my main linux and will have a go at testing ubuntoo on a spearate partition.
Any way have a think about this, could be really worthwhile for you.