0

I am attempting to write a C# program (in visual studio) that takes some numbers as keyboard input and prints which of them is the smallest and largest. This is a homework assignment and I intend to use only the things covered in the class up to this point. So, I am quite aware that this could be done in a much simpler way with an array and the MATH.min and max methods. However, the point of this program is just to practice if/else logic. Anyway, the logic is not my issue. The code below works as intended until the final user entered number is input, then it just closes without printing the final writeline statement used to show the results. Is there something that needs to be done to fix this? Thanks!

using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            double maxNum = 0;
            double minNum = int.MaxValue;

            int numToEnter;
            int enterCounter = 0;

            double currentNum;

            Console.Write("How many numbers will be entered?: ");
            numToEnter = int.Parse(Console.ReadLine());

            while (enterCounter < numToEnter)
            {
                Console.Write("Enter a positive number: ");
                currentNum = double.Parse(Console.ReadLine());
                if (currentNum >= 0)
                {
                    if (currentNum >= maxNum)
                    {
                        maxNum = currentNum;
                    }

                    if (currentNum < minNum)
                    {
                        minNum = currentNum;
                    }

                    enterCounter++;
                }
                else
                {
                    Console.Write("Please enter a positive number: ");
                }


            }

            Console.WriteLine("The largest number is: {0}. The lowest number is: {1}", maxNum, minNum);
        }

    }
}
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
Zachary Louie
  • 49
  • 1
  • 11
  • 1
    Unable to reproduce: `How many numbers will be entered?: 3 Enter a positive number: 12 Enter a positive number: 12 Enter a positive number: 12 The largest number is: 12. The lowest number is: 12`. Please post the actual code – Camilo Terevinto Sep 08 '18 at 19:58
  • It's usually the best to provide a minimal example that reproduces your problem. If your problem is that the console window closes when process finishes simple `class Program { static void Main() { System.Console.WriteLine("Hello World!");}}` is enough to reproduce it. Putting as little code as possible will prevent people from chasing red herrings. – Andrew Savinykh Sep 08 '18 at 20:23
  • 1
    Possible duplicate of [Why is the console window closing immediately once displayed my output?](https://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-once-displayed-my-output) – Andrew Savinykh Sep 08 '18 at 20:23

2 Answers2

0

Your issue is likely related to the way that you're testing the program. I can't be sure but one way to fix it is by using this at the end of your main.

Console.ReadLine();

Are you running it w/ debugging or without debugging? I suspect you're running with debugging which would explain why it's happening. There is a difference between "Start with debugging" and "Start without" generally speaking with debugging will terminate the application immediately however a "Release Build" will not. So you can use either Console.ReadLine() method to stop it but if your instructor requires a release build then your code is fine.

F5 = Run With Debugging CTRL + F5 = Run Without Debugging (Used for Release Builds) or if you don't know how to use breakpoints.

Personally I recommend using a for loop with this instance.

for (enterCounter; enterCounter <= numToEnter; enterCounter++)
{
     //Run This code
}
  • That doesn't fix the issue of the writeline statement at the end from not showing up before the program closes. But thanks for the advice. – Zachary Louie Sep 08 '18 at 19:28
  • it's closing because it's not going to ask for the last input. You're missing the point, the issue is most certainly your logic. –  Sep 08 '18 at 19:29
  • Give me a sec, gonna throw this in VBS and see exactly what you mean. –  Sep 08 '18 at 19:30
  • Sure. Thanks for the assistance. – Zachary Louie Sep 08 '18 at 19:33
  • I think I know your problem. Let me know. –  Sep 08 '18 at 19:39
  • OK, adding that line worked. If it matters, I believe that without that line it closed in both debug and release mode. Strangely, the sample programs from class do not require that line. – Zachary Louie Sep 08 '18 at 19:54
  • 1
    @ZacharyLouie if this answer solved your problem please accept it. Now, re:debugging mode - if you put a breakpoint after the last line, that, where it would stop. Secondly, if you run you program from the console, that is if you open your console window _before_ running the program and then run it in there, the console windows won't close and you'll see your line. – Andrew Savinykh Sep 08 '18 at 20:19
  • Feelsbadman, helps with answer, #noRepLyfe Care to share that sample code? I'd be interested to look at it. Essentially what you're doing with that "Console.ReadLine();" Is you're telling the console app to "WAIT we need an input from the user!" but you're not assigning it a variable so it doesn't store. My recommendation is to always run w/o debugging unless ofc you are debugging. –  Sep 08 '18 at 20:25
0

Add

Console.ReadLine();

After

Console.WriteLine("The largest number is: {0}. The lowest number is: {1}", maxNum, minNum);
Impurity
  • 1,037
  • 2
  • 16
  • 31