Hi,

Im trying to filter a user number entered by the user so that it is between 3 and 31 and odd. I also want the prompt to enter the user number to loop if the user number entered does not meet the requirements.

This is what i have:

bool validUserInput (true);
	do {
	cout << "Please enter an odd value between 3 and 31: " << endl;
	cin >> rate;
	myWaveForm.getSampleValue( rate );
	
	if ( rate % 2 != 0 ) validUserInput = false ;  
   	if ( rate < 3 ) validUserInput = false;
	if ( rate > 31 ) validUserInput = false;
	

	
	}   
while  (validUserInput);

This works perfectly within the range of between 3 and 31 and being odd but allows any number regardless of odd or even outside of the limits. What am i doing wrong?

>bool validUserInput (true);
This guy should be reset inside the loop. Right now if you fail once, it fails forever.

>if ( rate % 2 != 0 ) validUserInput = false ;
When rate % 2 is 0, the number is even. This test will set validUserInput to false when the number is odd, which isn't what you want.

>while (validUserInput);
Perhaps you meant while ( !validUserInput ) .

Compare and contrast:

bool validUserInput;

do {
  validUserInput = true;

  cout << "Please enter an odd value between 3 and 31: " << endl;
  cin >> rate;

  myWaveForm.getSampleValue( rate );

  if ( rate % 2 == 0 ) validUserInput = false ;
  if ( rate < 3 ) validUserInput = false;
  if ( rate > 31 ) validUserInput = false;
}
while (!validUserInput);
commented: explained my problem thorougly +1

Thanks for your help, can't believe how close i was

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.