ToString, ToInt

using System;

namespace Review
  class Program
    static void Main(string[] args)
      int dogYears = 7;
      string myAge = "";
      Console.WriteLine("Enter your age: ");
      myAge = Console.ReadLine();

      myAge = myAge * dogYears;
      Console.WriteLine("You are ", myAge, " old.");



Working on a simple multiplication function, but I keep getting build errors with Codecademy’s compiler. In this particular case, it isn’t recognizing ToString or ToInt32, even though I’m using System. And maybe I’m doing other stuff wrong as well… not sure because I can’t get past this. Any advice?

Hello, @markholley.

Your code has a couple of issues. For your conversion to an int, you can use Convert.ToInt32(string), but you’ll need to assign the converted value to another variable, or use it immediately. The same goes for using ToString(). Assign to new value to a variable, or use it immediately. You could do this:

      myAge = (Convert.ToInt32(myAge) * dogYears).ToString();
      Console.WriteLine("You are " + myAge + " old."); //use + to concatenate strings

Here, we’ve converted myAge to an integer, used that value immediately in the multiplication by dogYears and then converted that result, which is an integer, back to a string using ToString(). This string value is then assigned to myAge. A slightly more simplified approach would be to do this in steps:

      int TempIntAge = Convert.ToInt32(myAge);
      int myDogYearsAge = TempIntAge * dogYears;
      myAge = myDogYearsAge.ToString();
      Console.WriteLine("You are " + myAge + " years old in dog years.");

You should note that you don’t need to convert myAge back to a string to concatenate its value into the output string, but you obviously can do so, if you wish.

Here is yet another approach:

      int dogYears = 7;
      int myAge; //declare myAge as an int
      Console.WriteLine("Enter your age: ");
      myAge = Convert.ToInt32(Console.ReadLine()); //convert the input value prior to assigning it

      myAge *= dogYears;

      Console.WriteLine("You are " + myAge + " years old in dog years.");

All of these methods have the potential flaw of the user entering something that cannot be converted to an integer. What if the user types Hello? You may want to look into the Parse and TryParse methods.

One last thing. The Codecademy learning environment won’t allow you to enter user input. To see your code in action, you may want to try running it on a site like

Thank you midlindner, I appreciate your taking the time to explain all of this to me. Makes perfect sense. Have a good weekend.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.