wiki wiki
wiki wiki

Reputation: 27

How do i continuously check for commands in c#?

i imagine something like this:

prompt> checkprice
price of foo is 42$
prompt>

To run multiple commands.

Upvotes: 1

Views: 186

Answers (3)

BladeMight
BladeMight

Reputation: 2810

Maybe this

//this is your price variable
static int price = 42;
//function to check it
static void CheckPrice()
{
    Console.WriteLine("price of foo " + price + "$");
}
static void Main()
{
    bool exit = false;
    do
    {
        //write at begin ">"
        Console.Write(">");
        //wait for user input
        string input = Console.ReadLine();
        // if input is "checkprice"
        if (input == "checkprice") CheckPrice();
        if (input == "exit") exit = true;
    } while (!exit);
}

Upvotes: 0

Adriano Godoy
Adriano Godoy

Reputation: 1568

Do while until type a specific command like "exit":

static void Main(string[] args) {
    var line = System.Console.ReadLine().Trim();

    while(line!="exit") {
        myOperationCommand(line);
        line = System.Console.ReadLine().Trim(); // read input again
    }

    System.Console.WriteLine("End!\n");
}

// Do some operation...
static void myOperationCommand(string line) {
    switch(line) {
        case "checkprice":
            System.Console.WriteLine("price of foo is 42$");
            break;
        default: 
            System.Console.WriteLine("Command not reconized: " + line);
            break;
    }
}

Upvotes: 1

derpirscher
derpirscher

Reputation: 17407

Something like:

while(true) {
    Console.Write("prompt>");
    var command = Console.ReadLine();

    if (command == "command1") doSomething();
    else if (command == "command2") doSomethingElse();
    ...
    else if (command == "quit") break;
}

Upvotes: 1

Related Questions