Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I finished my app but i failed in these two things

make list of weeks divided into 6 columns
When i press 0 i need it to go back to the main menu, instead i am getting the phrase, "press any key to exit. how do i fix this? what have i done wrong?
thank you

What I have tried:

 public void Start()
    {

        int choice = -1;
        while (choice != 0)
        {
            ShowMenu();
            Console.WriteLine("Your Choice :");
            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Weekends();
                    break;
                case 2:
                    Nights();
                    break;
                case 0:
                    return;
                default:
                    Console.WriteLine("Invalid Option: Choose 0, 1, or 2 Thank you ");
                    break;
            }
        }
    }

    private void Weekends() // this function is used to 
    {
        for (int x = 1; x <= 52; x += 3)
        {
            Console.WriteLine("Week" + x);
        }
    }
    private void Nights()
    {
        for (int y = 6; y <= 51; y += 5)
        {
            Console.WriteLine("Night" + y);
        }
    }

    private void ShowMenu()
    {
        Console.WriteLine("------------------------------------------------------------");
        Console.WriteLine("                    YOUR SCHEDULE PROGRAM                   ");
        Console.WriteLine("Select from the menu which type of schedule you want to see.");
        Console.WriteLine("------------------------------------------------------------");
        Console.WriteLine();
        Console.WriteLine("1 Show a list of the weekends to work .");
        Console.WriteLine("2 Show a list of the nights to work .");
        Console.WriteLine("0 Return to Main Menu               ");
        Console.WriteLine("------------------------------------------------------------");
    }
}
Posted
Updated 31-Jan-21 3:17am
Comments
Richard MacCutchan 31-Jan-21 7:04am
   
Where is the call to ShowMenu, Start etc.?

Because you told it to:
C#
            switch (choice)
            {
...
                case 0:
                    return;
...
            }
That's what return does - exit from the current method immediately. Since that code is executed in the Start method, when the user choses "0" the method immediately ends, and control returns to the previous method.
Since you don't show us where that is called from, the best guess is that it's from your Main method, and it does nothing else there but exits that method as well.
When Main exits, a console application ends, and you get "Press any key to exit".

What I'd suggest is that you start running your code under the debugger - if you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Learning to do that will save you a huge amount of time and effort in the future!

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Rash Abs 31-Jan-21 8:45am
   
thank you i will try
OriginalGriff 31-Jan-21 9:01am
   
You're welcome!
Quote:
When i press 0 i need it to go back to the main menu, instead i am getting the phrase, "press any key to exit. how do i fix this? what have i done wrong?

Not a specific solution to your problem, but an help to understand what is going on.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
Rash Abs 31-Jan-21 10:06am
   
Thank you . I appreciate the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900