Console App Goes Crazy - Ignoring i/o [c++]

Started by
2 comments, last by b2b3 19 years, 4 months ago
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
.:<<-v0d[KA]->>:.
Advertisement
If you call the first part that works first, then the second part, the problem may be that the second cin.getline is reading the '\n' character left in the buffer from before
Such has happened to me anyway
Try cin.ignore() before the offending
cin.getlines

this will ignore this '\n' char and (hopefully)
read the valid input

Hope this helped
Also, remember that cin.ignore doesn't always work, here's a better way of doing the cin.ignore, it will also clear the buffer.

P.S. If I'm way off track, then just ignore my post, otherwise cheers :)

Anyways, as I was saying, this is a nifty little function that I've made to use in my programs to help when I run into this error. It also works if you are doing a icn to an int, and someone decides to type someting like MEAT, as my Professor likes to type lol.

/*******************************************************	Function:		FlushCin	 Description:   This function clears extra characters out 	  of the buffer	Parameters:		void	Returns:		void********************************************************/void FlushCin(void) {    cin.clear();     cin.ignore(cin.rdbuf()->in_avail() , '\n');    return;  } 


Please, let me know if that doesn't work, I may have typed it in wrong or something, I am known to make lots of typo's lol.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Why are you using cout << Question.c_str()? cout has overloaded << for strings too, so you can use cout << Question (of course, if it's a string).

This topic is closed to new replies.

Advertisement