I'm very new to prgramming (in my first Programming class) and have a question about division. My project is to build a math flashcard program. I've gotten every thing else done but having trouble with the division. Where I'm having a problem is when the user inputs the correct answer to the hundredth place, the sum still says it's incorrect (probably because I can't seem to figure out how to setprecision to 2 for the sum to match the user input. Here's what I have so far (this example has the user enter the two number to be divided instead of random numbers to save space on this post). It works just fine when dealing with whole numbers. Again, I'm very new to this so you may ask why I did or didn't do something, it's becuase I'm an idiot and haven't caught on as quick :)

#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
	
int main(int argc, char *argv[])
{
    int number1 = 0;
    int number2 = 0;
    double user_answer = 0;
    double sum = 0;
    
    cout  <<  "Enter 2 Numbers to be Divided: \n"; 
          cin  >>  number1;
	 cin  >>  number2; 
	 
          sum = static_cast<double>(number1) / number2;   
            
    cout <<"Enter Your Answer: ";
         cin >> user_answer;
    
    if(user_answer == sum)
          cout << "Correct" << endl;
    
    else cout <<  "Incorrect.  Answer is: " <<  setprecision(2) << fixed << sum << endl; 
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 6 Replies

Floating-point values are imprecise by nature. Trying to compare them for equality is fraught with peril. ;) A fuzzy comparison is usually the best approach. As long as the difference of the two values is less than a certain epsilon, you're good:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
{
    int number1 = 0;
    int number2 = 0;
    double user_answer = 0;
    double sum = 0;

    cout  <<  "Enter 2 Numbers to be Divided: \n"; 
    cin  >>  number1;
    cin  >>  number2; 

    sum = static_cast<double>(number1) / number2;   

    cout <<"Enter Your Answer: ";
    cin >> user_answer;

    if(fabs(user_answer - sum) < .001)
        cout << "Correct" << endl;
    else 
        cout <<  "Incorrect.  Answer is: " <<  setprecision(2) << fixed << sum << endl; 

    system("PAUSE");
    return EXIT_SUCCESS;
}

Floating-point values are imprecise by nature. Trying to compare them for equality is fraught with peril. ;) A fuzzy comparison is usually the best approach. As long as the difference of the two values is less than a certain epsilon, you're good:

Thank you very much for helping out. Problem I have, when I run the code (with fabs) and use, say, 100 for the numerator and 30 for the denominator, then I enter my answer as 3.33, the program tells me "Incorrect. Answer is: 3.33", which is what i entered. I'm guessing that my answer is #.## but the sum is #.##############... and it's not matching. How do I fix that? Again, thank you and anyone that's willing to help out. Much appreciated.

If you're only going to the hundredths place, try changing the epsilon to something larger, but still reasonable.

Yep, I changed the decimal in the if(fabs line (I'm guessing that is the epsilon?) to .01 instead of .001 and it's working just fine. Thank you both very very much. I struggeled with that all weekend. This is a huge help.

That may be a little sloppy. I think you may want to make it tighter, something more like 0.005. If you use 0.01, that makes answers ranging from 3.323 to 3.343 (3.333 +- 0.01, 20-answers) correct, but neither of those actually is correct. If you use 0.005, then 3.33 is still the only acceptable answer because it will only accept 3.328 to 3.338 (3.333 +- 0.005, 10-answers) as correct.

That may be a little sloppy. I think you may want to make it tighter, something more like 0.005. If you use 0.01, that makes answers ranging from 3.323 to 3.343 (3.333 +- 0.01, 20-answers) correct, but neither of those actually is correct. If you use 0.005, then 3.33 is still the only acceptable answer because it will only accept 3.328 to 3.338 (3.333 +- 0.005, 10-answers) as correct.

Ok, I changed it. Thanks for explaining that. It helped me see how the .005 vs. .01 worked.

By the way, my code for this FlashCard program is VERY sloppy, almost spaghetti so anytime I can gain a sliver of knowledge how to make it neater, I'll take it! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.