Executing C# code

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
61
Location
UK
Programming Experience
3-5
Hello,

When I execute the code below by pressing the F5 key, it somehow skips printing the last Console.WriteLine("Your total cost is {0}", TotalCost).
Can someone please explain why this happens and how do I go about getting it printed when executing my code?

C#:
using System;
class Program
{
    static void Main()
    {
        bool result;
        int UserChoice;
        int TotalCost = 0;
        string UserDecision;
        do
        {
            UserDecision = string.Empty;
            do
            {
                Console.WriteLine("Please select your coffee size: 1 - Small, 2 - Medium, 3 - Large");
                result = int.TryParse(Console.ReadLine(), out UserChoice);
            } while (result == false || (UserChoice != 1 && UserChoice != 2 && UserChoice != 3));
            switch (UserChoice)
            {
                case 1:
                    TotalCost = TotalCost + 1;
                    break;
                case 2:
                    TotalCost = TotalCost + 2;
                    break;
                case 3:
                    TotalCost = TotalCost + 3;
                    break;
            }
            do
            {
                Console.WriteLine("Do you want to buy another coffee? - enter yes or no");
                UserDecision = Console.ReadLine().ToLower();
            } while (UserDecision != "yes" && UserDecision != "no");
        } while (UserDecision == "yes");

        Console.WriteLine("Your total cost is {0}", TotalCost); 
    }
}
 
Have you debugged the code? It seems not. You should do that first. Don't just go by what you see as the user. You are the developer and thus have access to a myriad of development tools, so use them. Set a breakpoint at the top of the code and step through it, line by line. Before each step, ask yourself exactly what you expect to happen and then check that it did happen after the step. You can evaluate all your variables and any other relevant expressions to determine the state of the application. If the application doesn't meet your expectations at any step, you've found an issue and you know exactly what the application state was before and after. If you can figure out how to fix it, at least you can pass all that relevant information on to us. If the application does meet all your expectations but still doesn't produce the desired result then you need to re-evaluate your expectations.
 
I stepped through the code line by line and it does execute the last console.writeline statement but I just don't see it being printed on the screen - maybe its getting executed too fast?

When I click the "Start" button I think the app runs too fast that you do not see the last statement although I believe it does get executed.
When I run the code by Ctrl + F5 (i.e. start without debugging) you see the last statement being printed.

I then decided to create a new piece of code with just one simple Console.WriteLine statement to say hello. When I run this by clicking the start button it runs too fast also and you don't see hello being printed. But works fine when you run the code by Ctrl + F5.

Is this a common problem in C#??
 
Use Console.ReadKey if you want wait for user feedback before the console is closed.
 
I stepped through the code line by line and it does execute the last console.writeline statement but I just don't see it being printed on the screen - maybe its getting executed too fast?

When I click the "Start" button I think the app runs too fast that you do not see the last statement although I believe it does get executed.
When I run the code by Ctrl + F5 (i.e. start without debugging) you see the last statement being printed.

I then decided to create a new piece of code with just one simple Console.WriteLine statement to say hello. When I run this by clicking the start button it runs too fast also and you don't see hello being printed. But works fine when you run the code by Ctrl + F5.

Is this a common problem in C#??

In all that, you didn't answer the question I asked. The answer to that question is that it exits. There's no more code to execute so it's done. If you expect the application to do something after that WriteLine then you have to tell it to do something.
 
jmcilhinnery you can stop being patronising and your comments do not help in anyway what so ever.
I request you to ignore my posts as I don't like the sound of you! I don't want to hear from you again.

Thank you @ JohnH - that works. Also, I could put Console.ReadLine() too.
 
Back
Top Bottom