tags:

views:

119

answers:

7

I made a program that asks for input and returns a value. After, I want to ask if the user wants to continue. But I don't know what to use.

+3  A: 

You want to use a do / while loop, or an infinite while loop with a conditional break.

SLaks
A: 

use Do While Loop. something similar will work

int input=0;
do
{  
  System.Console.WriteLine(Calculate(input)); 
  input =  GetUserInput();
} while (input != null)
Asad Butt
A: 

You probably want a while loop here, something like:

bool doMore= true;

while(doMore) {
  //Do work
  //Prompt user, if they refuse, doMore=false;
}
Nick Craver
+2  A: 

A do-while loop is often used:

bool @continue = false;
do
{
    //get value
    @continue = //ask user if they want to continue
}while(@continue);

The loop will be executed once before the loop condition is evaluated.

Lee
+1  A: 

I would use a do..while loop:

bool shouldContinue;
do {
    // get input
    // do operation
    // ask user to continue
    if ( Console.ReadLine() == "y" ) {
        shouldContinue = true;
    }
} while (shouldContinue);
Lucas Jones
This one was the easiest to understand. Thanks.
Slateboard
+2  A: 

This only allows 2 keys (Y and N):

ConsoleKeyInfo keyInfo;
do {
    // do you work here
    Console.WriteLine("Press Y to continue, N to abort");

    do {
        keyInfo = Console.ReadKey();
    } while (keyInfo.Key != ConsoleKey.N || keyInfo.Key != ConsoleKey.Y);
} while (keyInfo.Key != ConsoleKey.N);
Obalix
A: 

Technically speaking, any loop will do it, a for loop for example (which is another way to write a while(true){;} )

    for (; true; )
    {
        //Do stuff
        if (Console.ReadLine() == "quit")
            {
            break;
        }
        Console.WriteLine("I am doing stuff");
    }
martani_net