When I run my code it works UNLESS you enter an age outside of the limits. It will ask you to enter the correct age but then it calculates it twice. I don't run into this problem when entering the wrong weight.
int main()
{
string dog;
double age, weight, humanAvg, humanMd, humanLg;
char again;
cout << "Welcome to Jessica Mercer's Dog Age Calculator! \n";
cout << endl << "Please enter the name of your dog: ";
getline(cin, dog);
//cout << "Please enter the actual age of your dog (1-16): ";
//cin >> age;
do
{
cout << "Please enter the actual age of your dog (1-16): ";
cin >> age;
humanAvg = 4 * age + 16;
humanMd = 4.5 * age + 15;
humanLg = 7.5 * age;
//While the dog age is between 1 and 16 the program will continue.
while (age < 1 || age > 16)
{
cout << "ERROR: Enter your dogs age between 1 and 16. \n";
cout << "Please enter the actual age of your dog (1-16): ";
cin >> age;
}
cout << "Please enter your dog's actual weight in pounds: ";
cin >> weight;
//Weight must be more than 0.
//While the dog weigh entered is more than 0 the program will continue.
while (weight < 0)
{
cout << "ERROR: Your dog's weight must not be negative. \n";
cout << "Please enter your dog's actual weight in pounds: ";
cin >> weight;
}
if (age == 1)
{
cout << endl << "Your dog's age in human years is 15. \n\n";
}
//Dog is older than 1 continue to define age
//Is the dog between 2 and 5 years old?
elseif (age >= 2 && age <= 5)
{
//Display the calculations if weight entered is more than 0.
if (weight > 0)
cout << endl << "Your dog's age in human years is " << humanAvg << "\n\n";
}
//Dog is not 1 AND is not between 2 and 5.
//Is it between 6 and 16?
elseif (age >= 6 && age <= 16)
{
//Dog is between 6 and 16. What does it weigh?
//Weight is greater then zero. Is it less than 20?
if (weight <= 20)
{
cout << endl << "Your dog's age in human years is " << humanAvg << "\n\n";
}
//Weight is greater then 20. Is it less than 50?
elseif (weight > 20 && weight <= 50)
cout << endl << "Your dog's age in human years is " << humanMd << "\n\n";
//Weight is greater than 50.
elseif (weight > 50)
cout << endl << "Your dog's age in human years is " << humanLg << "\n\n";
}
cout << "Would you like to enter another dog? (Y/N) ";
cin >> again;
getline(cin, dog);
} while (again == 'Y' || again == 'y');
cout << endl << "Thank you for using the dog age calculator!\n";
system("pause");
return 0;
}
These were the instructions for my assignment.
For this assignment you will modify the dog age calculator so it does the following:
Prompts for the name of the dog and uses the dog's name when displaying the results. (You may assume the dog's name does not contain spaces)
Uses a loop to validate that the age of the dog is in the correct range.
Uses a loop to validate that the weight of the dog is not less than zero
Uses a loop to repeatedly prompt the user for input until they want to exit the program.
I didn't seem to have the problem you mentioned when I ran the code. But, you need to move lines 19, 20, and 21 below line 29 so that you have a valid age to work with when calculation humanAvg, humanMd, and humanLg.
Basically the problem I was having was this:
I enter the dogs name: Fido
Then I enter the age: 28
The program would say error: the age must be between 1 & 16 then it ask for an age between those numbers.
If I enter: 14
It will then prompt for dogs weight. I enter 90.
I should get: Your dog's age in human years is 105.
Instead I was getting: Your dogs age is 210.
Moving the calculations under the statement made it calculate properly. Which is 1 time after the user enters the correct information.