Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop a Console App

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 :)

like image 572
jay_t55 Avatar asked Nov 18 '09 07:11

jay_t55


3 Answers

Console.WriteLine("bla bla - enter xx to exit");
string line;
while((line = Console.ReadLine()) != "xx")
{
  string result = DoSomethingWithThis(line);
  Console.WriteLine(result);
}
like image 83
Traveling Tech Guy Avatar answered Oct 21 '22 20:10

Traveling Tech Guy


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?

like image 44
Tiberiu Ana Avatar answered Oct 21 '22 22:10

Tiberiu Ana


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

like image 27
Daniel Elliott Avatar answered Oct 21 '22 20:10

Daniel Elliott