473,554 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14880
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(Conso le.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(Conso le.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.ReadLin e() );
break;
}catch{
Console.WriteLi ne( "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******** *************@e 3g2000cwe.googl egroups.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.d kwrote in message
news:WfZNg.3782 2$_q4.2339@duke read09...
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(Conso le.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.d kwrote in message
>int v = int.Parse(Conso le.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.ReadLi ne());

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.ReadLi ne());
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
20425
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 pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey...
4
8726
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. The obvious way to do this is by using the static Console type properties, In and Out. When trying to debug the app in the IDE however, this doesn't...
3
3081
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
1671
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 do I output messages while the console is listning for command inputs? I'm looking forward for your answer with great anticipation
6
4699
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; Console.ReadLine;.... all read input until the user hits <Enter>. For example, if you get the question "Do you wish to continue? (Y/N)", from the moment the user...
3
11355
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 it myself. Here's the URL: http://www.codeproject.com/dotnet/ConsolePasswordInput.asp
1
1349
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
10551
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 guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number...
12
6510
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned...
4
3772
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, when I input data and enter "enter" so fast, the Windows console is freezed, I don't know why, does anybody know?Thank you very much. My code...
0
7600
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8042
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5436
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3560
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2020
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
841
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.