Question

I'm making a C# console application for my college course and I've got an issue where I (or anybody else on the course) don't know what's wrong. In fact the tutor's not sure why it's happening. I'll show you part of the code to see if anyone can help. Probably a good idea to mention that I'm new to C# and programming in general.

  static void Main(string[] args)
    {
        string userName = GetName();
        int gradelevel = level();
        double random1 = 0;
        double random2 = 0;
        int userChoice = menu();
        int numberofquestions = 0; 
        string Message;
        int userScore = 0;

        do
        {
            if ((gradelevel == 1) && (userChoice == 1))//ADDITION LEVEL 1
            {

                generateSingleDigit(ref random1, ref random2);
                double userAnswer = additionQuestion(ref random1, ref random2);
                double Correctanswer = random1 + random2;
                Message = checkAnswer(userAnswer, Correctanswer);

                if (userAnswer == Correctanswer)
                {
                    generatePositiveResponse();
                    userScore++;
                }
                else
                {
                    int numberofAttempts = 1;

                    do
                    {
                        generateNegativeResponse();
                        userAnswer = additionQuestion(ref random1, ref random2);
                        Message = checkAnswer(userAnswer, Correctanswer);
                        numberofAttempts++;
                    } while ((numberofAttempts < 3) && (Message == "Incorrect"));
                    Console.WriteLine("The correct answer is {0}", Correctanswer);
                }
            }


                numberofquestions++;
        } while (numberofquestions <= 9);
           percentage(ref userScore); ` 

The issue I'm having is that once the user has completed the 10 questions, the results from the percentage method briefly flash up and then the application closes itself. No "Press any key to continue" that I've seen in other applications I've made.

I would really appreciate any help on this. Thanks

Was it helpful?

Solution

If you add Console.ReadLine() at the end, the window will stay open until you press the enter key. Otherwise, once it's completed, it'll close the command window.

OTHER TIPS

It sounds like you're missing a Console.ReadLine() at the end of your program. When you run a console app in Visual Studio it's normal behavior for the window to be closed once the program finishes, unless there's code (like a Console.ReadLine()) to keep the program running.

IF you ran the program directly from a DOS window, the program would still exit but the window would stay open, you'd just be back at the command prompt again.

No "Press any key to continue" that I've seen in other applications I've made.

Why would there be? That doesn't happen unless you write code to make it happen:

Console.WriteLine("Press any key to continue");
Console.ReadKey(true);

Visual Studio used to put code to do that into the default template of C++ programs, but I've never seen it for C#, and even with the old C++ programs you could see the code that caused this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top