943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 367
  • C++ RSS
Aug 31st, 2009
0

problem with looping

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
methmignonne is offline Offline
11 posts
since Aug 2009
Aug 31st, 2009
0

Re: problem with looping

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.
}
Last edited by GDICommander; Aug 31st, 2009 at 10:06 am.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Aug 31st, 2009
0

Re: problem with looping

i see.... alrite, thanks for the clue.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
methmignonne is offline Offline
11 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with subexpression in regex
Next Thread in C++ Forum Timeline: Why isnt cin.get() working here





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC