Hi guys, my assignment is to create a program that sets up a menu for a user to choose from. The menu should consist of an addition, subtraction, multiplication and division problem. It seems to work well, but the only thing that's not working is the division problem. Whenever I try to test it out, I don't get the correct answer to the random division problem that I set up. What is wrong here? I think it has to do with floats and division but I can't pinpoint the error.

/ This program displays a menu allowing the user to choose from a set of 4 math operations and a choice to exit the program. Once the user selects one of the 4 math operations, they can enter the answer. If the answer is correct, the user will be congratulated. If the answer is wrong, the correct answer will appear. The menu will appear again allowing the user to choose from an option.

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

int main()
{

    // Constants for the menu choices
    const int SEED = 999;
    const int ADDITION_CHOICE = 1,
              SUBTRACTION_CHOICE = 2,
              MULTIPLICATION_CHOICE = 3,
              DIVISION_CHOICE = 4,
              QUIT_CHOICE = 5;

    // Variables
    int userchoice;
    float studentanswer;
    float correctanswer;


    do
        { // Beggining of do-while loop
        unsigned int number1 =0;
        unsigned int number2 =0;
        srand(unsigned(time(0)));

        number1 = 1+rand() % SEED;
        number2 = 100+rand() % SEED;


        // Display the menu
         cout << "Choose from the following options:\n\n"
         << "1. Addition\n"
         << "2. Subtraction\n"
         << "3. Multiplication\n"
         << "4. Division\n"
         << "5. Quit\n"
         << "Enter your choice: \n";
         cin >> userchoice;


        // Validates the input                                              
        if (userchoice < ADDITION_CHOICE || userchoice > QUIT_CHOICE)


        {
            cout << "Please choose a valid number from 1 to 5\n\n"
            << "1. Addition\n"
            << "2. Subtraction\n"
            << "3. Multiplication\n"
            << "4. Division\n"
            << "5. Quit\n";

            cin >> userchoice;
        }


        // Respond to user's choice
        switch (userchoice)



            { // Addition
            case ADDITION_CHOICE:
                    cout << "Solve the problem below: \n";
                    cout << number1 << " + " << number2 << " = " << endl;
                    correctanswer = number1 + number2;
                    cin >> studentanswer;
                    if (studentanswer == correctanswer)
                        cout << "Correct! Congratulations.\n";
                    else
                        cout << "That is incorrect. The correct answer is " << correctanswer << endl;
                    break;

            // Subtraction
            case SUBTRACTION_CHOICE:
                    cout << "Solve the problem below: \n";
                    cout << number1 << " - " << number2 << " = " << endl;
                    correctanswer = number1 - number2;
                    cin >> studentanswer;
                    if (studentanswer == correctanswer)
                        cout << "Correct! Congratulations.\n";
                    else
                        cout << "That is incorrect. The correct answer is " << correctanswer << endl;
                    break;

            // Multiplication
            case MULTIPLICATION_CHOICE:
                    cout << number1 << " * " << number2 << " = " << endl;
                    correctanswer = number1 * number2;
                    cin >> studentanswer;
                    if (studentanswer == correctanswer)
                        cout << "Correct! Congratulations.\n";
                    else
                        cout << "That is incorrect. The correct answer is " << correctanswer << endl;
                    break;

            // Division
                case DIVISION_CHOICE:
                    cout << number1 << " / " << number2 << " = " << endl;
                    correctanswer = number1 / number2;
                    cin >> studentanswer;
                    if (studentanswer == correctanswer)
                        cout << "Correct! Congratulations.\n";
                    else
                        cout << "That is incorrect. The correct answer is " << correctanswer << endl;
                    break;

            // Exit
                case QUIT_CHOICE:
                    exit(0);
                    break;
            }

}while(userchoice != 5);

        return 0;

}

Recommended Answers

All 6 Replies

The program is doing integer division which means no fractions because number1 and number2 are both integers. You can correct the problem by typcasting either the numerator or demoninator to float

correctanswer = (float)number1 / number2;

What AD said, and thanks for posting your code. We get a lot of people just asking for us to do their homework for them, without trying to solve the problem(s) themselves. You get an up-vote from me for that! :-)

Thanks for the help.

However, I encountered a new problem from the same code above. Here is an extract

// Division
                case DIVISION_CHOICE:
                    cout << number1 << " / " << number2 << " = " << endl;
                    correctanswer = (float)number1 / number2;     
                    cin >> studentanswer;
                    cout << setprecision(2) << fixed;
                    if (studentanswer == correctanswer)
                        cout << "Correct! Congratulations.\n";
                    else
                        cout << "That is incorrect. The correct answer is " << correctanswer << endl;
                    break;

I tested this out and got the following random numbers:

759/484. My answer was 1.57.

This is what I got from the program.

"That is incorrect. The correct answer is 1.57"

The answer I'm entering, even though it is correct, seems to not match up with what the program is outputting somehow? Anyone know how to fix this?

It's almost impossible to test for equality with floats because the varables can contain small rounding errors. It is better to convert both floats to strings then compare the two strings. You might also be able to use floor()

^ How do I do that? :|

RTFM? Sorry, but it is elementary. Here is the Linux manpage for floor():

FLOOR(3)                   Linux Programmer’s Manual                  FLOOR(3)

NAME
       floor, floorf, floorl - largest integral value not greater than argument

SYNOPSIS
       #include <math.h>

       double floor(double x);
       float floorf(float x);
       long double floorl(long double x);

       Link with -lm.

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       floorf(), floorl(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or cc -std=c99

DESCRIPTION
       These functions return the largest integral value that is not greater than x.

       For example, floor(0.5) is 0.0, and floor(-0.5) is -1.0.

RETURN VALUE
       These functions return the floor of x.

       If x is integral, +0, -0, NaN, or an infinity, x itself is returned.

ERRORS
       No errors occur.  POSIX.1-2001 documents a range error for overflows, but see NOTES.

CONFORMING TO
       C99, POSIX.1-2001.  The variant returning double also conforms to SVr4, 4.3BSD, C89.

NOTES
       SUSv2  and  POSIX.1-2001  contain text about overflow (which might set errno to ERANGE, or raise an FE_OVERFLOW
       exception).  In practice, the result cannot overflow on any current machine, so this  error-handling  stuff  is
       just  nonsense.   (More  precisely,  overflow can happen only when the maximum value of the exponent is smaller
       than the number of mantissa bits.  For the IEEE-754 standard 32-bit and 64-bit floating-point numbers the maxi-
       mum  value  of  the  exponent is 128 (respectively, 1024), and the number of mantissa bits is 24 (respectively,
       53).)

SEE ALSO
       ceil(3), lrint(3), nearbyint(3), rint(3), round(3), trunc(3)

COLOPHON
       This page is part of release 3.22 of the Linux man-pages project.  A description of the project,  and  informa-
       tion about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.
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.