0

Currently working on addition program that will loop until the user enters "n".

It will generate two random numbers and display to the user to add them. The user will then input the answer and the program with check if the answer is right or wrong.

My code is working fine however I need help for my code below to keep count of right and wrong answers.

I have not tired anything because I do not know how to do it.

/******************************************************************************
Basic Template for our C++ Programs.
STRING
*******************************************************************************/
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string> // String managment funtions.
#include <iostream> // For input and output
#include <cmath> // For math functions.
#include <math.h>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////////

int main()
{
    srand(time(0));

    string keepgoing;
    do
    {
        const int minValue = 10;
        const int maxValue = 20;

        int y = (rand() % (maxValue - minValue + 1)) + minValue;
        // cout<< " the random number is y "<< y << endl;
        int x = (rand() % (maxValue - minValue + 1)) + minValue;
        // cout<< " the random number is x "<< x << endl;


        cout << " what is the sum of " << x << " + " << y << " =" << endl;
        int answer;
        cin >> answer;

        


        if (answer == (x + y))
        {
            cout << "Great!! You are really smart!!" << endl;
           
        }

        else
        {
            cout << "You need to review your basic concepts of addition" << endl;
          
        }
       

        cout << "Do you want to try agian [enter y (yes) or n (no) ]";
        cin >> keepgoing;

    } while (keepgoing == "y");
    return 0;
}
  • Why are you creating new `right` and `wrong` variables in the loop? So, you will start from `0` in each iteration. Also, why are you directly incrementing `wrong`? – mch Nov 02 '21 at 19:53
  • @mch am a straight beginner i don't know how to do this i removed those when i realized i left that while i was playing with my code – user17312577 Nov 02 '21 at 19:54
  • Especially when a beginner, start small. Write a tiny program that does one easily testable thing. Once that works exactly the way you want it, add one more thing. Repeat until the program does everything you need it to do. this keeps the complexity that you have to deal with at any one time to an absolute minimum. – user4581301 Nov 02 '21 at 20:02
  • If you are a _straight beginner_, you may want to read [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) through early chapters until you are comfortable with concepts such as counting. It would certainly go faster than writing a new question for each new thing you want to try. – Drew Dormann Nov 02 '21 at 20:02

1 Answers1

0

To make life easier, let's use two variables:

unsigned int quantity_wrong_answers = 0U;
unsigned int quantity_correct_answers = 0U;

(This should go before the do statement.)

When you detect a correct answer then increment one of these variables:

if (answer = (x+y))
{
    ++quantity_correct_answers;
}
else
{
    ++quantity_wrong_answers;
}

Before returning from main, you could print the statistics.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154