Jump to content
  • Advertisement
Sign in to follow this  
ramen

Weird output (I am sure this topic name has been used before)

This topic is 5008 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I am having a weird program output. When I input any decimal or integer besides 1.999 the program works properly. When I input 1.999 the program repeats infinitly which out stopping for input, just keeps outputting as fast as it can "Please enter a number between 2 and 1000: _". Below is the code I am having trouble with, and below that is the full code. problematic code
void prime::inputnum() {
	while((number < 2) || (number > 1000)) { // will repeat until user enters in an
											  // acceptable number
	
	cout << "Please enter a number between 2 and 1000: _";
	cin >> number; // gets user input and stores as variable number
	}

}
full code
#include <iostream.h>
#include <math.h>
#include <stdlib.h>

// class to organize all the variables and functions for this program
class prime {
	int number; // number in integer form
	void inputnum(); // this prompts a user for a number between 2 and 1000.

public:
	prime(); // default constructor
	void initiate(); // main function for this program	
};

// default constructor
prime::prime() 
:number(0)
{

}

void prime::initiate(void) {

	inputnum(); // This function prompts for a number between 2 and 1000.


}
 // This function prompts for a number between or equal to 2 and 1000.
void prime::inputnum() {
	while((number < 2) || (number > 1000)) { // will repeat until user enters in an
											  // acceptable number
	
	cout << "Please enter a number between 2 and 1000: _";
	cin >> number; // gets user input and stores as variable number
	}

}

int main (void) {

	prime p; // default call
	p.initiate();

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
I believe the problem is in the parsing algorythm. When it sees the . it will start to flip out and spam the data. Try "3/" It'll flip out with that too I think.

Now to fix the problem instead of int, use float, but because this is a prime thing, just don't use anything other then integers, maybe?

Share this post


Link to post
Share on other sites
The problem occurs becuase the buffer is not cleared from the bad inputed data.
When you put in 1.999, your input buffer looks like this "1.999"
cin>>integer reads in the "1", now your input buffer looks like this ".999".

On the next iteration cin>>integer will look at "." and see that this is not an integer so will leave the integer unchanged and the buffer is still ".999"
This will continue upon subsequent iterations and will never stop since ".999" will always be left in your input buffer.

In order for the computer to ask you for input you buffer must be clear.
So what you do before each cin , is call
cin.clear();
flushBuffer();


You can find all this info on google =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!