Resolved User Input / Data Type

mobsaleh

New member
Joined
Nov 16, 2021
Messages
4
Programming Experience
Beginner
Hello, newbie here.
I just started learning C# and wrote this simple code in the attachment.
When I enter user input for name, it works.
However, when I enter user input for age, say 34. It displays a completely different number in output.
Why is that? What am I missing here?
 

Attachments

  • Code.png
    Code.png
    25.2 KB · Views: 18
Please post your code in code tags. Not as a screenshot. For people using small devices, screenshots are really hard to read. For people on mobile devices, you are eating into their data quota.
 
Please don't post pictures of code. It's more effort for us to read it that way and it's especially difficult if we're using a phone. There's also no way for us to copy the code, so if we want to test it for ourselves or show you an edited version, we have to write it out again. Copy the relevant code directly from the code editor and paste it directly into your post, wrapping it in appropriate formatting tags. That way, it is easy to read and easy to copy. It helps us help you.
 
As for the issue, you should be calling ReadLine rather than Read and then converting the string you read into an int. If you take the time to read the documentation for that Read method, the issue should be obvious. ALWAYS read the relevant documentation first. Simply click the method name in the code editor and press F1 to go directly to the relevant documentation topic.
 
Anyway, Console.Read() returns one char. What you are seeing is the ASCII/Unicode point for that first character '3' which happens to be 51.
 
I was in the process of posting something similar when Skydiver posted. I read the documentation myself and it might not be quite as clear as it could be. Still, that's not a reason not to read it. Always use F1 for context-sensitive Help where you can but I would strongly recommend favouriting/bookmarking the Microsoft Docs site in your web browser. You can get there from the Help menu in VS and then clicking a few links but here's a direct link:


That's the Australian version, so you can simply edit that URL for your own culture. That will allow you to search for specific types and members whenever you want. Just be sure that you're looking at the documentation for the correct API, e.g. .NET Framework 4.8 or .NET 6.0. Later versions will often have new types, members and overloads and there are some things available in .NET Core (.NET 5 and .NET 6 are based on .NET Core) that are not available in .NET Framework.
 
Thanks very much guys. This makes perfect sense.
Apologies for not reading the documentation before I asked. I didn't know they existed lol.
I just started learning C# last week as you can see by the simple codes I'm trying to write here.
 
Back
Top Bottom