| | |
problem with looping
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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;
} 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.
}
while(input is not correct)
{
//Ask for input
//Verify it.
}
Last edited by GDICommander; Aug 31st, 2009 at 10:06 am.
![]() |
Similar Threads
- Problem related to c in looping (C)
- looping through a multi dimentional arrry (PHP)
- Some difficulty with loops (Java)
- Script for accessing child nodes does not work for Firefox... (RSS, Web Services and SOAP)
- i need help with GUI (Java)
- looping to insert event types into schedule (PHP)
- datagrid, innertext, javascript, database update (ASP.NET)
- adding numbers of binary output (C)
- WebCrawler problem (Java)
- Need some help with my do-while and while loops (Java)
Other Threads in the C++ Forum
- Previous Thread: Problem with subexpression in regex
- Next Thread: Why isnt cin.get() working here
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





