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;
}

Dani AI

Generated

Two separate bugs are visible in the code: the operator-selection path never loops (so an invalid operator is only read once), and the answer-retry path is tripping over stream state and confusing loop control. was right to suggest looping the operator prompt; the fix is to keep asking until a valid token is read. For robustness, read a whole line and parse the first non-space character so leftover input can’t sneak through.

A simple, robust operator loop (read a line and test the first char) avoids leftover-char problems and is user-friendly:

std::string line;
char op;
do {
  std::cout << "Choose operator (* / + -): ";
  if (!std::getline(std::cin, line)) break; // handle EOF
  if (line.empty()) continue;
  op = line[0];
} while (op != '+' && op != '-' && op != '*' && op != '/');

The repeated “Bad input” on re-entry is almost always caused by the input stream remaining in an error state after a failed extraction. If Fraction::GetInput uses operator>> and the format is wrong, cin.fail() becomes true and subsequent reads immediately fail. Fixes: (1) don’t mutate the loop bound variable inside the loop; use a separate attempts counter. (2) when GetInput fails, clear the stream and discard the rest of the line before retrying:

int attempts = 3;
while (attempts > 0) {
  if (!answer.GetInput()) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Bad input. Try again.\n";
    continue; // do not count this as an attempt
  }
  if (answer == result) return true;
  --attempts;
  std::cout << "Wrong answer. " << attempts << " left.\n";
}

Also: avoid changing NumberOfAttempt inside the for loop, and consider letting Fraction::GetInput consume the remainder of the input line or at least document its contract. Finally, system("pause") is non-portable; use a portable wait or a final prompt if you need the console to stay open.

Recommended Answers

All 2 Replies

It is normal that you jump directly to system("pause") when the operator is not correct the first time. You are leaving the if-else clause after the second input prompt. You should do something like...

while(input is not correct)
{
//Ask for input
//Verify it.
}

i see.... alrite, thanks for the clue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.