• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Getting an infinite loop in MS Visual Basic

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Tipycol

Member
Joined
Aug 20, 2002
I'm trying to learn C++, and right now I'm trying to make an idiot proof program (ie: enter in all the wrong values) so I use while loops. But I have a problem. If I input a letter where a number should go, the program enters an infiniteloop.

Here's an exmaple of the code I'm using:

double Water;

cout<<"Enter the amount of water used: ";
cin>>Water;

while (Water<0){
cout<<"Value must be zero, or a positive number"<<endl;
cout<<"Enter the amount of water used (in gallons): ";
cin>>Water;
}

When run and a letter is entered for Water, this is what gets displayed:

Enter the amount of water used: Value must be zero, or a positive number
Enter the amount of water used: Value must be zero, or a positive number
Enter the amount of water used: Value must be zero, or a positive number
etc.

What I should do to prevent the loop from happening?

Thanks
Tipycol
 
Can you use an if statement to check whether the input is a letter or number. You could check using ascii values. Why is the title "Getting an infinite loop in MS Visual Basic but you're using C++. Just wondering.

Clint
 
I've had this problem before... the problem is that after cin>>Water there is still some input left in the input buffer (such as a carriage return or something similar) and subsequent calls to cin>>Water will not actually wait for input but get the data that is already in the buffer.

Here is an example of how to clear the input buffer. Put a call to the function after both cin>>Water statements and you should be fine.

Code:
//----------------------------------------------------------------
// ClearError
//----------------------------------------------------------------
void ClearError(istream& istr)
{
	//--------------------------------------------------------
	// This code was taken from Visual Studio (VC++) online 
	// help files and modifed to B209 conventions. This 
	// function fully resets anything left in the input buffer.
	//--------------------------------------------------------
	const int	BUFFER_LENGTH = 20;

	streambuf*  buffer;
	char        tempBuffer[BUFFER_LENGTH];
	int         count;


	istr.clear();					// Clear error flags
	buffer = istr.rdbuf();			// Get streambuf pointer
	count = buffer->in_avail();		// Number of characters in buffer

	while(count)					// Extract them to tempBuffer
	{
		if(count > BUFFER_LENGTH)
		{
			buffer->sgetn(tempBuffer, BUFFER_LENGTH);
			count -= BUFFER_LENGTH;
		}
		else
		{
			buffer->sgetn(tempBuffer, count);
			count = 0;
		}
	}
}

//----------------------------------------------------------------

for example,

Code:
double Water;

cout<<"Enter the amount of water used: ";
cin>>Water;
ClearError(cin);

while (Water<0){
   cout<<"Value must be zero, or a positive number"<<endl;
   cout<<"Enter the amount of water used (in gallons): ";
   cin>>Water;
   ClearError(cin);
}

NOTE: There are other ways of clearing errors in cin, however I have found this method to be best for all situations so far.
 
seadave77 said:
...Why is the title "Getting an infinite loop in MS Visual Basic but you're using C++. Just wondering.

Clint

Whoops, sorry bout that. I've been used to saying MS Visual Basic ;)


Thanks for your help VeryFirstSMP, the ClearError() function did the trick.

Tipycol
 
Back