FAQ: Data Types and Variables - Converting Data Types

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Converting Data Types” exercise from the lesson "Data Types and Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Converting Data Types

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

Please help me. I get stuck in this particular problem and got this hint
“int variableName = (int)value;”
and I cant quite understand the value. What should I put there or leave there.

Im not sure what I did, I got stuck too! I think I used this:
int faveNumber = (int)Console.ReadLine();
not sure though, sorry.

2 Likes

very helpful thank you I didn’t know what to do lmao

Ty. It actually worked wow the hint made it worst fk me. Idk how long i was stuck there 4.

OMG THANK YOU! THE solution is wrong

So the user’s expected to first type

string variableName = Console.ReadLine();

Even though the introduction told the user that this won’t work and they should write something like

int variableName = (int)Console.ReadLine();

Which is what I did, and got stuck for a while because the lesson insisted that I make the mistake first. The hint told me that I should write

string variableName = Console.ReadLine();

which I also got stuck on for a while because I was confused. After I eventually agreed to do it this way, and got to the third part, I tried to do it by again defining a separate variable as the input and then feeding that to the converter, something like this:

string variableName = Console.ReadLine();
int faveNumber = Convert.ToInt32(variableName);

but the lesson didn’t accept it because the user is now expected to put Console.ReadLine(); directly into the converter, contrary to what the first hint told.

So this lesson is being pretty unfair.

5 Likes

somebody plz give me the answer for that i cant do it

Hello, @shaizx12! Welcome to the forum. The instructions for this exercise are not very clear. Which step are you on? If you can post your code, and the step you’re on, I’ll try to help you walk through it.

It gives an error even with the suggested solution:

Console.Write("Enter your favorite number!: ");

  // Turn that answer into an int
  
  // Attempt 1: use implicit conversion
  // int faveNumber = Console.ReadLine();
  
  // Attempt 2: use explicit conversion
  // int faveNumber = (int)Console.ReadLine();
  
  // Attempt 3: use Convert method
  int faveNumber = Convert.ToInt32(Console.ReadLine());

If you input anything other than an integer there is an error which not happen?

The first 2 attempts are supposed to generate errors in the console. The exercise is walking us through a couple of methods that don’t work before using the 3rd method which does work. However, there is a bug in the lesson that generates SCT errors even when you do exactly what is expected. The SCT errors are the messages that appear at the bottom of the code editor. They also fill in X’s in the check boxes before you have even attempted steps 2 or 3. I can assure you that the Codecademy engineering team is working on this bug.
For now, just make sure you understand the content, click “Get Solution” if you have to, and move on. If you have questions regarding the content feel free to ask, and I’ll do my best to explain. Happy coding!

2 Likes

what does the (int) operator actually do? because i have never seen such operator before.

Hello, @rubywhiz86690.

Welcome to the forum.

In the lesson, the (int) operator is a cast operator. It is required to explicitly cast one type to another type. As the lesson demonstrates, not every type can be cast to every other type. See this link to the official Microsoft Docs for more information, and examples.