Help understanding the switch() and how to fit it into a math generator.

I previously made a program that just generates two random numbers, and the user must add them together and they get a point. I got a loop to work so that it made 10 questions and it would output their score out of how many questions they had thus far. However I want to take it to the next level and add a switch() so I can randomly generate an operand for the questions. (I'm pretty sure switch is the best way to do that)
My guess would be that on line 65, 69, and 74 where the "+" is. Is where I would put the "switch"? but I don't know how to do that :( ...it does run for the record.

A few of the problems I'm having is that:
1. It doesn't output their score every time. Only every two questions.
2. It only switches between addition and subtraction no matter how many times you've tried it.
3. On line 78 I used to have it say
cout << "Your score is: " << score << " out of" << counter << " . " << endl <<endl;

However this threw an error about not recognizing "counter".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include <iostream>
#include <cstdlib>

using namespace std;


int main()
{
int score = 0;
int ans1;
int num1 = rand() % 10 +1;
int num2 = rand() % 10 +1;
int op = rand() % 3;


    for (int counter = 1; counter <=10; counter ++)

    {
            switch(op)
        {
            case 0: cout << "What is " << num1 << " + " << num2 << " ?" << endl;
            cin >> ans1;
            cout << " " << endl;
            if (ans1 == num1 + num2)
            { cout << "Nice, you got the correct answer" << endl;
            score ++;
            }
            else
            { cout << "Wrong, the answer is " << num1 + num2 << "." << endl; }break;

            case 1: cout << "What is " << num1 << " - " << num2 << " ?" << endl;
            cin >> ans1;
            cout << " " << endl;

            if (ans1 == num1 - num2)
            { cout << "Nice, you got the correct answer" << endl;
            score ++;
            }
            else
            { cout << "Wrong, the answer is " << num1 - num2 << "." << endl; }break;

			case 2: cout << "What is " << num1 << " * " << num2 << " ?" << endl;
            cin >> ans1;
            cout << " " << endl;
            if (ans1 == num1 * num2)
            { cout << "Nice, you got the correct answer" << endl;
            score ++;
            }
            else
            { cout << "Wrong, the answer is " << num1 * num2 << "." << endl; }break;

			case 3: cout << "What is " << num1 << " divided by " << num2 << " ?" << endl;
            cin >> ans1;
            cout << " " << endl;
            if (ans1 == num1/num2)
            { cout << "Nice, you got the correct answer" << endl;
            score ++;
            }
            else
            { cout << "Wrong, the answer is " << num1/num2 << "." << endl; }break;


        }
    cout << "What is " << num1 << " + " << num2 << " ?" << endl;
    cin >> ans1;
    cout << '\n' << endl;

        if (ans1 == num1 + num2)
        { cout << "Nice, you got the correct answer" << endl;
          score ++;
        }                                            
        else
        { cout << "Wrong, the answer is " << num1 + num2 << "." << endl; }



        cout << "Your score is: " << score << " . " << endl <<endl;


	}
    return 0;

}





Thanks in advance! :D
closed account (Dy7SLyTq)
a switch is essentially (not taking in things like speed in to consideration merely its effect) a (imo) simplified if with the major problem of you cant switch strings. so you declare a switch with the variable to be "switched" in the paranthenses. the case is the condition, so case 1: would be like saying if(int == 1). you have to have a break after every case except default. default is the equivalent to an else
Counter does not exist outside the for loop. If you want it to you should define it prior to the for loop.

1
2
int counter ;
for ( counter = 0 ; ... ) 


Here's an alternative version of your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstdlib>

using namespace std;

enum { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION } ;

int main()
{
    int score = 0 ;    

    char const * opStr[] =
    {
        " + ",
        " - ",
        " * ",
        " / "
    };


    for (int counter = 1; counter <=10; counter ++)
    {
        int a = rand() % 10 + 1 ;
        int b = rand() % 10 + 1 ;
        int operation = rand() % 4 ;

        int answer ;
        switch(operation)
        {
        case ADDITION:          answer = a+b; break;          
        case SUBTRACTION:       answer = a-b; break;
        case MULTIPLICATION:    answer = a*b; break;
        case DIVISION:          answer = a/b; break;   // Note that this is integer division.
        }

        std::cout << "What is " << a << opStr[operation] << b << "?\n" ;

        int input ;
        std::cin >> input ;
        
        if ( input == answer )
        {
            std::cout << "Nice, you got the correct answer.\n" ;
            ++score ;
        }
        else
            std::cout << "Wrong.  The answer is " << answer << ".\n" ;

    }

    cout << "Your score is: " << score << " .\n" ;
}
@DTScode ok thanks.
@cire
hmm ok I'm reading this better just a few questions about it:
What is the enum?
How does the char const * opStr[] = work? Like what is that?
and what is the std: : cout<<? why do you need std::?
What is the enum?

In this case the enum (or enumeration) is just a way to declare a few constants.

In enum { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION } ; ADDITION is assigned the value 0, SUBTRACTION is assigned 1, etc.


How does the char const * opStr[] = work? Like what is that?
opStr is an array of pointers to string literals. The first element is " + ", the second element is " - "...

The first element is accessed by the notation opStr[0], the second by opStr[1].


and what is the std: : cout<<? why do you need std::?

It isn 't needed here since I left in the using directive from your code. Just done out of habit.
Topic archived. No new replies allowed.