Jump to content

Read input into a variable

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Well as most of you know I started C#, but at school it is going really slow. So I am going to start asking some questions here. Ok so what I want is to make a console application, input some marks and read them to a variable, output them onto the screen and work the total and average. now all I need is to know how to read the user input into a variable, I have the rest done.

Thanks. I remember in Pascal I used to do something like

Readln(variablename);

now is it the same thing in C#? But change ReadLn to
Console.ReadLine(variablename);
Because I tried it but didn't work
I know this might be the noobest question, but in VB6.0 I'm not familiar with console.

Thanks!

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
You almost got it, the correct code is:

string variablename = Console.ReadLine();


#3
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Thanks, but does this mean I cannot write to byte or integers? because as soon as I tried it showed some errors that it was supposed to be a string.

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
If you want to read integers, then you shall use Console.Read()
int VariableName = Console.Read();
More information on MSDN, right here.

#5
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
When I do that it shows me the error:

Quote

.....\ConsoleApplication1\Class1.cs(16): Cannot implicitly convert type 'string' to 'int'


#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You need to convert it:

int VariableName = 
System.Convert.ToInt(Console.ReadLine());


#7
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Well at school we used another method last Monday.
We declared the variable at the start of the program and then we used the parse command like this:
variablename = int.Parse(Console.ReadLine);


#8
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
There's a lot of ways to receive input, so you shall not be surprised when you see some new ways. It's not only like that in C#, but in many other languages as well.

#9
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
I think it is interesting, but what is the use of all these methods if they all lead to the same result?

#10
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It's a coincidence that there's multiple ways to receive input. The language designers didn't sit down, and thought about how many ways they could make it possible to receive input in the language.

#11
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Lol - That sounded funny :D