[C#] Console // Read Line [2005]

Skilled Illusionist
Joined
Jun 23, 2007
Messages
310
Reaction score
1
Hey,

I've started a project in C# - it's a console application and I would like to know how it reads the last line.

eg. if they user types: 'hello' then Console.WriteLine("Hello to you too!");

Please help =]

Daney
 
Well if you have made a Console Application then your project should contain a module (Module1 by default) containing the sub 'Main'.
So look in your project for the 'Sub Main'.
Then put there:
string userInput = Console.ReadLine();

That'll store the users last input in the string userInput each time when the user hits Enter.

Now you need to figure out what the string was that the user entered.
For example, use 'switch'.

There we go, full code:

PHP:
string userInput = Console.ReadLine();
switch (userInput)
{
            case "nubcakes":
                Console.WriteLine("You is a nubcake kthx");
                break;

            case "hello":
                Console.WriteLine("Hei wats ur name");
                break;

            case "exit":
                Console.WriteLine("Bye");
                Environment.Exit;
                break;

            default:
                Console.WriteLine("Enter a valid command cunt");
                break;
        }

Oh and your console will close after you hit enter, so put this between a while loop to keep it getting executed. (while true or something)

Hope this helped.

Rolls'
Nillus
 



static void Main(string[] args)
{

Console.WriteLine("Hello, What is your Name?");
string userName = Console.ReadLine();
Console.WriteLine("Hello, " + userName);


ok as you see below, the userName part is what the entered text is going to be..
Code:
string[SIZE=2] userName = [/SIZE][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].ReadLine(); [/SIZE]

below the userName input stored is put at the end of the printed text using a "+".
Code:
[COLOR=#2b91af]Console[/COLOR][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Hello, " + userName[/COLOR][/SIZE][SIZE=2]);[/SIZE]


ok, need any more help email me at [email protected]
 
Back