well it will be good looking,easier to understand.But when compiled it will be the same as the if else statement so it's the same thing.
Also using clases and operations will make it make the code so much cleaner and better to be understood.
well you can solve this problem i n another way.
let's divide this problem:
the main 2 operations here are:
1-choose a random number.
2-choose a random operator.
let's start with the first:
1-choose a rendom number:
We will make a function that will generate a random number for us let's call rand_number.
the random number can be:
a)between 0-9(if 10 questions)
b)between 0-19(if 20 questions)
c)between 0-29(of 30 questions)
So there is a parameter witch is the number of the question provided by the user that will be passed to this function.
2-choosing the random operator:
this one is easy you can put it as a function.let's name it rand_op().a little trick will be used here so we are going to need the generated number as a parameter for this function.to put this one up we need a table that contain the operators needed (/ - + *) and the random function will choose a number between 0 to 3 (again the rand_number function will be used).
Now the main program.
we will repeat the process(choosing a number and choosing an operator) the needed times.in it we will be printing the operation and calculating the result.
PHP:
#the includes here
using namespace ..
int rand_nuber(int n)
{//returns a random integer between 0 and n}
void rand_op(int n,int &res)
{
/* OP is a table contains the operators needed (/ * + -)
n is the generated number by rand_number will be used in the operation.
res will store the result of the operation. */
ope=rand_number(3);
cout<< n + op[ope];
switch ope
case 0
res=res+n;
break;
case 1
res=res-n;
break;
case 2
res=res*n;
break;
case 3
res=res/n;
break;
}
void main()
{//vars here.
//get the number of questions
max= num_question % 10;
for (i=0;i<=num_question;i++)
counter=0;
do{
x=rand_number(num_question-1);
if (counter != max)
rand_op(x,&res);
}while (max!=counter)
cout<< " = ??"
//get the result from user
//compare with the var res that u have
//tell if he's wrong or not.
}
}
}
NOTICE: this solution won't respect operation priority cause it's calculating while generating numbers and operations.just make sure to put a notice in your program about it or just make it respect it.think about it just a little more.