### 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".
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687`` `````` #include #include 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 <

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.

 ``12`` ``````int counter ; for ( counter = 0 ; ... ) ``````

Here's an alternative version of your code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include 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<

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.