0

As i have added 3rd option i.e to exit from console. When i am pressing any key other than 1 and 2.it is not exiting from console.It is still asking user to select from following menu. updated/////////

    static void Main(string[] args)
    {
            bool returntype = true;

            while (true)
            {
                returntype = main();
            }

    }
    static bool main()
    {

        Console.WriteLine("please select from the following");
        Console.WriteLine("Press 1 to print even numbers below 20");
        Console.WriteLine("Press 2 to print odd numbers below 20");
        Console.WriteLine("Press any other key to exit");
        string option = (Console.ReadLine());
        if (option == "1")
        {
            evenfx();
            return true;
        }

        else if (option == "2")
        {
            oddfx();
            return true;
        }
        else 
        {
            return false;
        }   


    }

// after adding exit option it look like this.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 7
    `while(true);` never ends; remove `;` - `while(true) returntype = main();` – Dmitry Bychenko Sep 06 '19 at 09:06
  • In addition, your `bool main()` method doesn't always return a value - it only returns a value if `option` is not 1 or 2 -- if the user inputs 1 or 2, then your `bool main()` method will try and exit without returning a value, which is not allowed – canton7 Sep 06 '19 at 09:08
  • Your code edits have made your question title completely meaningless. In the future, don't edit your code so that it changes the meaning of your question. If you have an additional issue, post a new question. – Chris Dunaway Sep 06 '19 at 17:57

2 Answers2

2

Unreachable Code Error

If you want returntype = main(); to run indefinitely, then change your code to:

bool returntype = true;
while(true)
{
    returntype = main();
}

However, if you want main() to run only once, remove the while(true); line entirely.

Not All Code Paths Return a Value

Only one of the if/else/else if statements will be called, and only one of them return a Boolean (the type you specified to return). Since you return false for invalid input, I presume you want to return true on a valid response. Simply add return true; after evenfx() and oddfx().

If those functions also return a boolean, then you can have return evenfx(); instead

Luke Parker
  • 299
  • 1
  • 10
  • thank you.My 1st issue is solved but still getting the second error not all code path returns a value".I just could not understand why ?thanks again. – ruralindian Sep 06 '19 at 09:28
  • Does this line `evenfx();`, now look like `evenfx(); return true;`? @ruralindian – Luke Parker Sep 06 '19 at 10:03
  • yes.I have changed it to evenfx(); return true; – ruralindian Sep 06 '19 at 10:09
  • I have added an option to exit from the code i.e Console.WriteLine("Press any other key to exit"); string option = (Console.ReadLine()); if (option == "1") { evenfx(); return true; } else if (option == "2") { oddfx(); return true; } else { return false; } ... but when i am pressing 3 or any other key it is not exiting from the console.it displays same menu.idk why? – ruralindian Sep 06 '19 at 10:10
  • Change `while(true)`, to `while(returntype)` – Luke Parker Sep 06 '19 at 11:49
  • oh..thanks a lot it worked.could you please tell me why while(true) was not working correctly .What's the difference between while(true) and while(returntype)? – ruralindian Sep 06 '19 at 12:22
  • The expression inside the brackets (in our case true and returntype is a Boolean, the code inside the while loop executes while whatever is inside the brackets is true. Since true is always true, while(true) will execute indefinitely, however returntype is false when the user wants to exit, stopping the loop – Luke Parker Sep 06 '19 at 21:34
1

Well, you have a typical erroneous infinite loop while(true);: infinitely -while(true) do nothing - ;

static void Main(string[] args)
{
    bool returntype = true;
    while(true);            // <- Will never end (infinite loop doing nothing)
    returntype = main();    // <- Unreachable code (since the loop above will never end)
}

check the main return value within the loop:

static void Main(string[] args)
{
     // keep on looping while main() returns `true`; stop when main returns `false`
     while(main());
}

or (more wording code)

static void Main(string[] args)
{
    while (true) 
    {
        bool returntype = main();

        if (!returntype)
            break; 
    }
}

Edit: if you want to implement Press any other key to exit option, you can do it like this:

  static bool main() 
  {
      Console.WriteLine("please select from the following:");
      Console.WriteLine("  Press 1 to print even numbers below 20");
      Console.WriteLine("  Press 2 to print odd numbers below 20");
      Console.WriteLine("  Press any other key to exit"); 

      // .Trim() - let's be nice and tolerate leading / trailing spaces
      string option = Console.ReadLine().Trim(); 

      if (option == "1") 
          evenfx(); 
      else if (option == "2") 
          oddfx();
      else 
          return false; // <- any other key

      // not any other key i.e. either 1 or 2
      return true;
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • thank you very much it .it is solved now. – ruralindian Sep 06 '19 at 09:37
  • I have added this option --- Console.WriteLine("Press any other key to exit"); string option = (Console.ReadLine()); if (option == "1") { evenfx(); return true; } else if (option == "2") { oddfx(); return true; } else { return false; } --- but when pressing 3 or any other key it is not exiting from console.showing the menu again – ruralindian Sep 06 '19 at 10:25
  • @ruralindian: please, edit your quetions. How does `main` method now look like? – Dmitry Bychenko Sep 06 '19 at 10:42
  • i have updated it.Please have a look. – ruralindian Sep 06 '19 at 12:02
  • @ruralindian: you still have problem with `Main` method (capital `M`). Please, take (copy + paste?) one of my implementations , e.g. 1st one `static void Main(string[] args) { while(main()); }` – Dmitry Bychenko Sep 06 '19 at 12:16
  • thank you so much for your time.as suggested by luke parker i changed while(true) to while(returntype) and it is working.Could you please tell me how these both while are different. – ruralindian Sep 06 '19 at 12:25
  • @ruralindian: the difference is only in *readability*. For me `while(main());` is the best choice: "keep looping while `main()` returns `true`"; you seem to prefer explicit variable `returntype`, but as you can see i's more difficult to debug. – Dmitry Bychenko Sep 06 '19 at 12:42