0

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?

2
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by johnnyjohn20
1

>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);
Votes + Comments
explained my problem thorougly
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.