0

After providing the first input I have to press enter twice. What am I doing wrong in my code:

public static void Ifcondition()
{            
    string answer,value1;
    Console.Clear();
    Console.WriteLine("Would you like to enter your Name");
    answer = Console.ReadLine();             
    if (answer == "Yes")
    {               
        Console.WriteLine("Great!!! - Please enter your Name:");
        value1 = Console.ReadLine();
        Console.WriteLine("Have a Great Day - {0}", value1);                
    }
    else
    {
        Console.WriteLine("Bye!!!");
    }
    Console.ReadKey();
}
Matthias
  • 3,582
  • 2
  • 30
  • 41
Ashish
  • 19
  • 3
  • 1
    Where exactly do you have to press enter twice? Before `Great!!! - Please enter your Name:` shows up? – Tobberoth Nov 14 '13 at 13:09
  • Your code is working fine for me. show us the code where you are calling Ifcondition – Microsoft DN Nov 14 '13 at 13:11
  • 7
    Cannot repro - works fine here; have you perhaps done anything like `ReadKey` *before* this method? – Marc Gravell Nov 14 '13 at 13:11
  • You forgot `Console.Write("Press any key to continue");` before the ReadKey() call. Which avoids the user being confounded about what to do next. As well as you. – Hans Passant Nov 14 '13 at 13:26
  • Hi Marc, yes I am calling this function after taking one console menut input from user through "ReadKey", so if user persses the menu option "C: IfCondition" then this mentioned function gets called. – Ashish Nov 14 '13 at 14:10

1 Answers1

0

In your code:

    public static void Ifcondition()
    {            
        string answer,value1;
        Console.Clear();
        Console.WriteLine("Would you like to enter your Name");
   1     answer = Console.ReadLine();             
        if (answer == "Yes")
        {               
            Console.WriteLine("Great!!! - Please enter your Name:");
   2        value1 = Console.ReadLine();
            Console.WriteLine("Have a Great Day - {0}", value1);                
        }
        else
        {
            Console.WriteLine("Bye!!!");
        }
   3     Console.ReadKey()};

You have 3 spots that require user action. After (1), if the answer=="Yes", logic would go to (2) and ask for input then falls to (3) and ask for input again. If the answer is not =="Yes", it falls to (3). Move (3) after "have a great day" message.

NoChance
  • 5,632
  • 4
  • 31
  • 45