6

i just need to be able to loop a console app. what i mean by that is:

program start:
display text
get input
do calculation
display result
display text
get input.

REPEAT PROCESS INFINATE NUMBER OF TIMES UNTIL THE USER EXITS THE APPLICATION.
program end.

i hope that made sense. can anyone please explain how i would go about doing this? thank you :)

jay_t55
  • 11,362
  • 28
  • 103
  • 174

6 Answers6

19
Console.WriteLine("bla bla - enter xx to exit");
string line;
while((line = Console.ReadLine()) != "xx")
{
  string result = DoSomethingWithThis(line);
  Console.WriteLine(result);
}
Larsenal
  • 49,878
  • 43
  • 152
  • 220
Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
8
while(true) {
  DisplayText();
  GetInput();
  DoCalculation();
  DisplayResult();
  DisplayText();
  GetInput();
}

The user can stop the program at any point with CTRL-C.

Is this what you meant?

Tiberiu Ana
  • 3,663
  • 1
  • 24
  • 25
6

You could wrap the whole body of your Main method in program.cs in a while loop with a condition that will always be satisfied.

E.g (in pseudo-code)

While (true)
{
   Body
}

Kindness,

Dan

Daniel Elliott
  • 22,647
  • 10
  • 64
  • 82
  • 1
    In addition, here's a nice discussion exploring more CPU friendly approaches: https://stackoverflow.com/questions/7402146/cpu-friendly-infinite-loop – Bart Verkoeijen Aug 26 '18 at 09:15
5

Use a While loop

bool userWantsToExit = false;

get input

while(!userWantsToExit)
{

  do calc;
  display results;
  display text;
  get input;
  if (input == "exit") 
    userWantsToExit = true;
}

program end;
3
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace InputLoop
{
    class Program
    {
        static long PrintFPSEveryXMilliseconds = 5000;
        static double LimitFPSTo = 10.0;
        static void Main(string[] args)
        {
            ConsoleKeyInfo Key = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, false);
            long TotalFrameCount = 0;
            long FrameCount = 0;
            double LimitFrameTime = 1000.0 / LimitFPSTo;
            do
            {
                Stopwatch FPSTimer = Stopwatch.StartNew();
                while (!Console.KeyAvailable)
                {
                    //Start of Tick
                    Stopwatch SW = Stopwatch.StartNew();

                    //The Actual Tick
                    Tick();

                    //End of Tick
                    SW.Stop();
                    ++TotalFrameCount;
                    ++FrameCount;
                    if (FPSTimer.ElapsedMilliseconds > PrintFPSEveryXMilliseconds)
                    {
                        FrameCount = PrintFPS(FrameCount, FPSTimer);
                    }
                    if (SW.Elapsed.TotalMilliseconds < LimitFrameTime)
                    {
                        Thread.Sleep(Convert.ToInt32(LimitFrameTime - SW.Elapsed.TotalMilliseconds));
                    }
                    else
                    {
                        Thread.Yield();
                    }
                }
                //Print out and reset current FPS
                FrameCount = PrintFPS(FrameCount, FPSTimer);

                //Read input
                Key = Console.ReadKey();

                //Process input
                ProcessInput(Key);
            } while (Key.Key != ConsoleKey.Escape);
        }

        private static long PrintFPS(long FrameCount, Stopwatch FPSTimer)
        {
            FPSTimer.Stop();
            Console.WriteLine("FPS: {0}", FrameCount / FPSTimer.Elapsed.TotalSeconds);
            //Reset frame count and timer
            FrameCount = 0;
            FPSTimer.Reset();
            FPSTimer.Start();
            return FrameCount;
        }

        public static void Tick()
        {
            Console.Write(".");
        }

        public static void ProcessInput(ConsoleKeyInfo Key)
        {
            Console.WriteLine("Pressed {0} Key", Key.KeyChar.ToString());
        }
    }
}
kory
  • 472
  • 4
  • 9
1

You can just put a loop around whatever you're doing in your program.

Joey
  • 344,408
  • 85
  • 689
  • 683