-2

I am learning c++ program in my home. So I am not good at using it.

  1. First I want to output the programme like the below statement.
  2. "Question 1: 4 + 5 = 9 correct!
  3. "Question 2: 5 * 5 = 9 fail!
  4. "Question 3: 9 - 5 = 4 correct! I want to produce the question at least 5 and not more than 10 questions. My curiosity is that how can I increase the question number and how can I check that whether I answer this question correct or not. This is so far I'm done. Please do understand my code because I am learning C++ alone in my home

int main()
{
    int a, b, digit1, digit2,;
    char op, k;
    a = digit1;
    b = digit2;
    op = k;

    srand (time (NULL));

    generate2Digits(a,b);
    generateOperators(op);


}

void generate2Digits (int& a, int& b) 
{
    int digit1;
    int digit2;

    srand (time (NULL));

    digit1= rand ()%10;
    digit2= rand ()%10;

    for(int i = 1; i<= rand()%10; i++) 
    {
        cout << digit1 << endl;
        cout << digit2<< endl;
    }


}

void generateOperators(char& op)

{ 
    int k;

    for(int i=1; i<= 10; i++)
    {
        k = rand () % 4;

        switch(k)
        {
            case 0: cout << "+" << endl;
            break;
            case 1: cout << "-" << endl;
            break;
            case 2: cout << "*" << endl;
            break;
        }

    }


}

void printExpression(int a, int b, char op)
{
    do
    {






    }
}

// how to print the question expression like the above example? // how to check the whether the answer is correct or not?

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
hoi
  • 11
  • 1
  • 4

1 Answers1

2

In generate2Digits(), you are generating and printing the numbers 10 times via a loop right away.

I think you should just find the numbers and wait till you find the operator.

Since you got functions to generate digits (generate2Digits()) and operand (generateOperators()), you could write a loop to repeat n times (where n is the number of questions) and call the functions in each iteration of the loop.

Let's modify generate2Digits() to

void generate2Digits (int& a, int& b)
{
    a=rand()%10;
    b=rand()%10;
}

a and b are passed by reference and changes made here will be reflected back to the actual arguments.

In this function, we just find 2 values and store them in a and b.

Now

int generateOperators(char& op, int a, int b)
{
    int k = rand () % 3;
    switch(k)
    {
        case 0: op='+';
            return a+b;
        case 1: op='-';
            return a-b;
        case 2: op='*';
            return a*b;
    }
}

In your program you used k = rand () % 4 but didn't have a case for 3. Since you don't have such a case, you may use k = rand () % 3;.

An operand is given to op and to make things easier, the values of a and b are also available here and based on the chosen operator, the correct answer is also calculated and returned. Here I return this value and make the return type int instead of void. You could use a parameter passed by reference if you like.

I didn't use breaks inside the switch here because return will take the control out of the case anyway.

Now

void printExpression(int n)
{
    int a, b, ans, res;
    char op;
    for(int i=0; i<n; ++i)
    {
        generate2Digits(a, b);
        res=generateOperators(op, a, b);
        cout<<a<<op<<b<<"=";
        cin>>ans;
        if(ans==res)
        {
            cout<<" correct!";
        }
        else
        {
            cout<<" fail!";
        }
        cout<<endl;
    }
}

Make a loop to iterate n times where n may be the number of questions. On each iteration, the 2 numbers are found using generate2Digits() and operand and correct answer is found with generateOperators().

The use enters a number as answer and if this answer is same as the value calculated by generateOperands() here, the answer is correct.

You need to use the srand() only once and before the first rand() in here. The one in main() is enough. See this.

I see that you don't consider division. If you need that you must be using floating point type like double instead of int.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • Hi I really appreciate what you are done for me! But I want to use another function such as int result(a,b,op), so that I can check result there! – hoi Nov 01 '17 at 12:45
  • @hoi You can do what's done in the last part of `printExpression()` in that function then – J...S Nov 01 '17 at 16:29
  • do you know how to count the number that is correct? – hoi Nov 02 '17 at 08:40
  • 1
    use that function to calculate and return the result, then match it to the user input. if its correct, increment a counter. – elanor Nov 02 '17 at 12:19