decimal

Pages: 12
as with all the other options, it shows error when it supposed to but also shows error when I input 0.7 0.9 1.3 1.4 1.8 1.9 2.1 2.3 2.6 2.8
it looks all random so it's hard to fix it
Oh, so sorry!! Use fabs() instead of abs().
it shows errors in the same place (aaaa!!!!!!going crazy)
which is exactly what it does with my idea, so I decided to cout
((int)(score * 100.0) % 10) just to see what it gives for the problematic numbers. All the numbers I listed before and more I didn't list are giving value of 9.
so I'm thinking to do this:

1
2
if (((int)(score * 100.0) % 10) != 0 && ((int)(score * 100.0) % 10) != 9)
    cout << "\n\n\tERROR\n\n";


I know it brings risk of some other %10 having value of 9 but somehow

(int)(score * 100.0)

gives 128 for 1.29, 228 for 2.29 etc.

and for 1.3 it gives 129...

Last edited on
Please what's going wrong with the epsilon comparison I've given you? It's the standard way of comparing two double values. If it shows errors in the same place then there's another bug around.

To answers your any idea why?:

((int) (1.0 * 100.0) % 10)  ->  0     =>  0 != 9  =>  "\n\nERROR\n\n"
Sorry, I removed that post because it work now (the 1.0 problem)

with the other thing I did exactly what you told me, I changed abs to fabs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float score;
static double epsilon(0.000001);
static double allowedShift(10.0);
do{
        cout << "\n\n\tEnter score: ";
        cin >> score;
if (fabs(trunc(score * allowedShift) - score * allowedShift) > epsilon)
{
   cout << "one decimal only please.\n";
}}
while (score <=6);
return 0;
}


but still 1.3 comes out as an error as well as 1.4 etc

I didn't even attached it to my main program so nothing else is interfering with it.
Could it be compiler? I'm using code::block..
Declare all floating point numbers of the same type (say double or float). The error may result of internal conversion from float to double.
thank you tcs, I work with all doubles (not with floats)
now i just have to understand how it works and I'm all good:)
Topic archived. No new replies allowed.
Pages: 12