Hi i am doing a program that has to add up the percentage for example if i get one question wrong it adds to the percentage and after i answer 20 questions i would get the final mark but i dont get why it doesnt work.

void multiplier(int difficulty)// function to multiply values
{
    int answer = 0;
    int num1 = 0;
    int num2 = 0;
    int rightanswer = 0;
    int total = 0;
    
    srand (time(0));

    for (int counter = 0; counter < 20; counter++)
    {
        
        if (difficulty == 1)
        {
            num1 = 1+rand()%9;
            num2 = 1+rand()%9;
        }
        else if (difficulty == 2)
        {
            num1 = 1+rand()%99; 
            num2 = 1+rand()%99;
        }
        
        rightanswer = num1 * num2;
        while(answer != rightanswer)
        {
            cout << "What is " << num1 << " * " << num2 <<"?"<< endl;
            cin >> answer;
            cout <<endl;

            if (answer == rightanswer)
            {
                rightresponse();
            }
            else
            {
                wrongresponse();
            }
            cout <<endl;
            total += answer;//  add the answer to total value
        }// end while
    }//end for
     evaluate(total);


void evaluate(int total)
{

    int percent;
    
    percent = total / 20 * 100;
    cout << "Percentage is " << percent << "%"<< endl;
    
    if (percent < 75)// if the percentage is less than 75
    {
        cout << "Please ask instructor for extra help" << endl;
    }
    else // if greater than 75
    {
        cout << "Well done! " << endl;
    }

}

Hi i am doing a program that has to add up the percentage for example if i get one question wrong it adds to the percentage and after i answer 20 questions i would get the final mark but i dont get why it doesnt work.

void multiplier(int difficulty)// function to multiply values
{
    int answer = 0;
    int num1 = 0;
    int num2 = 0;
    int rightanswer = 0;
    int total = 0;
    
    srand (time(0));

    for (int counter = 0; counter < 20; counter++)
    {
        
        if (difficulty == 1)
        {
            num1 = 1+rand()%9;
            num2 = 1+rand()%9;
        }
        else if (difficulty == 2)
        {
            num1 = 1+rand()%99; 
            num2 = 1+rand()%99;
        }
        
        rightanswer = num1 * num2;
        while(answer != rightanswer)
        {
            cout << "What is " << num1 << " * " << num2 <<"?"<< endl;
            cin >> answer;
            cout <<endl;

            if (answer == rightanswer)
            {
                rightresponse();
            }
            else
            {
                wrongresponse();
            }
            cout <<endl;
            total += answer;//  add the answer to total value
        }// end while
    }//end for
     evaluate(total);


void evaluate(int total)
{

    int percent;
    
    percent = total / 20 * 100;
    cout << "Percentage is " << percent << "%"<< endl;
    
    if (percent < 75)// if the percentage is less than 75
    {
        cout << "Please ask instructor for extra help" << endl;
    }
    else // if greater than 75
    {
        cout << "Well done! " << endl;
    }

}

There are several problems I can see in your code.

This is one of them:

total += answer;//  add the answer to total value

So each time your user answers a question (whether they were correct or not) you're adding the answer to the total. So if the answer was 81, the total would be incremented by 81?!!
Surely you just want to add one to the total and then only when the user answers correctly, not every time they make an answer...So something like this perhaps? :

if (answer == rightanswer)
            {
                rightresponse();
		total++;	// total only gets incremented
            }			// when answer is correct!
            else

Also, you're using a while loop when you ask the user for an answer, but you're asking them repeatedly until they get the answer correct...So that way, they can never get a wrong answer...

But most crucially, in your percentage calculation total is an int.
Bearing that in mind, if your user got 12 out of 20, then in your percentage calculation:
12 divided by 20 = 0.6
Now because total is an int and the result is less than 1, the 0.6 gets rounded down to 0.
And as we all know 0 * 100 = 0.

So to get around this you need to cast total to a float in your calculation and then cast the result back to an int like this:

percent = (int) ((float)total / 20 * 100);

So your final code for your two functions should look something like this:

void multiplier(int difficulty)// function to multiply values
{
    int answer = 0;
    int num1 = 0;
    int num2 = 0;
    int rightanswer = 0;
    int total = 0;
    
    srand (time(0));

    for (int counter = 0; counter < 20; counter++)
    {
        
        if (difficulty == 1)
        {
            num1 = 1+rand()%9;
            num2 = 1+rand()%9;
        }
        else if (difficulty == 2)
        {
            num1 = 1+rand()%99; 
            num2 = 1+rand()%99;
        }
        
        rightanswer = num1 * num2;

	// While loop snipped!!

            cout << "What is " << num1 << " * " << num2 <<"?"<< endl;
            cin >> answer;
            cout <<endl;

            if (answer == rightanswer)
            {
                rightresponse();
		total++;	// total gets incremented
            }			// when answer is correct!
            else
            {
                wrongresponse();
            }
            cout <<endl;

    }//end for
     evaluate(total);
}

void evaluate(int total)
{

    int percent;
    
    percent = (int) ((float)total / 20 * 100);
    cout << "Percentage is " << percent << "%"<< endl;
    
    if (percent < 75)// if the percentage is less than 75
    {
        cout << "Please ask instructor for extra help" << endl;
    }
    else // if greater than 75
    {
        cout << "Well done! " << endl;
    }

}

I'm guessing your while loop was really intended to validate user input to stop them from entering characters instead of numbers.
So... while user is entering nonsense - repeat the question kinda thing...
If this is the case then perhaps you should be taking the users input as a string and then parsing it to make sure that contains only integers. If the entered data is valid, copy it to an int variable and then compare that against the answer to find out whether the users answer was correct or not and set a condition so that the while loop exits and the next question can be asked.
But if the user enters invalid input (i.e. characters instead of numbers) then the while loop should re-iterate and ask the question again.

If this is what you need to be doing I'll leave it to you to work that out! I'm not doing all of your homework for you! ;)

Anyway, I'm guessing the main issue there was the percentage calculation!

Cheers for now,
Jas.

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.