c#.net

  • Thread starter Thread starter Ricky
  • Start date Start date
R

Ricky

In c#.net, how can I get input from the user, and store
that input in a int data type.

example in c++

int a = int();

cin >> a;
 
int a; //declare variable
a = (int) console.readline(); // read standard input as string and cast to
int
 
Hi, I tryed

int a = (int) Console.ReadLine();

to get input from the user, and store that input in a int data type.

from that line of code, I got the following errors

*cannot convert type 'string' to 'int'

So I try just Read() example

int a = (int) Console.Read();

that code gave me the ascII value instead of the user input.

Do you know anything else I can try in c#.net, and also can you
recommend any good c#.net books?

Thanks a lot for your help.

Ricky
 
Ricky Reed said:
int a = (int) Console.ReadLine();

to get input from the user, and store that input in a int data type.

from that line of code, I got the following errors

*cannot convert type 'string' to 'int'

So I try just Read() example

int a = (int) Console.Read();

that code gave me the ascII value instead of the user input.

Do you know anything else I can try in c#.net, and also can you
recommend any good c#.net books?

You need to *convert* a string to an int - you can't just cast it, as
you've seen. So, you want:

string line = Console.ReadLine();
int a = Convert.ToInt32(line);

Have a look at the documentation for Convert.ToInt32 for the exceptions
you should be aware of (eg if the user has typed in xxx instead of a
number).
 
Back
Top