1

I am making a simple OS using cosmos. I am a beginner in c#. Run() method in cosmos's default code loops until I quit VMware. However, once I fix little bit, my program exits automatically. I don't understand why. I am trying to make it loop. Cosmo's default code before I fix:

 public class Kernel : Sys.Kernel
{
    protected override void BeforeRun()
    {
        Console.WriteLine("Cosmos booted successfully. Type a line of text to get it echoed back.");
    }

    protected override void Run()
    {
         Console.Write("Input: ");
         var input = Console.ReadLine();
         Console.Write("Text typed: ");
         Console.WriteLine(input);
    }
}

Below is my new Run() method. Everything else remains the same.

protected override void Run() {
    Console.WriteLine("Input:");
    String input = Console.ReadLine();
    if (input.StartsWith("echo"))
    {
        var index = input.IndexOf("echo");
        var initial = input.Substring(0, index);
        var final = input.Substring(index + "echo".Length);
        var echoInput = initial + final;
            Console.WriteLine(echoInput);
        }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
m.k
  • 73
  • 1
  • 10
  • Hi M.k, what do you mean by trying to make it loop? Do you get to the point where you can type in an input? What do you mean by **my code exits automatically** so if you type in **_echo this is test_** you should see **_this is test_** printed out on the console – jmesolomon Oct 26 '16 at 00:46
  • Run() method before I fixed it loops until I quit VMware automatically. But after I fix it, as soon as I input, VMware quits. I do not see my strings printed out on the console. – m.k Oct 26 '16 at 00:48
  • you can add `Console.ReadKey()` just after the `if` block statement. that should keep the console open until you hit a key. – jmesolomon Oct 26 '16 at 00:51
  • How is the `Run` method called? Does the ancestor class have some sort of event loop that calls it over and over? Can you post the code? – John Wu Oct 26 '16 at 00:55

1 Answers1

1

my program exits automatically....

This is because you have an unchecked exception in the new run method at runtime and the application is crashing silently...

The logic here must be debugged and corrected...

var index = input.IndexOf("echo");
var initial = input.Substring(0, index);
var final = input.Substring(index + "echo".Length);
var echoInput = initial + final;
Console.WriteLine(echoInput);

Check this out:

input

"echo world"

Then:

Var index=0

Var initial="" // empty string

Var final = "echo "// here explodes with an index out of bounds exception if you give only echo as input

var echoInput = ""+ "echo "

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • `var final =` part returns with **`""`** empty string so i'm not sure how you manage to get an index out of bounds exception even with an input of only **echo**. maybe I'm missing something here.. ^^ – jmesolomon Oct 26 '16 at 01:24