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.
#include <iostream>
#include <string>
usingnamespace 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;
}
elseif (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;
}
elseif (actualage == 1) {
cout << "Your dog's age in human years is: " << actualage + 14 << endl;
}
elseif ((dogweight >= 20) && (dogweight <= 50) && (actualage >= 6) && (actualage <= 16)) {
cout << "Your dog's age in human years is: " << 4.5 * actualage + 16 << endl;
}
elseif ((dogweight >= 50) && (actualage >= 6) && (actualage <= 16)) {
cout << "Your dog's age in human years is: " << 7.5 * actualage << endl;
}
system("PAUSE");
return 0;
}
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;
}
elseif (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
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
#include <iostream>
#include <string>
usingnamespace 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;
}
elseif (age >= 2 && age <= 5)
{
if (weight > 0)
cout << dogName << " 's age in human years is " << humanAvg << "\n\n";
}
elseif (age >= 6 && age <= 16)
{
if (weight <= 20)
{
cout << dogName << " 's age in human years is " << humanAvg << "\n\n";
}
elseif (weight > 20 && weight <= 50)
cout << dogName << " 's age in human years is " << humanMd << "\n\n";
elseif (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...