Random Number Generator with a twist

Hello, I have to come up with a program that I have to create which is:
The Computer guesses a random calculation "Value1 Operator Value2 = Answer." The computer randomly displays two of the four objects. The Player must guess the remaining two objects. If the player gets one right then the computer says "one is right." The game should continue until the user has provided all the correct calculations.

This is what I have, BUT I have 2 errors that I can't seem to figure out. Any help is greatly appreciated!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221`` ``````#include "stdafx.h" #include #include #include using namespace std; int main() { srand(static_cast(time(0))); //Random Number Generator double value1 = rand() % 50 + 1; //Random number between 1 and 50 double value2 = rand() % 50 + 1; int operatorDefine = rand() % 4 + 1; // random number between 1 and 4 to determine random operator char operator1; double answer; int defineObject1 = rand() % 3 + 1; int defineObject2 = rand() % 3 + 1; double defineObject3; int guess; if (operatorDefine == 1) // if-else to decide random operator { operator1 = '+'; } else if (operatorDefine == 2) { operator1 = '-'; } else if (operatorDefine == 3) { operator1 = '*'; } else if (operatorDefine == 4) { operator1 = '/'; } if (operator1 == '+') // if-else to solve the random equation { (answer = value1 + value2); } else if (operator1 == '-') { (answer = value1 - value2); } else if (operator1 == '*') { (answer = value1 * value2); } else if (operator1 == '/') { (answer = value1 / value2); } cout << "Welcome to Guess My Number, with a twist!\n\n"; if (defineObject1 == 1) //if else to display the first piece of equation { cout << "Value 1 is " << value1 << endl; } else if (defineObject1 == 2) { cout << "Value 2 is " << value2 << endl; } else if (defineObject1 == 3) { cout << "The answer is " << answer << endl; } if (defineObject2 == 1) //if else to display the second piece of the equation { cout << "Value 1 is " << value1 << endl; } else if (defineObject2 == 2) { cout << "Value 2 is " << value2 << endl; } else if (defineObject2 == 3) { cout << "The answer is " << answer << endl; } if (defineObject1 == 1 && defineObject2 == 2) //if else to determine what the player needs to guess { (defineObject3 = answer); } else if (defineObject1 == 2 && defineObject2 == 1) { (defineObject3 = answer); } else if (defineObject1 == 1 && defineObject2 == 3) { (defineObject3 = value2); } else if (defineObject1 == 3 && defineObject2 == 1) { (defineObject3 = value2); } else if (defineObject1 == 2 && defineObject2 == 3) { (defineObject3 = value1); } else if (defineObject1 == 3 && defineObject2 == 2) { (defineObject3 = value1); } cout << "The operation is " << operator1 << endl; cout << "Input the missing piece of the equation." << endl; //Retrieve player's guess cin >> guess; while (guess != defineObject3) // loop to determine if the player was correct { cout << "Sorry, but that is incorrect. Please try again." << endl; cin >> guess; } else (guess == defineObject3); { cout << "Good Job, that is CORRECT!" << endl; } system("PAUSE"); return EXIT_SUCCESS; }``````

The errors I am getting in Visual Studio are:
error C2181: illegal else without matching if, line 212
IntelliSense: expected a statement, line 212

Thank you kindly!
Line 209 is wrong.

Looks like you're trying to supply an argument to else, but else has no argument. It's the "default" case.

You just want:
 ``1234`` ``````else { cout << //whatever... }``````

Ugh actually your whole format here is just no good. Hard to follow. An else has to follow an if, not a while.
Last edited on
Doh.. Not sure how I missed that!

Thanks! corrected it and all is working well.

Best regards,
Frank
Topic archived. No new replies allowed.