-7

I already learned python and am trying to learn C# by recreating one of my python text based games. in the game there is a main method which is where you can look to fight something, see stats, coins, shop, turn on or off music and quit the program. in python its simply quit() but i cant figure how to just stop the program in C#. i asked people on discord but they cant understand the question and go on rants about random things so ill show the python code then my C# code so far if that helps you understand. Python:

def Main():
    print("-------------------------------------------")
    choice = input("\n1.fight \n2.Shop \n3.Stats \n4.See coins \n5.music on \n6.music off \n7.quit \nInput:")
    if choice == "1":
        battlestate()

    elif choice == "2":
        Shop()

    elif choice == "3":
        print("-------------------------------------------")
        print("health: ",character.health)
        print("strength: ",character.strength)
        print("armor: ",character.armor)

    elif choice == "4":
        print("-------------------------------------------")
        print("you have: ",character.coins," coins")
        Main()

    elif choice == "4":
        battlestate()

    elif choice == "5":
        music = True
        music = winsound.PlaySound("311 - Love from Afar.wav", winsound.SND_ASYNC | winsound.SND_ALIAS )
        Main()

    elif choice == "6":
        music = False
        music = winsound.PlaySound("311 - Love from Afar.wav", winsound.SND_ASYNC | winsound.SND_ALIAS )
        Main()
        
    elif choice == "7":
        quit()

    else:
        print("i dont understand, please try again!")
        Main()

C#

class Program
    {
        private void Main()
        {
            Console.Write("1.Fight \n7.Quit");
            string choice = Console.ReadLine();
            if (choice == "1")
            {
                BattleState();
            }
            else if(choice == "7")
            {
                
            }
        }
Lore
  • 1
  • You're wanting to exit the entire program? Have you tried `Environment.Exit(0)`? – Jaskier Aug 19 '21 at 02:54
  • 1
    [HCF](https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing)) – Ňɏssa Pøngjǣrdenlarp Aug 19 '21 at 02:56
  • 1
    Have you observed what happens when `choice` is anything other than `"1"`? – Lance U. Matthews Aug 19 '21 at 03:01
  • @Jaskier thank you this works, do you know how i can archive the question so people can keep answering but people can see answer? – Lore Aug 19 '21 at 03:03
  • @LanceU.Matthews the C# for the python one yes, thats why i have the else statement then it says it doesnt understand then calls the main function again. im going to do this for the C# one to – Lore Aug 19 '21 at 03:05
  • 2
    You'll probably get away with it because of tail-call optimization, but recursively calling `Main` instead of just using a loop is not good practice in either language. That's why we have looping constructs like `while`. – Corey Aug 19 '21 at 03:14
  • @Lore I was asking about your C# code. – Lance U. Matthews Aug 19 '21 at 03:24
  • @Lore, you can mark the posted answer as such and it will close out your question. People can still interact with the question (comment and supply more answers). User15119845 has a great solution for this posted. – Jaskier Aug 19 '21 at 03:33
  • Does this answer your question? [Command to close an application of console?](https://stackoverflow.com/questions/5682408/) and [What is the command to exit a Console application in C#?](https://stackoverflow.com/questions/10286056/) and [To exit the console window in C# console application](https://stackoverflow.com/questions/21374998/) and [Why is the console window closing immediately once displayed my output?](https://stackoverflow.com/questions/8868338/) and [How to implement a console menu having submenus in C#](https://stackoverflow.com/questions/58760184/) –  Aug 19 '21 at 05:00

1 Answers1

3

You don't have to do anything, the program will terminate and shutdown on it's own. The real question should be how do you stop the program from shutting down.

private void Main()
    { 

        bool running = true;

        Console.Write("1.Fight \n7.Quit");
        while(running) {
           string choice = Console.ReadLine();
           if (choice == "1")
           {
               BattleState();
            } 
           else if(choice == "7")
           {
               running = false;
           }
        }
        Console.WriteLine("Shutting Down...");
    }
CorrieJanse
  • 2,374
  • 1
  • 6
  • 23