C++ Dog Years Project- Help with Math

Hey guys I’m working on the Dog years project and wanted to fiddle with getting user inputs. what follows is my code and the link there after. Problem is I can’t get the math to work right. when I look at the answers section my formula matches but I get some really strange results. any help would be appreciated.


int main() {
// Variable’s are defined here

int dog_age = 0;
std::string dog_name = “/n”;
int early_years, int later_years, int dog_years;

//First 2 years of a dogs life
early_years = 21;
//after 2 years of age
later_years = (dog_age - 2) * 4;
//human years to dog years conversion
dog_years = early_years + later_years;

// Output of the program
std::cout << “What is your dogs name?\n”;
std::cin >> dog_name;
std::cout << “How old is your dog in human years?\n”;
std::cin >> dog_age;
std::cout << "Your dog " << dog_name << “, is " << dog_years << " dog years old\n”;


When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

You are computing the age before the input. That will give you -8.

Thank you so much. I didn’t realize the order mattered- still new and learning :stuck_out_tongue:

1 Like