I have this function in a Dog Class and for some reason, the age nearly always ends up being 77. I can't seem to tell what the problem is. Any ideas?

void Dog::setage()
{
while (1)
	{
	int age;
	std::cout << "Please Enter " << itsname << "'s age: ";
	if(!(std::cin >> age))
		{
		std::string badchars;
		std::cin.clear();
		std::cin >> badchars;
		std::cout << "\"" << badchars << "\"" << " is not a number.  Please try again.\n";
		}
	else if (age < 0 || age > 20)
		{
		std::cout << "\"" << age << "\"" << " is a strange age for a dog.";
		std::cout << "Please try again.\n";
		}
	else
		{
		itsage = age;
		break;
		}
	}

}

Whoops. I accidentally was setting the age again from within the function for setting the weight. Fixed now. Thanks anyway.

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.