Fixing errors in program calculator

Hello :)

I had a previous assignment where it was a dog age calculator and got quite a few things wrong. I need some help in figuring out where the problem is with my calculations. Since my assignment I'm working on now is building on the previous assignment but just adding "while loops" I need to figure out what the previous errors were.

My professor said:
Program does not display an error when the dog's weight is less than 0

Program does not display an age of 4 x age+ 16 when the dog's age is between 2 and 5

Program does not display an age of 4 x age + 16 when the dog's age is between 6 and 16 and the dog's weight is between 21 and 50

Program does not display an age of 4.5 x age + 15 when the dog's age is between 6 and 16 and the dog's weight is between 21 and 50

Even though whenever I entered a weight less than zero it gave me the correct error message. When I turned this in I didn't have the problems he's addressing so I'm not sure where my problems are.

Any help would be appreciated :)

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
#include <iostream>
#include <string>
using namespace std;

int main()

{
	int actualage = 0, dogweight = 0;

	cout << "Welcome to My name's dog age calculator!" << endl;
	cout << endl;

	cout << "Please enter the actual age of your dog (1-16): " << endl;
	cin >> actualage;

	if (actualage<1) {
		cout << "Invalid age. Please enter a number between 1 and 16. " << endl;
		cin >> actualage;
	}
	else if (actualage>16) {
		cout << "Invalid age. Please enter a number between 1 and 16. " << endl;
		cin >> actualage;
	}
	if ((actualage >= 1) && (actualage <= 16)) {
		cout << "Please enter your dog's weight in pounds: " << endl;
		cin >> dogweight;
	}
	if ((dogweight >= 20) && (dogweight <= 50) && (actualage >= 2) && (actualage <= 16)) {
		cout << "Your dogs age is in human years is: " << 4 * actualage + 16 << endl;
	}
	else if (actualage == 1) {
		cout << "Your dog's age in human years is: " << actualage + 14 << endl;
	}
	else if ((dogweight >= 20) && (dogweight <= 50) && (actualage >= 6) && (actualage <= 16)) {
		cout << "Your dog's age in human years is:  " << 4.5 * actualage + 16 << endl;
	}
	else if ((dogweight >= 50) && (actualage >= 6) && (actualage <= 16)) {
		cout << "Your dog's age in human years is: " << 7.5 * actualage << endl;
	}

	system("PAUSE");
	return 0;

}
Hi,

Line 34 will never be executed as line 28 will satisfy all the conditions that line 34 does so the program will only execute line 28 and ignore line 34

Also
Line 13 to 22 are prone to bug
1
2
3
4
5
6
7
8
9
10
cout << "Please enter the actual age of your dog (1-16): " << endl;
	cin >> actualage;

	if (actualage<1) {
		cout << "Invalid age. Please enter a number between 1 and 16. " << endl;
		cin >> actualage;
	}
	else if (actualage>16) {
		cout << "Invalid age. Please enter a number between 1 and 16. " << endl;
		cin >> actualage;
Welcome to My name's dog age calculator!

Please enter the actual age of your dog (1-16): 
18
Invalid age. Please enter a number between 1 and 16. 
28
 
Exit code: 0 (normal program termination)


You don't want your program to crash without crashing....
Use while loops

Hope it helps
Yes that's very helpful. Thank you. I can definitely see I'm making things more complicated than they need to be. I will use this base and do more specifics my instructor wants. Thank you again. :D
Oh I see! Thank you shadder I think I know how to fix that now.
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
#include <iostream>
#include <string>
using namespace std;

int main()

{
	string dogName;
	double age, weight, humanAvg, humanMd, humanLg;
	char again;

	cout << "Welcome to My name's Dog Age Calculator!" << endl;

	cout << endl << "Please enter the name of your dog: ";
	getline(cin, dogName);

	do
	{
	cout << "Please enter the actual age of your dog (1-16): ";
	cin >> age;

	while (age < 1 || age > 16)
	{
		cout << "ERROR: Enter your dogs age between 1 and 16." << endl;
		cout << "Please enter the actual age of your dog (1-16): " << endl;
		cin >> age;
	}

humanAvg = 4 * age + 16;
humanMd = 4.5 * age + 15;
humanLg = 7.5 * age;

cout << "Please enter your dog's actual weight in pounds: ";
cin >> weight;

	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 << dogName << " 's age in human years is 15."  << endl;
	}
		
	else if (age >= 2 && age <= 5)
	{	
	if (weight > 0)
		cout << dogName << " 's age in human years is " << humanAvg <<  "\n\n";
	}
	else if (age >= 6 && age <= 16)
	{	
	if (weight <= 20)
		{
			cout << dogName << " 's age in human years is " << humanAvg << "\n\n";
		}
	else if (weight > 20 && weight <= 50)
				cout << dogName << " 's age in human years is " << humanMd << "\n\n";

	else if (weight > 50)
				cout << dogName << " 's age in human years is " << humanLg << "\n\n";
		}
	cout << "Would you like to enter another dog? (Y/N) ";
	cin >> again;
	getline(cin, dogName);

	} while (again == 'Y' || again == 'y');

system("pause");
return 0;
}


Ok so this is what I've been messing with and everything seems to be working just fine. Only thing is I'm supposed to take the input from the user on the name of the dog and display it in output. But when I get asked if I want to enter another dog it doesn't take a new name. I know I need to loop that somehow but my brain is beginning to die tonight...
Yay! Yes that worked! Thank you very much :D Sorry I'm a beginner so I just miss too many things sometimes
Thank you to everyone who helped me out!
Topic archived. No new replies allowed.