Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to fix the division case? As you notice in the picture when comparing two fl

ID: 3852458 • Letter: H

Question

How to fix the division case?

As you notice in the picture when comparing two floating point numbers, it gives the “wrong” result which the answer always rounds up to integers.

(HELP Me with fixing the code below, only the divsion part, so that it won't round up again)

using namespace std;

int main()

{

    int choice = 0,num1, num2, ans, user_ans;

    while (choice != 5)

    {

    cout << "    Math Menu ";

    cout << "------------------------ ";

    cout << "1: addition ";

    cout << "2: subtraction ";

    cout << "3: multiplication ";

    cout << "4: division ";

    cout << "5: Exit ";

    cout << "Enter your choice (1-5):";

    cin >> choice ;

    while ((choice > 5) or ( choice < 1))

    {

        cout << "Invalid Entry! ";

        cout << "Enter your choice again:";

        cin >> choice;

    }

    if (choice == 5)

        cout << "Thank you for using, Press enter to exit. ";

            else if ( choice == 1)

            {

            srand(time(NULL));

            num1 = rand() %999, num2 = rand() % 999;

            cout << "Please Enter the answer ";

            cout << " " << num1 <<" ";

            cout << "+ " << num2 <<" ";

            cout << "------- ";

            cin >> user_ans ;

            ans = num1 + num2;

            if ( user_ans == ans)

            cout << "congratulations you got the correct answer!";

            if ( user_ans != ans)

            cout << "The answer is in correct, the correct answer is: " << ans << endl;

            }

                else if ( choice == 2)

                {

                srand(time(NULL));

                num1 = rand() %999, num2 = rand() % 999;

                cout << "Please Enter the answer ";

                cout << " " << num1 <<" ";

                cout << "- " << num2 <<" ";

                cout << "------- ";

                cin >> user_ans ;

                ans = num1 - num2;

                if ( user_ans == ans)

                cout << "congratulations you got the correct answer!";

                if ( user_ans != ans)

                cout << "The answer is in correct, the correct answer is: " << ans << endl;

                }

                    else if ( choice == 3)

                    {

                    srand(time(NULL));

                    num1 = rand() %999, num2 = rand() % 999;

                    cout << "Please Enter the answer ";

                    cout << " " << num1 <<" ";

                    cout << "x " << num2 <<" ";

                    cout << "------- ";

                    cin >> user_ans ;

                    ans = num1 * num2;

                    if ( user_ans == ans)

                    cout << "congratulations you got the correct answer!";

                    if ( user_ans != ans)

                    cout << "The answer is in correct, the correct answer is: " << ans << endl;

                    }

                        else if (choice == 4)

                        {

                        srand(time(NULL));

                        num1 = rand() %999, num2 = rand() % 999;

                        cout << "Please Enter the answer ";

                        cout << " " << num1 <<" ";

                        cout << "/ " << num2 <<" ";

                        cout << "------- ";

                        cin >> user_ans ;

                        ans = num1 / num2;

                        if ( user_ans == ans)

                        cout << "congratulations you got the correct answer!";

                        if ( user_ans != ans)

                        cout << "The answer is in correct, the correct answer is: " << ans << endl;

                       }

    }

return 0;

}

Math Tutor Menu 1: addition 2: subtraction 3: multiplication 4: division 5: quit Enter your choice (1~5):4 Enter the answer 274 23 0.00 Your answer is wrong, the correct answer is: 1 Process returned 0 (0x0 execution time 2.274 s Press ENTER to continue

Explanation / Answer

We can solve this issue with the help of Typecasting.

Typecasting refers to the conversion on one datatype to another datatype.

In the above code we have to two things in order for the DIVISION to work correctly and these are -

In c++ language when we divide two integers it gives an integer value for example , 5/2=2. The value gets rounded off to integer.As a general rule = integer/integer = integer.

But, if we perform -

float/integer = float   or integer/float = float

So we can either change the numerator or the denominator to float to get the actual value of answer. This is called as Typecasting.

I have modified the division section of your code :

else if (choice == 4)

                        {

                        srand(time(NULL));

                        num1 = rand() %999, num2 = rand() % 999;

                        cout << "Please Enter the answer ";

                        cout << " " << num1 <<" ";

                        cout << "/ " << num2 <<" ";

                        cout << "------- ";

                        cin >> user_ans ;

(float)ans = num1 / num2(float);

                        if ( user_ans == ans)

                        cout << "congratulations you got the correct answer!";

                        if ( user_ans != ans)

                        cout << "The answer is in correct, the correct answer is: " << ans << endl;

                       }

Kindly rate my answer.ThankYou.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote