Why does this program calculate age twice?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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?
				else if (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?
				else if (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?
					else if (weight > 20 && weight <= 50)
						cout << endl << "Your dog's age in human years is " << humanMd << "\n\n";

					//Weight is greater than 50.
					else if (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.
Last edited on
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.
Well, you may not have had the problem but your suggestion fixed the problem I was having! Thank you so much!
Perhaps I misunderstood your problem. Regardless, I'm glad it's fixed.
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.
Topic archived. No new replies allowed.