Slateboard
Slateboard

Reputation: 1031

How do I categorize user input in C#

I've learned how to store a single input of data and return a value based on it.

What I want to do is obtain multiple inputs from the user, then return a result. But how do I define each input?

Like, if I want the first input to be known as 'stock', and the second as 'value', how do I do that?

I hope I explained my question properly.

Upvotes: 2

Views: 1434

Answers (3)

Warty
Warty

Reputation: 7405

string stock = Console.ReadLine();
string value = Console.ReadLine();

.. Or am I interpreting your question incorrectly?

Edit: As a response to your comment:

string input = Console.ReadLine(); //enter stock value
string[] parts = input.split(new String[]{ " " });

stock = parts[1];
value = parts[2];  

If you wish to actually define a "new" variable named "stock" and give it the value "value", you should look into System.Collections.Generic.Dictionary<key, value>

Upvotes: 3

Javier
Javier

Reputation: 4141

You can try this code:

        string name = String.Empty;
        string department = String.Empty;
        int age = 0;


        Console.WriteLine("Please, enter the name of your employee, then press ENTER");
        name = Console.ReadLine();
        Console.WriteLine("Please, enter the department of your employee, then press ENTER");
        department = Console.ReadLine();
        Console.WriteLine("Please, enter the age of your employee, then press ENTER");
        Int32.TryParse(Console.ReadLine(), out age); // default 0


        Console.WriteLine("Name: " + name);
        Console.WriteLine("Department: " + department);
        Console.WriteLine("Age: " + age.ToString());

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50235

If @ItzWarty's answer isn't quite what you want, this would allow your users to enter multiple values in one line:

string line = Console.ReadLine();

//whatever chars you wish to split on...
string[] inputs = line.Split(new char[] {' ', ',', ';'}); 

string stock = inputs[0];
string value = inputs[1];

Upvotes: 1

Related Questions