Help me to fix the error[solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Help me to fix the error[solved]

Can anyone help me to fix the error #include<iostream> #include<ctime> using namespace std; class math{ public: int x,y; int rands(); { x=(1+ rand()%10); y=(1+ rand()%10); *num1=x; *num2=y; return num1,num2; } }; int main() { srand(time(0)); int num1,num2,ans,c_ans,score=0; math R; for(int i=1;i!=20;i++) { R.rands(); c_ans=num1+num2; cout << "\n"<<num1<<"+"<<num2<<"=?\n"; cin>>ans; if(ans==c_ans) { score++; cout<<"you are correct\nyour score is:"<<score<<"\n\n"; } else { cout<<"your are wrong\n\n"; } } cout<<"your final score is:"<<score; return 0; }

7th Nov 2021, 4:55 PM
Samaan Sayed
5 Answers
+ 3
oh my gosh, it's so painful to read the OP's code. so I cleaned it, so you could read your own code clearly and also Jayakrishna🇮🇳 it is considered a bad practice to make global variables. https://code.sololearn.com/cJ5Zs8Ez3iU7/?ref=app
7th Nov 2021, 5:22 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 2
Thanks Jayakrishna🇮🇳 for helping me out ❤️
7th Nov 2021, 5:29 PM
Samaan Sayed
+ 1
#include<iostream> #include<ctime> using namespace std; int num1,num2; //declaring globally so you can use anywhere in program and modifies class math{ public: int x,y; void rands() { x=(1+ rand()%10); y=(1+ rand()%10); num1=x; num2=y; //return x,y; //using void , dont need return and you cant return more than one value so made void rands() } }; int main() { srand(time(0)); int ans,c_ans,score=0; math R; for(int i=1;i!=20;i++) //better to reduce limit { R.rands(); c_ans=num1+num2; cout << "\n"<<num1<<"+"<<num2<<"=?\n"; cin>>ans; if(ans==c_ans) { score++; cout<<"you are correct\nyour score is:"<<score<<"\n\n"; } else { cout<<"your are wrong\n\n"; } } cout<<"your final score is:"<<score; return 0; } //read comments and post reply if you not understand about chages..
7th Nov 2021, 5:13 PM
Jayakrishna 🇮🇳
+ 1
You're welcome.. you can use directly num1,num2 instead of x,y variables so they don't need x,y.
7th Nov 2021, 5:34 PM
Jayakrishna 🇮🇳
0
Rellot's screwdriver that may be write but I went to tell you to use like that. Otherwise it need more changes like passing variable and getting back. Not thought about instance variable. Your idea also good.. I thought only small changes if OP code . Thought only about the way of returning back both values... But not bad here in the small program. Its tells global exists to newers ..
7th Nov 2021, 5:54 PM
Jayakrishna 🇮🇳