noob c++ question

maclem8223

[H]ard|Gawd
Joined
Oct 28, 2013
Messages
1,849
I've been at this for hours and I'm sure it's something simple.
I'm just trying to figure out why the do-while loop for gender runs fine but the do-while loop for height continues infinitely. Any help would be appreciated.

Code:
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
int  height;
char gender;

do
{
	cout    << "Enter gender";
	cin.get(gender);
	cin.ignore(1000, '\n');
	if(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f')
	{
		cout << "Please enter a valid gender\n";
	}
}while(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f');

do
{
	cout << "Enter height: ";
	cin  >> height;
	cin.ignore(1000, '\n');

	if(height < 12 || height > 20)
	{
		cout << "Please enter a correct height\n";
	}
}while(height < 12 || height > 20);

return 0;
}
[CODE]
 
Last edited:
Remember to use
Code:
 tags around your code when posting code.

What values do you give for height as input? How does the program react? When you debug, what values do you see in the actual "height" variable?
 
Sorry about that....
I'm simply trying to error check some random letter, symbols, or a combination as the value for height.
When I run the program it starts an infinite loop:
Please enter a correct height
Please enter a correct height
Please enter a correct height.....etc.

Running the same type of error check on gender yields the expected result.
 
Last edited:
Try putting cin.clear() after your cin.ignore statements.

Hasn't been part of the curriculum yet, so I should be able to do this without cin.clear().
Regardless, thank you for the suggestion.

FYI: I'm using Eclipse.
 
Last edited:
Do you mean if you give it bad input like a letter?

At that point the error flag is set and you need clear() to reset it, or getting new input will fail.
 
Hasn't been part of the curriculum yet, so I should be able to do this without cin.clear().
Regardless, thank you for the suggestion.

FYI: I'm using Eclipse.

cin.ignore() only ignores input if it can, if there's an error condition then all calls to cin will fail and your variable never gets set.

You should check to see if your "height" variable is even being modified after the "cin" call. You can do this with a breakpoint and state viewer or by explicitly outputting the contents of the variable to the screen via "cout" immediately after trying to set it..

Additionally, you should initialize your variables to a known state that would meet the needs you have for your conditional tests so that you eliminate the chance that indeterminate contents that variables may contain after being declared end up being used later as valid data.
 
[CODE] blocks begin with [CODE] and end with [/CODE]. They'll preserve the indenting in your code and make it a lot easier to read. When your code is easier to read, it is easier for people to help you.

As ryan points out, entering something that's not an integer means the "cin >> hieght" line fails. cin.ignore() subsequently has nothing to ignore, height remains unchanged, and you keep looping because nothing changes the conditional that controls the loop.

The reason that this doesn't happen for the gender loop is that the gender loop reads a character. Any character is valid in that loop, so it just reads the next character. The cin object never enters an error mode, so it always gets something back for your gender variable (or waits for more if it has eaten all the input). The height loop wants to read only valid integers. If it gets something that isn't a valid integer, it sets its error flag and won't read anything; in that mode, "cin >> height" doesn't read anything from the input and doesn't change the value of height. That's why checking for the error and clearing the input stream is necessary.

Resetting the input stream like Sock suggests is the right way to fix this problem -- whether your class has covered that technique or not.

Code:
int main()
{
	int height;
	char gender;

	do
	{
		cout << "Enter gender";
		cin.get(gender);
		cin.ignore(1000, '\n');
		if(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f')
		{
			cout << "Please enter a valid gender\n";
		}
	} while(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f');

	do
	{
		cout << "Enter height: ";
		cin >> height;
		if ( cin.fail() )
		{
			cin.clear();
		}
			
		cin.ignore(1000, '\n');


		if(height < 12 || height > 20)
		{
			cout << "Please enter a correct height\n";
		}
	}while(height < 12 || height > 20);

	return 0;
}
 
Thank you for everyone's help on this, I've come to the same conclusion and it is simply something we haven't covered yet. So I'm to assume the user is nice and inputs a number:) At least that's what the professor said. Thanks again, now I know when the problem arises again!
 
Back
Top