2

If I run my code and insert correct value first time program works fine and does its job, but if I input wrong path and allow for loop to spin second time it skips path=Console.ReadLine(); but it does not skip j = (char)Console.Read(); same thing persist through out the remaining code.

do
{
    Console.WriteLine("Insert path:");
    path = Console.ReadLine();

    temp1 = CheckPath(path); //checks if inserted value is legit
    if (temp1 == false) 
    { 
        Console.WriteLine("\nDo you want to skip this step(by default directory will be set to Desktop)? Y/N ");
        j = (char)Console.Read(); 
        if (j.Equals('Y') || j.Equals('y')) 
        {
            path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
            temp1 = true; 
        }
    }
    //User inputs y/Y loop will end and exit with either path chosen by user or with desktop path
} while (!temp1);

path = Console.ReadLine(); is being skipped if user fails to insert correct path. Been looking for the solution since yesterday and I have failed to find identical problem on the web. Link to full code: Code.

juharr
  • 31,741
  • 4
  • 58
  • 93
  • 1
    Have you stepped through it? It's probably just the order in which you're assigning your bool and path values. Every time they're evaluated, hover over them and check if that's the value you'd expect to see. –  Oct 17 '14 at 12:37
  • @DeeMac I've been debugging this code like crazy. It does what is stated on original post. –  Oct 17 '14 at 12:42
  • 1
    apparently Consol.Read() add a carriage return to the input, and that carriage return make Console.ReadLine() skip check http://stackoverflow.com/questions/5162670/reading-two-characters-in-c-sharp – SeraphimFoA Oct 17 '14 at 12:52
  • @SeraphimFoA Let me try that right away. –  Oct 17 '14 at 12:54

2 Answers2

5

The call isn't being skipped - the problem is that Console.Read() will only return after the user has hit return - although it will only consume the first character it reads. So suppose that (when prompted about skipping) the user enters:

Nfoo

and then hits return... the value of path in the next iteration will be foo.

The simplest fix is probably to convert your Console.Read() call into Console.ReadLine() and just handle the situation where the user types more than one character.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • j = (char)Console.Read(); is not being skipped. While path = Console.ReadLine(); is. –  Oct 17 '14 at 12:41
  • And that was due to Console.Read(); once I've converted it into Console.ReadLine(); it worked. Thanks. Please do explain why is that? –  Oct 17 '14 at 12:59
  • @Carl: I explained it in the answer... Console.Read doesn't complete until the user hits return. – Jon Skeet Oct 17 '14 at 13:00
  • Sorry for my misunderstanding but I am trying to learn as much as possible. So please do elaborate, since I actually hit return (return= enter?). –  Oct 17 '14 at 13:05
  • 1
    @Carl: You hit enter, but `Console.Read()` doesn't consume that - it only consumes a single character of input. So if you type `"foo"`, it will consume the `f`, and then the next call to `Console.ReadLine()` will consume the "oo". – Jon Skeet Oct 17 '14 at 13:56
3

It's much more useful to use Console.ReadKey for that - it will read exactly one key, and will not require you to press enter.

Luaan
  • 62,244
  • 7
  • 97
  • 116