Repeating Sequence

You how you put Console.WriteLine("Press Enter to continue");
and then you put Console.ReadLine();

Now i want it to say would you like another entry Y or N if they enter Y how do i repeat the process of going back to the excution of the program....basically how do i start it over...
I know it seems i am asking very basic question...but i started programming very differently than most developers...Big Smile

[502 byte] By [Dpowers] at [2007-12-16]
# 1
Hi,
Try this,
do {
Console.Write("Quit (Y/N)? ");
} while (Console.ReadLine().ToUpper() != "Y");

cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Well this gives the output to the screen but it makes no decision...just repeats Quit(Y,N)....i need to include an if statement
Dpowers at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
That code should work.

Check this out: http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=970

VijayeRaji at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
Hi,
The decision is in the while statement. If the user does not press a 'Y' key then the loop never ends. Just place the logic inside the while loop...
cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
I understand that but if I input

If user clicks Y? how does the program repeat itself....

Dpowers at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

Hi,
Ok, lets dissect the statement:
do {
// Your logic should be here...
Console.Write("Quit (Y/N)? ");
} while (Console.ReadLine().ToUpper() != "Y");

the statements between do and while clauses would repeat until the expression in the while statement is true.

1. It will enter the loop, execute your logic and display "Quit (Y/N)? "
2. It evaluates the while statement.
3. Since the while statement includes a call to Console.Readline() the system then waits for an input from the user.
4. After a key has been entered it is then converted to Upper Case (String.ToUpper() function)..
5. It would then compare if the letter entered is equal to 'Y'.
- if yes, then the while statement is me, and the loop terminates.
- if No, the loops executes in the beginning...


cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 7
I understand the concept of the statement...But what makes the program repeat? You said the logic goes after the do...but what logic..is there some keyword that i am missing here is the entire sample code:

using System;

class Strings

{

static void Main()

{

string a,b,c;

a = "Demetrius";

b = "Salena";

c = "Family";

Console.WriteLine("Enter Your Name");

Console.Read();

if ( a == "Demetrius")

Console.WriteLine("This is Demetrius his girl is {0}", b);

else if( a != "Demetrius")

Console.WriteLine("Enter another Choice");

if ( b == "Salena")

Console.WriteLine("Salena boyfriend is {0}", a);

if ( c == "Family")

Console.WriteLine("{0}, and {1} will start a {2}", a, b, c);

Console.WriteLine("Press Enter to continue");

Console.ReadLine();

do

{

Console.Write("Quit (Y/N)? ");

} while (Console.ReadLine().ToUpper() != "Y");

}

}

Dpowers at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
You know what i think i got it...for the do logic i belive i put the

Console.WriteLine("Enter Your Name");

Console.Read();

in the do logic so that when i enter N it goes back to these statements...what do you think

Dpowers at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 9

You are putting the do in the wrong place if you want to repeat the entire program. You need more like:

using System;

class Strings

{

static void Main()

{

do {

string a,b,c;

a = "Demetrius";

b = "Salena";

c = "Family";

//etc, etc

Console.Write("Quit (Y/N)? ");

} while (Console.ReadLine().ToUpper() != "Y");

mhorton at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 10
Hi,
Yep, you got it right... Big Smile.
Your input logic must be placed in the do statement so that it would be included in the loop...

cheers,
Paul June A. Domag

PaulDomag at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...