hi, im facing problems with my codes. here is my bool RequestAnswer & main fcns (fyi, i already declared my class declaration before and it's fine).
when i run it, it will give a question such as:
2/10 + 3/10
and it asks for the answer.
if i enter correct answer, it works and will continue to the next question.
if i enter a wrong answer, it will tell that the answer is wrong and ask to re-enter the answer. the problem is, when i re-enter the answer it will give the "Bad input...." message.
the second problem is when it asks to choose the operator. if i enter an invalid operator, it will give an error message and asks user to re-enter again. but when i re-enter the operator, it will directly jump to system("pause").
please help me to show which part i got wrong, ur help is really appreciated. thanks.
bool RequestAnswer (const Fraction& result);
int main()
{
srand(time(0));
const int NumberOfTest = 5;
char op;
int score = 0, chance = 3;
cout << "\nChoose your arithmetic operator ( *, /, +, or - )\n"
<< "Your choice: ";
cin >> op;
cin.ignore(1, '\n');
if (op == '+' || op == '-' || op == '*' || op == '/')
{
Fraction first_input;
Fraction second_input;
Fraction result;
for (int i = 0; i < NumberOfTest; i++)
{
int first_numerator = (rand() % 5) + 1;
int second_numerator = (rand() % 5) + 1;
first_input.set(first_numerator, 10);
second_input.set(second_numerator, 10);
cout << "\nQuestion number " << (i + 1) << ":\n";
switch(op)
{
case '+':
cout << first_numerator << "/" << "10" << " + "
<< second_numerator << "/" << "10";
result = first_input + second_input;
break;
case '-':
cout << first_numerator << "/" << "10" << " - "
<< second_numerator << "/" << "10";
result = first_input - second_input;
break;
case '*':
cout << first_numerator << "/" << "10" << " * "
<< second_numerator << "/" << "10";
result = first_input * second_input;
break;
case '/':
cout << first_numerator << "/" << "10" << " / "
<< second_numerator << "/" << "10";
result = first_input / second_input;
break;
default:
break;
}
cout << "\n\nWhat is your answer?\n";
if (RequestAnswer(result) == true)
{
cout << "\nYour answer is correct.\n\n";
score++;
}
}
cout << "\n\nYour total score is " << score << " out of 5!\n\n";
}
else
{
cout << "Operator is not define. Choose your operator again (*, /, +, or -):\n";
cin >> op;
cin.ignore(1, '\n');
}
system("pause");
return 0;
}
bool RequestAnswer (const Fraction& result)
{
int NumberOfAttempt = 3;
Fraction answer;
for (int i = 0; i < NumberOfAttempt; i++)
{
if (answer.GetInput() == true)
{
if (result == answer)
{
return true;
}
else
{
NumberOfAttempt--;
if (NumberOfAttempt != 1)
{ cout << "\nYour answer is wrong.\n"
<< "You have " << NumberOfAttempt
<< " more chances to answer this problem. Try again!\n"
<< "What is your answer?\n";
answer.GetInput();
}
else
{
cout << "\nYour answer is still wrong. The correct answer is ";
result.display();
cout << "\n";
}
}
}
else
{
cout << "\nBad input. Try again!\n" << "Your answer is: ";
i--;
}
}
return false;
}