FAQ: Data Types and Variables - Converting Data Types

First answear is good for 2-nd question, and Second answear is good for 3-rd question, and I am adding my variant:
// Turn that answer into an int
// int faveNumber = (int)Console.ReadLine();
int faveNumber = Convert.ToInt32(Console.ReadLine());
Console.Write(“Is “);
Console.Write(faveNumber);
Console.Write(” your favorite number?”);

has this bug not been fixed yet? because i tried every possible “Wrong” answer i could give for task 2, and eventually it just worked, despite the final answer being a repeat of what i had already tried. this was really confusing and frustrating… almost a year for this bug fix? :confused:

Not going to lie this part of the lesson was pretty confusing I never actually understood the code and im still stuck after 30mins someone please help

This exercise was not very clear. Let me know if there was a better way to approach this. Looking forward to your feedback.

This is an example of what i did below that seemed to work:

using System;

namespace FavoriteDigit
{
class Program
{
static void Main(string args)
{
Console.Write("Enter your favorite digit: ");
int faveDigit = (int)Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your favorite digit is " + faveDigit);
}
}
}

1 Like

That’s about the same as how I did it.


using System;

namespace FavoriteNumber
{
  class Program
  {
    static void Main(string[] args)
    {
      // Ask user for fave number
      Console.Write("Enter your favorite number!: ");
      // Turn that answer into an int
      int faveNumber = Convert.ToInt32(Console.ReadLine());
    }
  }
}
1 Like

Clearly, the lesson panel is more than a bit confusing. If it were, perhaps. broken out to match something like this code, it might help. As written, it’s not clear that the intent is as noted above. It would also be helpful to specify the “favorite number” must be an integer and not something like 2.7182828 or 3.1415927.

using System;

namespace FavoriteNumber
{
  class Program
  {
    static void Main(string[] args)
    {
      // Ask user for fave number
      Console.Write("Enter your favorite number!: ");
      string faveNumber = Console.ReadLine();
   
      double faveDouble = Convert.ToDouble(faveNumber);
      int faveInteger = (int)faveDouble;
      int faveInt32 = Convert.ToInt32(faveInteger);
        
      Console.WriteLine("You entered " + faveNumber);
      Console.WriteLine("As a double, that is " + faveDouble);
      Console.WriteLine("As an integer, that is " + faveInteger);
      Console.WriteLine("As an Int32, that is " + faveInt32);
 
    }
  }
}

3 Likes

I can’t get lesson 5 (casting string input) to work either. I wish this worked, seems like a good tool. May come back at some point. - T

Thank you so much for the help!!
Yes, I agree. This example was way too confusing, not even the hints were good hints.
In their desire to not give away the answer so we may learn, they made it unnecessarily difficult.
Thanks again!

I, too, am stuck with this task.

My solution is:

using System;

namespace FavoriteNumber
{
class Program
{
static void Main(string args)
{
int ageNumber;
string ageNumberString;
// Ask user for fave number
Console.Write("How old are you? ");
ageNumberString = Console.ReadLine();
ageNumber = System.Convert.ToInt16(ageNumberString);
// Turn that answer into an int
Console.Write(“You are “);
Console.Write(ageNumber);
Console.Write(” years old.”);

    }
}

}

… and it works nicely in Visual Studio.

I don’t understand which it doesn’t get accepted.

Please help.

It might be because you coverted to Int16 instead of Int32. What’s the error message. To help troubleshooting, post your code between the </> symbols. Just click the icon, then paste your code in between and check that it’s formatted correctly. There’s a guide on posting questions somewhere.

I Need to know what is the benefit of this exercise and why i need to learn this i solve the instruction (it was ez) but still i don’t understand the concept of implicit conversion and explicit conversion what are these conversion and what can it help me with? Sorry for the long type XP

Take a look at this response from @midlindner.

1 Like

It’s not that i don’t what conversions are. Thing is i cant see how can i use this in real life(IRL). For example if i’m code something a program take a game or so whatever what would be the case i need use these conversions? Thank’s for your help.

Here’s an explanation conveniently in the context of C#.

2 Likes

Thank For The Help @harrjt

I noticed this lesson used this line: Console.Write("Enter your favorite number!: ");
After running a few times I changed it to: Console.WriteLine("Enter your favorite number!: ");
Both Console.Write() and Console.WriteLine() seem to work equally well.
What is the difference?

Where does the user input show up in each case?

Seriously, the fact that the designers of the course introduced an instruction that seems unsolvable to many / most users blows my mind. At this stage, we (the users) are probably testing out the material to consider joining the paid part of the course at a later stage. If I get discouraged already in the third exercise, I am unlikely to buy the rest of the course. Please change this instruction to make it solvable.

1 Like

Like many, I’m having a problem with this lesson too. I got that it’s meant to throw up errors and show you what doesn’t work (I don’t think this is a very effective way to teach, especially because this is only the second lesson so it’s not like we’ve had a chance to learn any bad habits to correct yet, and it can in fact confuse people by teaching them how to do the wrong things - I’d personally recommend just using different instances to teach where the different kinds of conversion should be applied, rather than teach a learner how to create the wrong code in order to demonstrate where they shouldn’t).

But my bigger problem is that it tells you to “Add the Convert.ToInt32() method so that it takes the user input as a string” without ever actually telling you what the correct syntax for these things is. And the hint isn’t super clear either, it just gives you a string of code without further explanation.

1 Like

Thanks so much I just started on this website yesterday, this has been so confusing.