Exit loop if while condition is met - c#

I have a program that stores user input in an array.
Every time a user input is stored, the int var Counter is increased by 1.
My problem is that if i store 25 variables, the loop ends BUT lets say i enter something that is not valid, the loop continues even after the 25 variables are stored. I want to make sure that the loop ends after the while condition is met even if the program catches the error if that makes any sense.
And no, i want to use an array and not a list!
Thanks
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
do
{
//Adds 25 passengers with a loop
for (int i = 0; i < Passenger.Age.Length; i++)
{
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
Counter++;
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Counter--;
Console.ReadKey();
break;
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
break;
case 2:
Passenger.Gender[i] = "Female";
break;
case 3:
Passenger.Gender[i] = "Other";
break;
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
Counter--;
break;
}
}
} while (Counter < 25);
This is not suppose to happen

In the scope of your question you could do something similar to the following (note this has not been run but quickly knocked up with comments)
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
do
{
//Adds 25 passengers with a loop
for (int i = 0; i < Passenger.Age.Length; i++)
{
// ensure we're not leaving the scope of the `while`
if(Counter >= 25)
break;
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
break;
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
break; // you needed to break here I assume
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
break; // you needed to break here I assume
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
break;
case 2:
Passenger.Gender[i] = "Female";
break;
case 3:
Passenger.Gender[i] = "Other";
break;
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
break;
}
Counter++; //move counter to here to only increment at the end if successful
}
} while (Counter < 25);
However, I would either change the name for Passenger or the model itself as it isn't clear why Passenger has an Array of ages or genders. Surely each passenger should be its own entity with these fields no?
Something like:
public Bus
{
public Passenger[] Passengers { get; set; }
public int TotalPassengers { get; set; }
}
public Passenger
{
public int Age { get; set; }
public string Gender { get; set; }
}
The for loop inside the while should be re-thought about. It doesn't make sense for a nested loop in this way.

There are 2 loops here, a for loop and a do-while loop. The for loop only stops if i reaches the array's limit, and the do-while loop only stops if Counter reaches 25.
You mentioned the loop continues even after 25 variables are stored. This is because the for loop restarts at 0 every time there's invalid input. Swap int i = 0 out for int i = Counter, that way, it continues where it left off.
Additionally:
You don't need 2 loops, specifically the for loop. You can remove it then replace all mentions of i with Counter, the same for break with continue. (In combination with #Tubs' answer)You also won't need if(Counter >= 25) break; in this case. Doing that effectively merges the 2 loops, instead of having 2 loops with the same function.
Console.WriteLine("\nEnter Gender for passenger {0}", Counter); doesn't add 1 to Counter, which will result in Enter Gender for passenger 0.
Extending on #Tubs' answer:
Also, Counter is only incremented after all the breaks, which means it's unreachable. You'll have to transfer to each successful case(1, 2, and 3).
The resulting code is:
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
//Adds 25 passengers with a loop
do
{
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
continue;
// Continue while loop instead of breaking it
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
continue;
// Continue while loop instead of breaking it
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter + 1);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
continue;
// Continue while loop instead of breaking it
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
case 2:
Passenger.Gender[i] = "Female";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
case 3:
Passenger.Gender[i] = "Other";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
continue;
// Continue while loop instead of breaking it
}
} while (Counter < 25);

Related

How to use loops instead of goto statement

I'm new to C# and want to implement loops , right now I'm using goto statement and labels but I have read that it is not suggested to use goto statement
so i was thinking to implement loops instead of goto and labels , byt i dont know how can i replace goto with loops
and if possible please also give a small explanation of the answer
here is my code with goto and labels
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
public static void Main()
{
int TotalCoffeeCost = 0;
// Start is a lable to point to this location so i can use it in goto
Start:
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
TotalCoffeeCost += 2;
break;
case 2:
TotalCoffeeCost += 5;
break;
case 3:
TotalCoffeeCost += 7;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a vaild choice");
goto Start;
}
// YesOrNo is a lable to point to this location so i can use it in goto
YesOrNo:
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
goto Start;
}else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
goto LastConfirmation;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a vailed choice");
goto YesOrNo;
}
// LastConfirmation is a lable to point to this location so i can use it in goto
LastConfirmation:
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if(UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
goto Amount;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
goto Start;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a vaild choice");
goto LastConfirmation;
}
// Amount is a lable to point to this location so i can use it in goto
Amount:
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if(EnterdAmt != TotalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
goto Amount;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}
}
Just use while loops and methods to make this work.
Your Start method could look like this:
public void Start(int TotalCoffeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, - large");
Console.Write(" ");
int CoffeChoice = 0;
do
{
CoffeChoice = int.Parse(Console.ReadLine());//Asking for Choice until you put in 1,2 or 3
}while(CoffeChoice != 1 | CoffeChoice != 2 | CoffeChoice != 3)
switch (CoffeeChoice)
{
case 1:
TotalCoffeeCost += 2;
break;
case 2:
TotalCoffeeCost += 5;
break;
case 3:
TotalCoffeeCost += 7;
break;
}
}
And you can call the method like this:
public static void Main()
{
int TotalCoffeeCost = 0;
Start(TotalCoffeCost);
}
We can use methods and a simple bool "lock" that either keeps you inside the while loop, or breaks out of it when conditions are met:
using System;
/* A simple coffee ordering program
* used switch,if else and lable*/
class Program
{
public static void Main()
{
var exiting = false;
while (!exiting)
{
programLoop();
}
}
private int askForCoffee()
{
int totalCoffeeCost = 0;
bool invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
Console.Write(" ");
int CoffeeChoice = int.Parse(Console.ReadLine());
switch (CoffeeChoice)
{
case 1:
totalCoffeeCost += 2;
invalidChoice = false;
break;
case 2:
totalCoffeeCost += 5;
invalidChoice = false;
break;
case 3:
totalCoffeeCost += 7;
invalidChoice = false;
break;
default:
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
return totalCoffeeCost;
}
private void programLoop()
{
int TotalCoffeeCost = 0;
TotalCoffeeCost += askForCoffee();
var invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
Console.Write(" ");
string UserChoice = Console.ReadLine();
string upperCaseChoice = UserChoice.ToUpper();
if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
{
TotalCoffeeCost += askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
}
else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
{
invalidChoice = false;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine(" ");
Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
Console.Write(" ");
string Anything = Console.ReadLine();
string UpperCaseAnything = Anything.ToUpper();
if(UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
{
invalidChoice = false;
}
else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
{
return;
}
else
{
Console.WriteLine("");
Console.WriteLine(" Please enter a valid choice");
}
}
invalidChoice = true;
while(invalidChoice)
{
Console.WriteLine("");
Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
Console.WriteLine("");
Console.WriteLine(" Please pay the amount by entering the amount bellow");
Console.Write(" ");
int EnterdAmt = int.Parse(Console.ReadLine());
if(EnterdAmt != TotalCoffeeCost)
{
Console.WriteLine("");
Console.WriteLine(" Please pay the correct amount");
}
else
{
invalidChoice = false;
}
}
Console.WriteLine("");
Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}

How to ask until the user get specific reply in C#

{
bool stayInLoop = true;
while(stayInLoop)
{
Console.WriteLine("Enter Yor Number");
var PlusA = Console.ReadLine();
Console.WriteLine("Enter Yor Number");
var PlusB = Console.ReadLine();
if(PlusA == ';')
{
stayInLoop = false;
break;
}
else if(PlusB == ';')
{
stayInLoop = false;
break;
}
else
{
Console.WriteLine("Answer =");
Console.WriteLine(PlusA + PlusB);
}
}
}
I want to make a plus calculator, I want to let the user type more then 2 numbers, keep asking for PlusC, PlusD, until they type the symbol ; .
For example the user numbers in PlusA PlusB PlusC and in PlusD, he/she type ; so it should print PlusA + PlusB + PlusC
If he type a number in PlusD, it should ask for PlusE, until he/she type ;, it should sum up all the number before
And I want to auto the process, The program will ask for PlusA to PlusZ itself instead of int it my own, how to do that? (I know I am not saying it clearly, coz i can't find better words)
You want to add numbers until the user enters ;. You should use loops for that. Here's the complete solution that uses a for loop:
switch(exp)
{
case "+":
{
var sum = 0;
for(;;)
{
Console.WriteLine("Enter Yor Number");
var line = Console.ReadLine();
if (line == ";") break;
sum += Convert.ToInt32(line);
}
Console.WriteLine(sum);
break;
}
}
Here we repeat the part inside the loop over and over, accumulating entered numbers into sum variable until the user enters ; - that's when we end the loop with break.
Use a while loop:
switch(exp)
{
case "+":
int sum = 0;
string input = "";
do
{
Console.WriteLine("Enter your number:");
input = Console.ReadLine();
if (input != ";")
sum += int.Parse(input);
} while (input != ";");
Console.WriteLine("Answer =" + sum);
break;
}
You are having problems because you should iterate the code until your exit/end condition is met using the while statement.
switch(exp)
{
case "+":
int mySum = 0;
string userInput = "";
while(userInput != ";")
{
Console.WriteLine("Enter number to add (';' to end the sum):");
userInput = Console.ReadLine();
if (userInput != ";")
{
// Would be interesting checking if entered really is an integer, for example Int32.TyParse()
mySum = mySum + Convert.ToInt32(userInput);
}
}
Console.WriteLine("Answer =" + mySum.ToString());
break;
}
Thankyou for your reply, but is there any way to auto the process, The program will ask for PlusA to PlusZ itself instead of int it my own
bool stayInLoop = true;
while(stayInLoop)
Console.WriteLine("Enter Yor Number");
var PlusA = Console.ReadLine();
Console.WriteLine("Enter Yor Number");
var PlusB = Console.ReadLine();
if(PlusA == ';')
{
stayInLoop = false;
break;
}
else if(PlusB == ';')
{
stayInLoop = false;
break;
}
else
{
Console.WriteLine("Answer =");
Console.WriteLine(PlusA + PlusB);
}
}
and when I run this, it run out 'error CS0019' and 'error CS0139'
What you're looking for is a while() loop.
example:
bool stayInLoop = true;
while(stayInLoop) // basically means (stayInLoop == true)
{
var text = Console.ReadLine();
if(text == ';')
{
stayInLoop = false;
break; // break will stop the loop, but you can also change the variable to false to break the loop.
}
}

Error detected trying to receive input as an integer in C#

I am trying to make a simple program where the user tries to guess numbers between 1 and 25 until they guess the right one. I am trying to make the input received as an integer so that I can use greater than and less than signs. When I use the command I found on another answer in this forum, it says that there is an error. What am I doing wrong?
int score = 0;
int add = 1;
while (add == 1)
{
Console.WriteLine("Guess A Number Between 1 and 25");
string input = int.Parse(Console.ReadLine());
score += add;
if (input == 18)
{
Console.WriteLine("You Did It!");
Console.WriteLine("Your Score was " + score);
break;
}
else if (input > 25)
{
Console.WriteLine("Error.");
}
else
{
Console.WriteLine("Try Again. Score: " + score);
}
}
Store their response from ReadLine() as a String, then use int.TryParse() to attempt to convert that String to an Integer. The code below is written to show you all the possible states that could occur using if else blocks. I've also used a bool to indicate when the game should end instead of using a break statement:
static void Main(string[] args)
{
int number;
string input;
bool guessed = false;
int score = 0;
while (!guessed)
{
Console.Write("Guess A Number Between 1 and 25: ");
input = Console.ReadLine();
if (int.TryParse(input, out number))
{
if(number>=1 && number<=25)
{
score++;
if (number == 18)
{
guessed = true;
Console.WriteLine("You Did It!");
Console.WriteLine("Your Score was " + score);
}
else
{
Console.WriteLine("Try Again. Score: " + score);
}
}
else
{
Console.WriteLine("Number must be between 1 and 25!");
}
}
else
{
Console.WriteLine("That's not a number!");
}
Console.WriteLine();
}
Console.Write("Press Enter to Quit.");
Console.ReadLine();
}

How to allow user to quit console app at any time?

Can someone help me make the option to quit the app at anytime work in this code? Also, I was wondering why I have to hit enter twice for the message "That is not an integer" when the user enters a string instead of a number?
using System;
using System.Threading;
namespace farino_HighLow
{
public class Game
{
// TAKES AND TRACKS USER INPUT
public void PlayGame()
{
// CREATES VARIABLE FOR INPUT FROM THE USER (IF THEY MAKE AN INVALID GUESS OR WANT TO EXIT),
// THE RANDOM VALUE GENERATED, THE USERS GUESS, AND A COUNTER FOR HOW MANY GUESSES THE USER TAKES
Random random = new Random();
int returnValue = random.Next(1, 100);
string input;
string line;
int guess = 0;
int count = 0;
Console.Beep(1000, 2000);
// INSTRUCTS USER WHAT TO DO
Console.WriteLine("Guess a number between 1-100");
Console.WriteLine("Hit Q at any time to exit the game");
do
{
input = Console.ReadLine();
try
{
guess = Convert.ToInt32(input);
}
catch
{
if (input == "Q")
{
break;
}
}
line = Console.ReadLine();
if (!int.TryParse(line, out guess))
Console.WriteLine("Not an integer!");
// MAKES SURE USER ENTERS A NUMBER WITHIN THE PARAMETERS OF THE GAME
// COUNTS THE USER GUESSES
// TELLS USER IF THEIR GUESS WAS RIGHT OR WRONG AND DIRECTS THEM TOWARDS A BETTER GUESS
if (guess >= 1 && guess <= 100)
{
if (guess > returnValue)
{
Console.Beep(100,2000);
Console.WriteLine("Guess Again! Your guess is too HIGH");
count += 1;
}
if (guess < returnValue)
{
Console.Beep(300,2000);
Console.WriteLine("Guess Again! Your guess is too LOW");
count += 1;
}
if (guess == returnValue)
{
Console.Beep(50,2000);
Console.Beep(60,2000);
Console.Beep(70,2000);
Console.WriteLine("You got it RIGHT!!!!!!");
count += 1;
Console.WriteLine("It took you" + count + "guesses to win the game.");
}
if (guess < 1)
{
Console.WriteLine("That is not a valid entry");
}
if (guess > 100)
{
Console.WriteLine("That is not a valid entry");
}
}
} while (guess != returnValue);
Thread.Sleep(5000);
Console.Clear();
}
}
}
Try This code
using System;
using System.Threading;
namespace farino_HighLow
{
public class Game
{
// TAKES AND TRACKS USER INPUT
public void PlayGame()
{
// CREATES VARIABLE FOR INPUT FROM THE USER (IF THEY MAKE AN INVALID GUESS OR WANT TO EXIT),
// THE RANDOM VALUE GENERATED, THE USERS GUESS, AND A COUNTER FOR HOW MANY GUESSES THE USER TAKES
Random random = new Random();
int returnValue = random.Next(1, 100);
string input;
string line;
int guess = 0;
int count = 0;
Console.Beep(1000, 2000);
// INSTRUCTS USER WHAT TO DO
Console.WriteLine("Guess a number between 1-100");
Console.WriteLine("Hit Q at any time to exit the game");
do
{
input = Console.ReadLine();
try
{
guess = Convert.ToInt32(input);
}
catch
{
if (input == "Q")
{
//after press Q exit from application
return;
}
}
line = Console.ReadLine();
if (!int.TryParse(line, out guess))
Console.WriteLine("Not an integer!");
// MAKES SURE USER ENTERS A NUMBER WITHIN THE PARAMETERS OF THE GAME
// COUNTS THE USER GUESSES
// TELLS USER IF THEIR GUESS WAS RIGHT OR WRONG AND DIRECTS THEM TOWARDS A BETTER GUESS
if (guess >= 1 && guess <= 100)
{
if (guess > returnValue)
{
Console.Beep(100, 2000);
Console.WriteLine("Guess Again! Your guess is too HIGH");
count += 1;
}
if (guess < returnValue)
{
Console.Beep(300, 2000);
Console.WriteLine("Guess Again! Your guess is too LOW");
count += 1;
}
if (guess == returnValue)
{
Console.Beep(50, 2000);
Console.Beep(60, 2000);
Console.Beep(70, 2000);
Console.WriteLine("You got it RIGHT!!!!!!");
count += 1;
Console.WriteLine("It took you" + count + "guesses to win the game.");
}
if (guess < 1)
{
Console.WriteLine("That is not a valid entry");
}
if (guess > 100)
{
Console.WriteLine("That is not a valid entry");
}
}
} while (guess != returnValue);
Thread.Sleep(5000);
Console.Clear();
}
}
}
Simply use either: Environment.Exit(0); or Application.Exit();
if(condition is true)
{
Environment.Exit(0);
}
else
{
//continue
}

How would you go about refreshing your application once you have pressed any key? C#

I'm currently creating a program; I cannot figure out how to refresh the application after a key is pressed.
So far I have:
Console.WriteLine("Press Any Key To Refresh");
Console.ReadKey();
Full Code Block
class Program
{
static void Main(string[] args)
{
int userInput;
DirectoryInfo folderInfo = new DirectoryInfo("C:\\Windows");
FileInfo[] files = folderInfo.GetFiles();
Console.WriteLine("Welcome To File Manager");
Console.WriteLine("");
Console.WriteLine("Current Folder: C:\\Windows");
Console.WriteLine("");
Console.WriteLine("Please Select An Opion Between 1 To 4:"); // Displays Options for Main Menu.
Console.WriteLine("1. ");
Console.WriteLine("2. ");
Console.WriteLine("3. ");
Console.WriteLine("4. ");
userInput =int.Parse(Console.ReadLine());
{
if (userInput == 1)
{
Console.WriteLine("Files in C:\\Windows:");
for (int index = 0; index < files.Length; index++) // Lists The Files Within The Speficied Folder C:\\Windows - Also Assigns Numerical Value To Each File.
{
Console.WriteLine("{0}" , index + ". " + 1 + files[index].Name + " (" +(files[index].Length) + ")");
}
Console.WriteLine("Press Any Key To Return To Main Menu");
Console.ReadKey();
}
else if (userInput == 2)
{
// code for option 2
}
else if (userInput == 3)
{
// Code for option 3
}
else if (userInput == 4)
{
// Closes Application.
}
} while (userInput != 4);
Once the operation within option (1) has ran, the message; "Press Any Key To Refresh" appears - Afterwards I'd like it to refresh the application once a key is pressed!
I hope this clarifies what I was asking!
Many Thanks
- Dan
This may help if I understood correctly what you want to accomplish.
bool isClicked = true;
while(isClicked)
{
Console.WriteLine("Please Select An Opion Between 1 To 4:");
int userInput = int.Parse(Console.ReadLine());
switch (userInput)
{
case 1:
Console.WriteLine("Press Any Key To Return To Main Menu");
Console.ReadKey();
//isClicked = false; // Used to suspend the operation
break;
case 2:
// code for option 2
break;
case 3:
// code for option 3
break;
case 4:
// code for option 4
break;
default:
Console.WriteLine("Error occured");
break;
}
}

Categories

Resources