It is supposed to generate two numbers based on the user's preference of the highest number to use. What I want it to do is to divide integers that result in integers or perfectly whole numbers. I do not want to have numbers that don't divide out with remainders. So I set up the multiplication of two numbers, then divided the quotient with the divident or divisor, but it is coming up with some weird numbers that doesn't equal to whole numbers without remainders.

void divfunction()
{
    unsigned int number1 = 0;So
    unsigned int number2 = 0;
    srand(unsigned(time(0)));



    int totaldivcorrect = 0;
    int totaldivwrong = 0;
    int multresult;


    highestNum = getHighestNum();
    numquestions = getNumQuestions();


    for (int count = 1; count <= numquestions; count++)
    {
        number1 = 1+rand() % highestNum;
        number2 = 1+rand() % highestNum;


        cout << number1 << " / " << number2 << " = " << endl;
        multresult = number1 * number2;
        correctanswer = multresult/number1;

        cin >> studentanswer;

        // Determines the winner
        determinewinner();

Recommended Answers

All 7 Replies

Lines 24-26 leave you with the value of number 2 as the correctanswer, but that value is not what you get from dividing number1 / number2

If you want to ensure that n1 and n2 result in an even division (no remainder) you'll probably have to generate them in a loop that continues till you get a satisfactory pair. First, n1 must be larger than n2. Then, do the division, storing the quotient. Multiply the quotient by the n2, does that equal n1? Now you have a valid pair.

Any other ways? :(

Well, strictly speaking, my first requirement that you test n1 > n2 isn't really needed, as the second division test will ensure that.

Why so glum? I see this adding only about 6 lines or so to what you have.

My code asks the user for the biggest number that they want in the division question. What kind of loop would I need to use in order to generate two numbers that would divide evenly? Would the loop then just generate two even numbers? Sorry I'm a begginers so I don't really know much yet.

Could you show me an example please?

Here's the pseudocode for you

get upper limit from user

set status_var to false

loop while false
  generate two numbers from 1 to user_limit
  if n1 > n2
     divide n1 / n2, store to quotient
     if quotient * n2 equals n1
        set status_var to true

^What is being meant by set status_var to true/false?

create a variable of type bool. That will store the fact that you have a satisfactory pair of numbers, or not.

I often use the following model

bool done = false;

while( !done )
{
    //do stuff
    if ( quit_condition_exists )
        done = true;
    //maybe do more stuff
}

Notice how the while statement makes sense as you read it out loud?

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.