| alfreddoozz (9) | |
|
hello! guys i am new here i am learning c++ in my university and i have an assignment for 2morrow. in this assignment i am asked to make three functions (which i did ) and call one of these three functions from main randomly. any idea how to do that really need help thank you in advance ! | |
|
|
|
| Moschops (5959) | |
|
You could use the rand function to generate a random nubmer and use that as the basis of the choice of function. http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ | |
|
|
|
| Timbo1 (91) | |||
|
You could use random numbers and then use: In your main bit of code put
Just replace "Function1" with the name of your function. Then replace "12, NULL" with any parameters you might have. | |||
|
Last edited on
|
|||
| alfreddoozz (9) | |
|
thank you for your reply i did not really understand what to do. here is my code : because i cant seem to be able to explain my problem #include<iostream.h> #include<iomanip.h> #include<stdlib.h> void addition(){ int result=0; int points=0; int counter=0; int nbr1 = 100 % rand() +10; int nbr2 = 100 % rand() +10; cout<<nbr1<<" + "<<nbr2<<" = "; cin>>result; if(result == nbr1+nbr2){ cout<<" CORRECT "; points+=10; return; } if(result !=nbr1+nbr2){ counter++; cout<<"WRONG TRY AGAIN"; } if (counter == 1){ points+=5; } if (counter == 2){ points+=2; } if (counter >= 2){ points=0; cout<<" WRONG!the anwser is "<<nbr1+nbr2<<endl; return; } } void substraction(){ int result=0; int points=0; int counter=0; int nbr1 = 100 % rand() +10; int nbr2 = 100 % rand() +10; cout<<nbr1<<" - "<<nbr2<<" = "; cin>>result; if(result == nbr1-nbr2){ cout<<" CORRECT "; points+=10; return; } if(result !=nbr1-nbr2){ counter++; cout<<"WRONG TRY AGAIN"; } if (counter == 1){ points+=5; } if (counter == 2){ points+=2; } if (counter >= 2){ points=0; cout<<" WRONG!the anwser is "<<nbr1-nbr2<<endl; return; } } void multiplication(){ int result=0; int points=0; int counter=0; int nbr1 = 100 % rand() +10; int nbr2 = 100 % rand() +10; cout<<nbr1<<" * "<<nbr2<<" = "; cin>>result; if(result == nbr1*nbr2){ cout<<" CORRECT "; points+=10; return; } if(result !=nbr1*nbr2){ counter++; cout<<"WRONG TRY AGAIN"; } if (counter == 1){ points+=5; } if (counter == 2){ points+=2; } if (counter >= 2){ points=0; cout<<" WRONG!the anwser is "<<nbr1*nbr2<<endl; return; } } int main(){ return 0; } ok now inside main i want to call multilpication, addition and substraction but i want to call them randomly. here is what the teahcer wants: Choose operation randomly (addition, substraction or multiplication) any idea on how to do that ?? | |
|
|
|
| alfreddoozz (9) | |
| i will try that ! thank you so much for your help really appriciate it :) | |
|
|
|