Random Math problem generator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Random Math problem generator

Hello I need help. I am trying to input the function so that should take "+,-,*, or /" as an choice in order to then input a random math problem to solve based on the symbol the user picked however whenever i input one of these symbols it will mark it as wrong or will say it isnt one of the symbols listed above, how can i make it so that the quote " cout << "SORRY, PLEASE ONLY ENTER A SYMBOL LISTED ABOVE!";" doesnt pop up when I actually input a symbol listed above? //C++ Program FINAL PROJECT //Gabriel Reyes, February 5, 2022 CIS-5 #include<iostream> #include<cmath> using namespace std; int main() { char sym_choice; //Symbol Choice int one, two; cout << "\tHello, please Select A symbol to\n"; cout << "\tsolve a random math problem between\n"; cout << "\t(+,-,*,/)\n\n"; cout << "\tENTER YOUR CHOICE: "; cin >> sym_choice; if (sym_choice != '+', '-', '*', '/') { cout << "SORRY, PLEASE ONLY ENTER A SYMBOL LISTED ABOVE!"; } int answer = 0; bool CORRECT = false; one = rand() % 500 + 1; two = rand() % 500 + 1; std::cout << "What is " << one << " " << sym_choice << " " << two << "? "; switch (sym_choice) { case '+': if (answer == one + two) CORRECT = true; break; case '-': if (answer == one - two) CORRECT = true; break; case '*': if (answer == one * two) CORRECT = true; break; case '/': if (answer == one / two) CORRECT = true; break; } if (CORRECT == true) { cout << "Nice job!" << endl; } return 0; }

5th Feb 2022, 8:40 PM
Gabriel Reyes
4 Answers
+ 3
Inside the if block that checks your symbol, add a line ๐Ÿ‘‰return 1;๐Ÿ‘ˆ EDIT: Also change the final print to be ๐Ÿ‘‰cout << answer << "\nNice job!" << endl;๐Ÿ‘ˆ https://code.sololearn.com/c2wl012Yn46D/?ref=app EDIT 2: My version as linked above. Requires the rand function to be seeded. I'll investigate how to do that in C++. it needs: #include<ctime> // in the headers srand (time(NULL)); //in main()
5th Feb 2022, 11:23 PM
HungryTradie
HungryTradie - avatar
+ 3
This is invalid syntax for the conditional expression that you intended: if (sym_choice != '+', '-', '*', '/') Try something like this: if ((int)string("+-*/").find(sym_choice)<0)
5th Feb 2022, 9:26 PM
Brian
Brian - avatar
+ 2
Looks like you forgot to read <answer> value in. I see <answer> was defined as 0, and a prompt for <answer>, but I don't see where <answer> was read in (except you've made some changes in the actual code).
5th Feb 2022, 11:46 PM
Ipang
0
Thank you! but now if I try to input a symbol other than the ones listed below it will proceed with the problem anyways! e.g.: 20 ! 410, 60 ? 200, only after would it output " "SORRY, PLEASE ONLY ENTER A SYMBOL LISTED ABOVE!";"
5th Feb 2022, 10:03 PM
Gabriel Reyes