Home > other >  How to keep count of right answer and wrong answers in C ?
How to keep count of right answer and wrong answers in C ?

Time:11-03

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;
}

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related