Dog Years C++ What If I Want To Let The User Enter The Dog Age And Name?

Just started learning C++, maybe it’s gonna be a stupid question but I still wanna know the reason or solution of the problem. The project does not required the user to enter anything, but it seems more fun to me if I can write one that interact with users. However, firstly, I don’t even have the chance to enter the dog age, secondly, the dog name will always be 0 and the human age keep changing!! Can anyone give me some suggestions?

#include <iostream>

int main() {
  int dog_name , dog_age , early_years , later_years , human_years;

  std::cout << "What's the name of your dog? \n ";

  std::cin >> dog_name ;

  std::cout << "Enter the dog age of your dog!\n ";
  std::cin >> dog_age;

  early_years = 21;

  later_years = ( dog_age - 2 ) * 4;

  human_years = early_years + later_years;

  std::cout << "My name is " << dog_name << "! Ruff ruff. I am " << human_years << " years old now! \n";


This is what will be shown (the dog_name will always be 0 but the human_years keep changing):

$ g++ dog_years.cpp
$ ./a.out
What's the name of your dog? 
Enter the dog age of your dog!
 My name is 0! Ruff ruff. I am -146513715 years old now!

Thanks so much!!

Hello @java9100032678, welcome to the forums! Should the name of the dog be a type int?

Actually I don’t know… I found that it can run smoothly if I only ask the age of the dog instead of both but I still wanna know why this version is not working… :sob:

When I changed the name of the dog to String, I got a more expected result. I believe String should be used here since the name of the dog isn’t an integer (unless the name is something like 1).

I’m getting a similar issue. With this code:

int main() {

int dog_age;

int total_age;

std::cout << “How old is your dog, in human years?\n”;

std::cin >> dog_age;

if (dog_age < 2) {

int total_age = 10.5 * dog_age;


else {

int total_age = 21 + ((dog_age - 2) * 4);


std::cout << “Your dog is " << total_age << " years old in dog years.\n”;

it doesn’t give me an error message, but with an age of 11 the dog is supposedly 32764 in dog years, when it’s only 57.

You’re declaring a new integer within your if statement, int total_age = 10.5 * dog_age;. Unfortunately this will go out of scope outside of this block, the {} blocks have their own scope. I think this will be introduced at some point or you might want to have a web search for some info.

So your original dog_age variable is never actually assigned to (only the local versions). You can test this by assigning it something arbitrary e.g. int dog_age = 10 and that will be the value output at the end.

In this case I assume the local declaration wasn’t intended (don’t use int dog_age, just dog_age). You may want to be careful with math operators and mixed types e.g. (10.5 * dog_age). This often leads to unintended behaviour even if you can get away with from time to time; probably best to be explicit and type cast where appropriate.