int number = console.read() reading wrong?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1780 Views - Last Post: 06 May 2011 - 02:53 PM Rate Topic: -----

#1 Chrisx18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-January 10

int number = console.read() reading wrong?

Posted 03 May 2011 - 11:14 AM

so i have

int number = console.read();



when i enter the number 20 it outputs 50??
I am really confused
Is This A Good Question/Topic? 0
  • +

Replies To: int number = console.read() reading wrong?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 11:18 AM

Please show us the entire block of code you are using. The one line is meaningless.

Not to mention it only allows you to press 1 character.
You might want to try .ReadLine() instead of .Read()


Standard resources, references and suggestions for new programmers.

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

Free editions of Visual Studio 2010

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes

Working with environmental variables

Debugging tutorial
Debugging tips
Great debugging tips

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.


These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on

This post has been edited by tlhIn`toq: 03 May 2011 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9057
  • View blog
  • Posts: 34,001
  • Joined: 12-June 08

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 11:21 AM

Have you tried two things:
1. Making it a 'readline'?
2. Converting that string to an integer?

  Int32 foo = Convert.ToInt32(Console.ReadLine());

Was This Post Helpful? 2
  • +
  • -

#4 Chrisx18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-January 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 11:28 AM

the convert to integer worked. thank you. I tried converting before but i was doing it wrong.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4453
  • View blog
  • Posts: 7,755
  • Joined: 08-June 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 11:35 AM

For the record, the reason this didn't work is because Console.Read returns the ASCII integer representation of the first character entered in the console. The reason that "20" echoed 50 to the screen is this:

Console.Read begins reading from the console's In stream.

The first character in the stream is '2'.

The ASCII value of '2' is 50.

number is assigned the value of 50.




There are many better ways to do what you want. First, as suggested, switch to ReadLine. This will read the entire stream up to the first newline character, and return it as an actual string.

Try to parse an integer from that string.

int number;
if(!int.TryParse(Console.ReadLine(), out number)){
  Console.WriteLine("Input was not an integer.");
  return;
}
Console.WriteLine(number);

This post has been edited by Curtis Rutland: 03 May 2011 - 12:12 PM

Was This Post Helpful? 4
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 11:38 AM

It returned 50 because that method reads the first character you typed, which is 2. It then returned the ASCII code for that character, which is 50.

EDIT: Beaten to it :)

This can be illustrated by casting the integer 50 to a char:

char c = (char)50;
Console.WriteLine(c);


2 is printed out to the console :)

This post has been edited by CodingSup3rnatur@l-360: 03 May 2011 - 11:55 AM

Was This Post Helpful? 3
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4453
  • View blog
  • Posts: 7,755
  • Joined: 08-June 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 11:56 AM

Great minds think alike.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 12:09 PM

Curtis Rutland, your TryParse is wrong. Just a simple mistake. It requires the out parameter. I'm assuming you just forgot to put it there.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4453
  • View blog
  • Posts: 7,755
  • Joined: 08-June 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 12:13 PM

Fixed. Yeah, it was an oversight. One of these days they'll integrate Chrome and VS so I don't have to just make it up as I go.
Was This Post Helpful? 0
  • +
  • -

#10 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 03:47 PM

int number = int.Parse(Console.Readline());
Console.Write("The number is {0} ", number );


Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: int number = console.read() reading wrong?

Posted 03 May 2011 - 04:02 PM

That breaks if you type something that isn't parse-able as an int. The TryParse is a better option.

There is also the option of writing your own method that do can get an number from the user, via the console.
(requires at least net 4.0) Read Decimal Snippet
Was This Post Helpful? 2
  • +
  • -

#12 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: int number = console.read() reading wrong?

Posted 06 May 2011 - 11:35 AM

View PostCurtis Rutland, on 03 May 2011 - 06:35 PM, said:

The reason that "20" echoed 50 to the screen is this:

Have you've been working in PHP lately??

"echoed" lol

BTW this is method confused me a bit at first glance


int number;
	if(!int.TryParse(Console.ReadLine(), out number)){
	  Console.WriteLine("Input was not an integer.");
	  return;
	}
	Console.WriteLine(number);


This is better

( Just from a different perspective , no voodoo ;) )


int number ;

if(int.TryParse(Console.ReadLine(),out number)
    Console.WriteLine(number);
else
    Console.WriteLine("Not a number");


Only use the not equal operator if you have to.

This post has been edited by marinus: 06 May 2011 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4453
  • View blog
  • Posts: 7,755
  • Joined: 08-June 10

Re: int number = console.read() reading wrong?

Posted 06 May 2011 - 11:53 AM

I disagree. If there's going to be a significant program body after the test, I'd prefer it not to be wrapped in a massive else statement. The if(!test){ return; } allows an early exit point without having to wrap the rest of your code in an else block.

Stylistic choice. Neither is objectively better than the other.
Was This Post Helpful? 1
  • +
  • -

#14 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: int number = console.read() reading wrong?

Posted 06 May 2011 - 02:17 PM

Yip it's a matter of personal preference.

This post has been edited by marinus: 06 May 2011 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4453
  • View blog
  • Posts: 7,755
  • Joined: 08-June 10

Re: int number = console.read() reading wrong?

Posted 06 May 2011 - 02:23 PM

We've actually had a nice long discussion about exactly that:

http://www.dreaminco...style-question/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2