473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Int input from Console

Sorry for very silly question.
But, how can I input an integer from keyboard in C# Console
applications?
Like this, in C++ code:
"int i ; cin >i ;"
I just read some tutorials about C#, and I can get a "string" from
keyboard, but I can't get an integer.

Thank you in advance.

Sep 13 '06 #1
7 14873
Adrián E. Córdoba wrote:
But, how can I input an integer from keyboard in C# Console
applications?
Like this, in C++ code:
"int i ; cin >i ;"
I just read some tutorials about C#, and I can get a "string" from
keyboard, but I can't get an integer.
int v = int.Parse(Console.ReadLine());

was at least one way.

Arne
Sep 13 '06 #2
Arne:
Thank you very much.

Arne Vajhøj wrote:
Adrián E. Córdoba wrote:
But, how can I input an integer from keyboard in C# Console
applications?
Like this, in C++ code:
"int i ; cin >i ;"
I just read some tutorials about C#, and I can get a "string" from
keyboard, but I can't get an integer.

int v = int.Parse(Console.ReadLine());

was at least one way.

Arne
Sep 13 '06 #3
Hi,

quick , dirty code :)

int i=0;
while( true )
{
try {
i = Convert.ToInt32( Console.ReadLine() );
break;
}catch{
Console.WriteLine( "Invalid number");
}
}

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Adrián E. Córdoba" <so***********@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Sorry for very silly question.
But, how can I input an integer from keyboard in C# Console
applications?
Like this, in C++ code:
"int i ; cin >i ;"
I just read some tutorials about C#, and I can get a "string" from
keyboard, but I can't get an integer.

Thank you in advance.

Sep 13 '06 #4
Hi,

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:WfZNg.37822$_q4.2339@dukeread09...
Adrián E. Córdoba wrote:
>But, how can I input an integer from keyboard in C# Console
applications?
Like this, in C++ code:
"int i ; cin >i ;"
I just read some tutorials about C#, and I can get a "string" from
keyboard, but I can't get an integer.

int v = int.Parse(Console.ReadLine());
You may get an exception if what you read is not a number
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 13 '06 #5
Ignacio Machin ( .NET/ C# MVP ) wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
>int v = int.Parse(Console.ReadLine());

You may get an exception if what you read is not a number
Yep.

The original poster should catch the exception and
so something appropriate if that happen.

Arne
Sep 13 '06 #6
For your information:
I found in a tutorial, a compact expression for Integer input:

int i = Convert.ToInt32(Console.ReadLine());

Thank you again.
Best regards.

--
Adrián

Sep 19 '06 #7
Adrián E. Córdoba wrote:
For your information:
I found in a tutorial, a compact expression for Integer input:

int i = Convert.ToInt32(Console.ReadLine());
To quote from docs for Convert.ToInt32:

Remarks
The return value is the result of invoking the Int32.Parse method on value.

Arne
Sep 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
4
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax....
3
by: Mahmood Ahmad | last post by:
Hi, I am getting the error 'Input String was not in correct format' in the following C# program (indicated line): using System; namespace CF7 { class CalcArea {
1
by: Teis Draiby | last post by:
I am working on a Win32 program with an additional console window to output messages. I also want to type commands in the console window. Is there any common way to achieve that? For example how...
6
by: JM | last post by:
Hi, I need a function for a character based program (console) that works exactly like the INKEY$() function in (old) QBasic: Read only 1 keystroke and then move on. Console.Read;...
3
by: julianmoors | last post by:
Hey, Currently I'm writing a VB.NET/1.1 app and I need to mask the input for the password field. Does anyone know how to do this in VB? I've seen a C# example, but wouldn't know how to convert...
1
by: gopal | last post by:
Hi, I am developing a console application and i would like to get userinput for Database User & Password. The Password should be input as ********** Please can some help me out on this?
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
4
by: jane007 | last post by:
Hello everybody: I am having a problem. On Unix platform, there is a script that need user to input data from console, then when I call Unix perl script from Windows, these is an issue occurs,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.