Code:
int start_game(int difficulty, int score){
int integer1=0, integer2=0, integer3=0, integer4=0, operand1 = -1, operand2 = -1, operand3 = -1, check, counter = -1;
double real_ans, user_ans;
char char_op1, char_op2, char_op3;
do
{
system("cls");
switch(difficulty)
{
case 1:
integer1 = (rand()%9)+1;
integer2 = (rand()%9)+1;
integer3 = (rand()%9)+1;
operand1 = (rand()%2);
operand2 = (rand()%2);
if(operand1 == 0)
{
char_op1 = '+';
}else{
char_op1 = '-';
}
if(operand2 == 0)
{
char_op2 = '+';
}else{
char_op2 = '-';
}
printf("Equation %d: %d %c %d %c %d\n\n", (counter+2), integer1, char_op1, integer2, char_op2, integer3);
break;
case 2:
integer1 = (rand()%9)+1;
integer2 = (rand()%9)+1;
operand1 = (rand()%2)+2;
if(operand1 == 2)//*
{
char_op1 = 'x';
}else{
char_op1 = '/';
}
printf("Equation %d: %d %c %d\n\n", (counter+2), integer1, char_op1, integer2);
break;
case 3:
integer1 = (rand()%9)+1;
integer2 = (rand()%9)+1;
integer3 = (rand()%9)+1;
operand1 = (rand()%4);
operand2 = (rand()%4);
switch(operand1)
{
case 0:
char_op1 = '+';
break;
case 1:
char_op1 = '-';
break;
case 2:
char_op1 = 'x';
break;
case 3:
char_op1 = '/';
break;
}
switch(operand2)
{
case 0:
char_op2 = '+';
break;
case 1:
char_op2 = '-';
break;
case 2:
char_op2 = 'x';
break;
case 3:
char_op2 = '/';
break;
}
printf("Equation %d: %d %c %d %c %d\n\n", counter, integer1, char_op1, integer2, char_op2, integer3);
break;
case 4:
integer1 = (rand()%9)+1;
integer2 = (rand()%99)+1;
integer3 = (rand()%9)+1;
operand1 = (rand()%4);
operand2 = (rand()%4);
switch(operand1)
{
case 0:
char_op1 = '+';
break;
case 1:
char_op1 = '-';
break;
case 2:
char_op1 = 'x';
break;
case 3:
char_op1 = '/';
break;
}
switch(operand2)
{
case 0:
char_op2 = '+';
break;
case 1:
char_op2 = '-';
break;
case 2:
char_op2 = 'x';
break;
case 3:
char_op2 = '/';
break;
}
printf("Equation %d: %d %c %d %c %d\n\n", (counter+2), integer1, char_op1, integer2, char_op2, integer3);
break;
case 5:
integer1 = (rand()%999)+1;
integer2 = (rand()%9)+1;
integer3 = (rand()%9)+1;
integer4 = (rand()%99)+1;
operand1 = (rand()%4);
operand2 = (rand()%4);
operand3 = (rand()%4);
switch(operand1)
{
case 0:
char_op1 = '+';
break;
case 1:
char_op1 = '-';
break;
case 2:
char_op1 = 'x';
break;
case 3:
char_op1 = '/';
break;
}
switch(operand2)
{
case 0:
char_op2 = '+';
break;
case 1:
char_op2 = '-';
break;
case 2:
char_op2 = 'x';
break;
case 3:
char_op2 = '/';
break;
}
switch(operand3)
{
case 0:
char_op3 = '+';
break;
case 1:
char_op3 = '-';
break;
case 2:
char_op3 = 'x';
break;
case 3:
char_op3 = '/';
break;
}
printf("Equation %d: %d %c %d %c %d %c %d\n\n", (counter+2), integer1, char_op1, integer2, char_op2, integer3, char_op3, integer4);
break;
}
//printf("%c", char_op1);
real_ans = get_equation(integer1, integer2, integer3, integer4, operand1, operand2, operand3, difficulty);
//printf("%lf\n", real_ans);
//printf("%d\n%d\n", operand1, operand2);
user_ans = get_answer();
check = compare_numbers(real_ans, user_ans);
if(check == 1)
{
printf("Good Job! Try another\n");
switch(difficulty)
{
case 1:
score +=10;
break;
case 2:
score += 50;
break;
case 3:
score += 200;
break;
case 4:
score += 1000;
break;
case 5:
score += 5000;
break;
}
getch();
}else{
score -= 50;
printf("Wrong answer, the answer is %.2lf\n", real_ans);
getch();
}
i++;
}while(counter<10);
return score;
}
This code is the main code for the game with other functions around it. However, I cannot figure out how to fine the right answer for the equations that are randomly generated. Does anyone know how I can go about using the appropriate order of operations to do this. For example: say my code randomly generates the equation y = 7 + 2 * 8. How can I tell the computer to compute the 2*8 before the 7+2 but be general enough to compute an equation like 7 / 2 * 8. See what I mean?