I haven't touched a C++ project in a rather long while, so bear with me here [smile]:
There a really strange error in my supposedly simple project, and I have no idea what the reason could be. In certain cases, my DOS program goes a bit haywire. I'm not exactly sure how to explain the problem here... the effect is the program starts to ignore all my std::cin's and getline's, allowing no more chance for user input. Getting out of the program requires you to answer all of the questions correctly (it's a quizzer program), and without any input, all it does is spit out the questions indefinately!
I had this program for ages, and it worked fine. Then I attempted to improve it, and that's what killed it.
A little more in-depth explanation: The program asks questions written in a file in random order, until you get them all right. there are 3 types of questions the program can ask, and I have seperate code to handle them. The question type that my program was built for originally (fill in) works fine. The new question type (multiple choice) I just added, and another (true/false) I added a few months ago don't work. As a matter of fact, they're the cause of the error. When you press enter after typing the answer, the program goes haywire, ignoring all input (but not output).
Here's my code to handle a fill in question, the one that works fine. It's defined within the CFillInQuest class, the class to handle all fill-in questions.
bool AskQuest()
{
// First, output the question
std::cout << Question.c_str()
<< std::endl
<< "Guess: ";
// Prompt for an answer
std::cin.getline( guess, sizeof( guess ) );
if( CompareAns() ) // Does the answer match any of the accepted answers?
{
std::cout << "Correct!\n\n";
return true;
}
else // Output all accepted answers
{
std::cout << "Wrong! Accepted answers:" << std::endl;
for( int b = 0; b < NumDefined; b++ )
std::cout << Correct.c_str() << std::endl;
std::cout << std::endl;
return false;
}
return false; // This shouldn't get executed
}
Now, here's some faulty code for handling multiple choice questions (defined in CMultChoiceQuest class):
bool AskQuest()
{
RandomizeChoices(); // Randomize the order of choices (a,b,c,d)
std::cout << std::endl;
std::cout << Question.c_str(); // Output question
std::cout << std::endl
<< "A. " << Choices[ ChoiceOrder[ 0 ] ].c_str() << " "
<< "B. " << Choices[ ChoiceOrder[ 1 ] ].c_str();
std::cout << std::endl
<< "C. " << Choices[ ChoiceOrder[ 2 ] ].c_str() << " "
<< "D. " << Choices[ ChoiceOrder[ 3 ] ].c_str();
std::cout << std::endl
<< "Answer: ";
std::cin.getline( guess, 2 ); // Prompt only for one letter (a || b || c || d)
if( AnsCompare() )
{
std::cout << "Correct!\n\n";
return true;
}
else
{
std::cout << std::endl
<< "Wrong! Correct answer: "
<< correct
<< std::endl << std::endl;
return false;
}
return false;
}
All functions/variables are defined, and there are no compiler errors. Any ideas? Need more code?
EDIT: Commented code