0

I have this code here:

    string code1 = null;
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    if (Console.ReadLine() == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (Console.ReadLine() == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else
    {
        string temp = Console.ReadLine();
        Console.WriteLine("'" + temp + "' is not known as a command \nEnter 'info' to view all commands");
    }
    Console.ReadLine();

When I type in "Set Code" it does nothing, and then when I type in something like info, it goes to string temp = Console.ReadLine(); but then it does not run Console.WriteLine("'" + temp + "' is not known as a command \nEnter 'info' to view all commands");

Why does it not run anything else when I put in that code? I debugged it step by step, and it just like breaks there.

Jack Nelson
  • 83
  • 1
  • 6

2 Answers2

3

Because "Set Code" is expected as an input only on the 2nd input and if only the first input was not "info"...

try this:

string code1 = null;
while(true)
{
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    string line = Console.ReadLine();
    if (line == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (line == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else if (line == "quit")
    {
        break;
    }
    else
    {
        Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
    }
}

You only need to do ReadLine() once, to get a line from the user, then you make your comparison on that line, not on a new ReadLine() since every ReadLine() you make yields a new, different input from the user.

eyalsn
  • 319
  • 1
  • 7
  • Just one more ReadLine to remove and you have it right. – Steve Feb 14 '14 at 23:32
  • Thanks, missed that one at the else :) – eyalsn Feb 14 '14 at 23:34
  • @Steve What do you mean? – Jack Nelson Feb 14 '14 at 23:34
  • there was another 'ReadLine()' at the last else going into a 'temp' variable... I removed it as @Steve remarked and replaced the temp with 'line' – eyalsn Feb 14 '14 at 23:36
  • When I use that code that you posted @eyalsn, then what happens, it that if I try something like null, then it will still not run the else code. The only thing that happens, is that temp is console.readLine and nothing else – Jack Nelson Feb 14 '14 at 23:36
  • There is not need to temp = Console.ReadLine(), the invalid command had been already written in the line = Console.ReadLine() – Steve Feb 14 '14 at 23:36
  • @eyalsn I see now, let me try that. Thanks for trying to help me! – Jack Nelson Feb 14 '14 at 23:36
  • That fixed that one problem, but now I can type something in once, and then if I try to type something in again, then it just closes it. What is wrong? – Jack Nelson Feb 14 '14 at 23:38
  • Then you should run that code in a while loop, and I edited the code... 'quit' breaks the while loop and exits – eyalsn Feb 14 '14 at 23:42
  • @JackNelson Was happy to help, If this answer solved your issue, please mark it as accepted. – eyalsn Feb 13 '18 at 01:26
0

Because each time you call Console.ReadLine() you are getting a new input.If you write "Set Code" your first if statement will be passed but then in your else if statement you are calling Console.ReadLine again, and else if will check your new input after you type,not the first one.Instead get input first and store into a variable then use that variable inside of your if - elseif statements:

string input = Console.ReadLine();
if (input == "info")
Selman Genç
  • 100,147
  • 13
  • 119
  • 184