8 Replies - 860 Views - Last Post: 17 April 2014 - 09:37 AM Rate Topic: -----

#1 fgrubercpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-April 14

Console App - Read Console Integer

Posted 17 April 2014 - 08:18 AM

Is there a way for a console app to 'accept' console input as an integer, as opposed to accepting a 'string' input and parsing it into an integer? for example, the User selects from a Console menu which has 'numerically' numbered options. The program then proceeds based on the numerical input.

thanks mates!

f
Is This A Good Question/Topic? 0
  • +

Replies To: Console App - Read Console Integer

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15497
  • View blog
  • Posts: 62,056
  • Joined: 12-June 08

Re: Console App - Read Console Integer

Posted 17 April 2014 - 08:30 AM

Side inquiry - why? What's the harm of having all the values come in as string and the programmer does the lifting to make determine the form and function of the text entered?
Was This Post Helpful? 1
  • +
  • -

#3 fgrubercpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-April 14

Re: Console App - Read Console Integer

Posted 17 April 2014 - 08:43 AM

Obviously, I am just a beginner, but it seems odd that there is no built in functionality to accept numerical input directly...really odd.

thanks
f
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Console App - Read Console Integer

Posted 17 April 2014 - 08:55 AM

You have to understand that the Console is an ancient and mysterious force. But seriously, it's ancient. It's been around since the DOS days, where it was the primary way you interacted with your computer. It's gone through some updates over time, but the basics are still there.

One of those basics is how you get information from it. Your Console.Whatever methods are wrappers around the Standard Streams. If you understand how Streams work, it makes sense that there's no option for directly reading anything except strings/characters from the console.

You can read from a stream or write to it, but you can't tell one to change how it's getting its source data. There's no way to "force" the console to accept only numeric input. Any program that you've seen doing that in the console is actually handling all input, keeping what's good, and redrawing the entire console to disregard what's not. And I doubt you want to go that route.

It's simple to bolt your own functionality on over it, depending on how you want it to behave. Methods (and Extension Methods) are your friend. Write a function to Console.ReadLine a value, and try to parse it as an int. If it doesn't work, that means the user entered an unparseable value, and you should try again.
Was This Post Helpful? 0
  • +
  • -

#5 fgrubercpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-April 14

Re: Console App - Read Console Integer

Posted 17 April 2014 - 09:09 AM

Obviously, I will need to study your reply, and thanks for the feedback.
it just seems bazaar to me that the user can't directly input an integer.

How old are you?
How many years have you gone to school?
Enter the number of Units you are taking.

It seems to me that if a User is going to 'input' something...anything, half the time it's going to be a number.

It's like it's a blatant oversight, so i guess i don't get it. Maybe it's like why in 1988, developers never saw the year 2000 coming.

But again,
thanks for your feedback!
f
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15497
  • View blog
  • Posts: 62,056
  • Joined: 12-June 08

Re: Console App - Read Console Integer

Posted 17 April 2014 - 09:18 AM

Quote

It seems to me that if a User is going to 'input' something

That is the key line.. input something. A number, a character, a symbol, or any other possible key and key combination. The console doesn't give a care on what you think the function should be, but all it matters is delivering you what was entered in the most universal format. It's on the developer's shoulders to put the input into context and meaning towards his/her code.


Quote

It's like it's a blatant oversight, so i guess i don't get it. Maybe it's like why in 1988, developers never saw the year 2000 coming.

You are new so I'll let the flippant and disrespectful comment slide, this time.
Was This Post Helpful? 0
  • +
  • -

#7 fgrubercpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-April 14

Re: Console App - Read Console Integer

Posted 17 April 2014 - 09:26 AM

i'm sorry, i didn't mean it that way,
f
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Console App - Read Console Integer

Posted 17 April 2014 - 09:32 AM

Modi hit the nail on the head with this line:

Quote

but all it matters is delivering you what was entered in the most universal format


Remember that the constructs you're used to using, or learning how to use, are the product of many generations of language evolution. All the modern conveniences you're used to are predicated on low level stuff like this. The DOS command prompt was made a long time ago. The STDIN/OUT streams are the simplest mechanism of IO: read to a stream, write to a stream. We still use this because it's convenient and available to all windows machines. But we also have other options now, GUI programs. That's where the improvements have been made. Console is an artifact of the past that we maintain because it's the most basic kind of program you can run, and many programs have no need of complicated GUIs or frameworks.

When you get into Streams, you'll understand the Console a bit better.
Was This Post Helpful? 0
  • +
  • -

#9 fgrubercpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-April 14

Re: Console App - Read Console Integer

Posted 17 April 2014 - 09:37 AM

thanks,
I appreciate it!
f
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1