How can I escape from this loop? - c#

I am a beginner at coding - I just started a week ago. I am trying to develop a program where the user can either see all of the names in a list or add a name to a list. When the user prompts to see the names in the list, and then decides to take another action, they are stuck in an endless loop where they can only see the names again. I want them to be able to go back to the start and choose to either see the names or add a name.
I've tried reorganizing and looking things up, but I'm still at a loss.
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> names = new List<string>();
names.Add("Vasti");
names.Add("Cameron");
names.Add("Ezra");
names.Add("Tilly");
bool program = false;
bool program2 = false;
Console.WriteLine("Welcome to the names lost! If you wish to add
a name to the list, type 1. If you want to see current names in
the list,
type 2.");
string userChoice = Console.ReadLine();
do
{
switch (userChoice)
{
case "1":
Console.WriteLine("Add a name to the squad.");
string userAddName = Console.ReadLine();
names.Add(userAddName);
break;
case "2":
Console.WriteLine("Here's the list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("Wanna do that again? Type yes or
no.");
do
{
string userContinue = Console.ReadLine();
switch (userContinue)
{
case "yes":
program = true;
program2 = false;
break;
case "Yes":
program = true;
program2 = false;
break;
case "no":
program = false;
program2 = false;
break;
case "No":
program = false;
program2 = false;
break;
default:
Console.WriteLine("Please enter either
yes or no");
userContinue = Console.ReadLine();
program2 = true;
break;
}
}
while (program2 == true);
break;
default:
Console.WriteLine("Please type either 1 or 2 to
select an option.");
break;
}
}
while (program == true);
}
}
I expect the user to return to the beginning prompt, but they are stuck in the same prompt over and over. There are no error messages.

Two major things to cover your difficulties.
First, the input hint should be in the loop so it can show when each loop begin.
Second, do while loop decide if the loop should continue at the end and in your design; it depends on what user input which should be directly utilized as while condition.
Therefore, your code could be simplified as
public static void Main()
{
List<string> names = new List<string>() { "Vasti", "Cameron", "Ezra", "Tilly" };
string userChoice = "";
do
{
Console.WriteLine($#"Welcome to the names lost!{Environment.NewLine} If you wish to add a name to the list, type 1.{Environment.NewLine} If you want to see current names in the list, type 2.");
userChoice = Console.ReadLine();
switch (userChoice)
{
case "1":
Console.WriteLine("Add a name to the squad.");
string userAddName = Console.ReadLine();
names.Add(userAddName);
break;
case "2":
Console.WriteLine("Here's the list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
break;
default:
Console.WriteLine("Please type either 1 or 2 to select an option.");
break;
}
Console.WriteLine(#"Wanna do that again? Type yes or no.");
userChoice = Console.ReadLine();
}
while (userChoice.Equals("yes", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("Program finished");
Console.ReadLine();
}

Change the do{}while() loops to regular while(){} loops.Move string userChoice = Cosole.ReadLine(); inside of the top while loop or you will never ask for other options. . . Also set the ((bool program=true; and bool program=true;)) at the beginning. Do{}while() loops do everything inside the {} first then check to see if the while statement is still true.

Related

C# if function criteria are fulfilled but not running linked code

I'm trying to finish a project, but the conditions I set in the if statement are fulfilled and not running the linked code. basically, I type null and it keeps running, even though it should set running to false, and it doesn't print the answer at the bottom. it's supposed to be a random number generator with variables linked to each number. I'm still new to C#, So I'm probably just using things wrong, as usual.
edit: added some fixes. also the line at the end, Console.WriteLine(answer) on line 99, is being considered unreachable, and it is the main way for me to know if my code works. if someone could let me know how to fix that, it would be great. also, could someone tell me some alternatives to the goto statement? edit 2: thanks for the help, It works now.
using System.Collections;
internal class Program
{
private static void Main(string[] args)
{
string item1;
string item2;
string item3;
string item4;
string item5;
string answer;
item1 = "";
item2 = "";
item3 = "";
item4 = "";
item5 = "";
answer = "";
bool Running = true;
while (Running)
{
Console.Write("Enter item 1: ");
item1 = Console.ReadLine();
Console.WriteLine("Enter item 2: ");
item2 = Console.ReadLine();
Console.WriteLine("Enter item 3, or type null: ");
item3 = Console.ReadLine();
if (item3 == "null" )
{
Running = false;
item3 = "";
}
Console.WriteLine("Enter item 4, or type null: ");
item4 = Console.ReadLine();
if (item4 == "null")
{
Running = false;
item4 = "";
}
Console.WriteLine("Enter item 5: ");
item5 = Console.ReadLine();
if (item5 == "null")
{
Running = false;
item5 = "";
}
}
reroll:
Random random = new Random();
switch (random.Next(1, 6))
{
case 1:
{
answer = item1;
break;
}
case 2:
{
answer = item2;
break ;
}
case 3:
{
if (item3 == "")
{
goto reroll;
}
answer = item3;
break;
}
case 4:
{
if (item4 == "")
{
goto reroll;
}
answer = item4;
break;
}
case 5:
{
if (item5 == "")
{
goto reroll;
}
answer = item5;
break;
}
Console.WriteLine(answer);
}
}
}
As far as your code is concerned I have several issues to bring up
When you feel the need to define variables like item1, item2, item3, etc then it is time to consider arrays and collections. You are learning bad habits otherwise. A fixed size array string[] items would suffice here, although a better solution would be to use a variable size List<string> items.
Try to divide up your code into functional parts, and use functions in order to minimize any possible side effects. This way you control the flow of information better.
If it all possible avoid goto, unless you absolutely need to. It makes code much harder to trace out and bugs are harder to catch. Alternatively use either a fixed count iteration like a for() loop, or a general while() statement and use the keyword break to exit the loop (or continue to move to the next iteration).
A lot of other smaller issues are a matter of experience with programming.
The way I read the code you are trying to do something like this
Enter Item 1
aaa
Enter Item 2
bbb
Enter Item 3
ccc
Enter Item 4 or press [ENTER] or type null to end.
ddd
Enter Item 5 or press [ENTER] or type null to end.
null
User List:
aaa
bbb
ccc
ddd
Randomly Selected Item: ddd
Here is one way of getting there using the principles laid out above. The code below is intended for learning purposes, with the hope that you are going to be able to follow along and understand what does what.
class Program
{
static readonly Random rng = new Random();
static void Main(string[] args)
{
// Get user inputs
string[] items = AskItemsFromUser(minItems:3, maxItems:5);
// Show what was entered as a list
Console.WriteLine("User List:");
Console.WriteLine(string.Join(Environment.NewLine, items));
Console.WriteLine();
// Show random item from list
string item = PickRandom(items);
Console.WriteLine($"Randomly Selected Item: {item}");
}
public static string[] AskItemsFromUser(int minItems, int maxItems)
{
List<string> items = new List<string>();
while (items.Count < maxItems)
{
// Change the prompt depending on the number of items entered.
if (items.Count >= minItems)
{
Console.WriteLine($"Enter Item {items.Count + 1} or press [ENTER] or type null to end.");
}
else
{
Console.WriteLine($"Enter Item {items.Count + 1}");
}
// get user input
string input = Console.ReadLine();
// if input=="null" or an empty string
if (input.Equals(string.Empty) || input.Equals("null"))
{
// and min # of items already reached
if (items.Count >= minItems)
{
// the exit loop
break;
}
}
else
{
// add user input to list
items.Add(input);
}
}
Console.WriteLine();
return items.ToArray();
}
public static string PickRandom(params string[] items)
{
// Use a random number generator to pick an item
// index = 0..count-1
int index = rng.Next(0, items.Length);
return items[index];
}
}

How to remove the input string from the list? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Here is my code.The first and third points of the program work properly, but the second does not.
If you press the "2" key, the data from the text should be transferred to the list, and the entered line should be deleted in the list,but for reasons I do not understand, the program crashes when entering a value into a string.
using System;
using System.IO;
using System.Collections;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String line;
List<String> list = new List<String>();
String writePath = #"C:\Work\X\hta.txt";
for (; ; )
{
Console.WriteLine("1.Add An Employee");
Console.WriteLine("2.Delete An Employee");
Console.WriteLine("3.Save Database");
Console.WriteLine("4.Exit");
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 4)
{
break;
}
switch (choice)
{
case 1:
for (; ; )
{
Console.WriteLine("1.Add A Manager");
Console.WriteLine("2.Add An Engineer");
Console.WriteLine("3.Exit");
int choice1 = Convert.ToInt32(Console.ReadLine());
if (choice1 == 3)
{
break;
}
switch (choice)
{
case 1:
line = Console.ReadLine();
list.Add(line);
break;
case 2:
line = Console.ReadLine();
list.Add(line);
break;
}
}
break;
case 2:
list.Clear();
using (StreamReader sr = new StreamReader(writePath))
{
while (!sr.EndOfStream)
{
list.Add(sr.ReadLine());
}
}
foreach(var item1 in list)
{
String linex = Console.ReadLine();
if (item1.Equals(linex))
{
list.Remove(linex);
}
}
break;
case 3:
Console.WriteLine("Do you want to continue?");
String x = Console.ReadLine();
if (x.Equals("yes")){
using (StreamWriter sw = new StreamWriter(writePath))
{
foreach (var item in list)
{
sw.WriteLine(item);
}
}
}
else
{
break;
}
break;
}
Console.ReadLine();
}
}
}
}
In this loop:
foreach(var item1 in list)
{
String linex = Console.ReadLine();
if (item1.Equals(linex))
{
list.Remove(linex);
}
}
You are reading input from the console for every item in the list. This would possibly look like a crash or not quite infinite loop as it's probably not what you're expecting. Instead you should read the input once then loop around the list. However, you should note that if you remove an item from the list while looping through it, you may also get strange behavior. Try just removing the string without the loop:
String linex = Console.ReadLine();
if (list.Contains(linex))
{
list.Remove(linex);
}
may be you writePath of value [C:\Work\X\hta.txt] file do not exists,so you should create an empty text file on you disk.

How do you execute a method only if another one has been run?

I have a choice for the user in Main using a switch. Depending on what the user chooses, several choices later, the program will either end or not. At least, that is what I'm trying to accomplish.
//This is in Main
string[] menyVal2 = new string[] {"Go to the hotel room", "Go to the dining hall"};
string title2 = "text";
int choice2 = menu(menuChoice2, title2);
switch (choice2)
{
case 0:
hotelRoom();
break;
case 1:
diningHall();
break;
}
Many lines of code later...
public static void save()
{
Console.Clear();
string[] menuChoice = {"Chapter 2", "Back to start"};
string title = "text";
int choice = menu(menuChoice, title);
switch (val)
{
case 0:
if (hotelRoom = true) //this if-statement does not work
{
withoutCross();
}
else if (diningHall = true)
{
withCross();
}
break;
case 1:
Main(new string[] { });
break;
}
}
When I understand your title of the question correctly, then this is a solution:
Make the return type of the method bool and fill it to a variable.
bool isMethodExecuted = MyMethod();
Later you can check with a if-Statement, if the method is executed:
if(isMethodExecuted)
MyOtherMethod();

Error in Switch case and not executing the program

The error is "Control cannot fall through from one case label ('case "B":')"
I am trying to do the following:
Upon starting the application a user will be prompted to login. The first action is to authenticate the user by validating an account exists for the entered username. Once the username has been matched to an account let the account object validate the entered pin (see Account class).
o After authentication, display a welcome message to the customer logged in using their name and display a menu offering options to get balance, deposit to account, withdraw from account, modify customer information, display current transactions, and exit.
static void Main(string[] args)
{
Account myCustAcc = new Account();
Transaction myCustTrans = new Transaction();
string input, choice = "";
string adminName, userName= "";
int adminPin, userPin;
//Login
Console.WriteLine("\t\t*****************WELCOME TO BANKING APPLICATION*********************\n");
choice = Console.Readline();
switch (choice)
{
case "A":
choice = "Admin";
Console.Write("\nAdminName :");
adminName = Console.ReadLine();
Console.Write("AdminName :");
Console.Write("AdminPIN :");
adminPin = Convert.ToInt32(Console.ReadLine());
//IT IS DEFINE USERNAME AND PASSWORD
if (adminName.Equals("admn1") && adminPin.Equals("9999"))
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Welcome to Bank");
}
break;
case "B":
{
choice = "User";
Console.Write("\nUserName :");
userName = Console.ReadLine();
Console.Write("UserName :");
Console.Write("PIN :");
userPin = Convert.ToInt32(Console.ReadLine());
//IT IS DEFINE USERNAME AND PASSWORD
if (userName.Equals("SMD") && userPin.Equals("1212"))
{
Console.ForegroundColor = ConsoleColor.White;
//Welcome Message with Name
Console.WriteLine("\t\t\t Welcome to Banking Application",userName);
Console.WriteLine("\n<<<Please Select Following Menus>>>");
do
{
//With Menu option to get balance, deposit/withdraw from account, modify customer information display current balance and exit
Console.WriteLine("\t1> GetBalance");
Console.WriteLine("\t2> Deposit");
Console.WriteLine("\t3> Withdraw");
Console.WriteLine("\t4> Modify");
Console.WriteLine("\t5> Display");
Console.WriteLine("\t6> Exit");
input = Console.ReadLine();
switch (input)
{
case "1":
break;
case "2":
break;
case "3":
break;
case "4":
break;
case "5":
break;
case "6":
default: Console.WriteLine("Exit the Application!!!");
break;
}
} while (input != "6");
}
break;
}
}
//Pause Display
Console.WriteLine("Press Any key to continue...........");
Console.ReadLine();
}
}
}
this is my account class
class Account
{
//Declare Instance Variables
private string customerFirstName;
private string customerLastName;
private string customerAddress;
private string customerState;
private int customerZip;
private double customerBalance;
//Class Variables
private static string customerUserName;
private static int customerPin;
//Retrieve Customer First Name
public string getCustomerFirstName()
{
return customerFirstName;
}
//Set Customer Name
public void setCustomerFirstName(String newFirstName)
{
customerFirstName = newFirstName;
}
//Retrieve Customer Last Name
public string getCustomerLastName()
{
return customerLastName;
}
//Set Customer Last Name
public void setCustomerLastName(String newLastName)
{
customerLastName = newLastName;
}
//Retrieve Customer Address
public string getCustomerAddress()
{
return customerAddress;
}
//Set Customer Address
public void setCustomerAddress(string newAddress)
{
customerAddress = newAddress;
}
//Retrieve Customer State
public string getCustomerState()
{
return customerState;
}
//Set Customer State
public void setCustomerZip(string newState)
{
customerState = newState;
}
//Retrieve Customer Zip
public int getCustomerZip()
{
return customerZip;
}
//Set Customer Zip
public void setCustomerZip(int newZip)
{
customerZip = newZip;
}
}
You need to move the break outside of the if statement. Also I recommend you to use brackes for case statements as they will make the intent clear(where statement block starts and ends).
Your code have too many execution path inside the single method, which makes code hard to read and hard to maintain. Refactor your code to methods which does only one thing, so that it will be easy to maintain.
Your main switch statement is using the choice variable which you are setting to an empty string and then never assigning. It looks like you meant to include a Console.Read after the initial message but forgot it.
The window flickers because 'choice' variable is not initialized to "A" or "B":
string input, choice = ""; //<----- here
Therefore there is no match for the switch block and the program quits.
Unreachable code warning is issued because you put some lines after the break command.
switch (choice)
{
case "A":
...
break;
case "B":
{
...
break; //<--- quits case "B", but the code below belongs to case "B"
}
//Pause Display
Console.WriteLine("Press the Enter key to Exit"); // <--- here is still considered part of case "B"
Console.ReadLine();
// case "B" ends here
} // end of outer switch block.
You may want to consider something like this:
...
Console.WriteLine("\t\t*****************WELCOME TO BANKING APPLICATION*********************\n");
Console.WriteLine("Choose A or B");
choice = Console.ReadLine();
switch (choice)
{
case "A":
...
break;
case "B":
...
break;
} // end of outer switch block.
//Pause Display
Console.WriteLine("Press the Enter key to Exit");
Console.ReadLine();

'Advanced' Console Application

I'm not sure if this question has been answered elsewhere and I can't seem to find anything through google that isn't a "Hello World" example... I'm coding in C# .NET 4.0.
I'm trying to develop a console application that will open, display text, and then wait for the user to input commands, where the commands will run particular business logic.
For example: If the user opens the application and types "help", I want to display a number of statements etc etc. I'm not sure how to code the 'event handler' for user input though.
Hopefully this makes sense. Any help would be much appreciated!
Cheers.
You need several steps to achieve this but it shouldn't be that hard. First you need some kind of parser that parses what you write. To read each command just use var command = Console.ReadLine(), and then parse that line. And execute the command... Main logic should have a base looking this (sort of):
public static void Main(string[] args)
{
var exit = false;
while(exit == false)
{
Console.WriteLine();
Console.WriteLine("Enter command (help to display help): ");
var command = Parser.Parse(Console.ReadLine());
exit = command.Execute();
}
}
Sort of, you could probably change that to be more complicated.
The code for the Parser and command is sort of straight forward:
public interface ICommand
{
bool Execute();
}
public class ExitCommand : ICommand
{
public bool Execute()
{
return true;
}
}
public static Class Parser
{
public static ICommand Parse(string commandString) {
// Parse your string and create Command object
var commandParts = commandString.Split(' ').ToList();
var commandName = commandParts[0];
var args = commandParts.Skip(1).ToList(); // the arguments is after the command
switch(commandName)
{
// Create command based on CommandName (and maybe arguments)
case "exit": return new ExitCommand();
.
.
.
.
}
}
}
I know this is an old question, but I was searching for an answer too. I was unable to find a simple one though, so I built InteractivePrompt. It's available as a NuGet Package and you can easily extend the code which is on GitHub. It features a history for the current session also.
The functionality in the question could be implemented this way with InteractivePrompt:
static string Help(string strCmd)
{
// ... logic
return "Help text";
}
static string OtherMethod(string strCmd)
{
// ... more logic
return "Other method";
}
static void Main(string[] args)
{
var prompt = "> ";
var startupMsg = "BizLogic Interpreter";
InteractivePrompt.Run(
((strCmd, listCmd) =>
{
string result;
switch (strCmd.ToLower())
{
case "help":
result = Help(strCmd);
break;
case "othermethod":
result = OtherMethod(strCmd);
break;
default:
result = "I'm sorry, I don't recognize that command.";
break;
}
return result + Environment.NewLine;
}), prompt, startupMsg);
}
This is easy enough, just use the Console.WriteLine and Console.ReadLine() methods. From the ReadLine you get a string. You could have a horrible if statement that validate this against known/expected inputs. Better would be to have a lookup table. Most sophisticated would be to write a parser. It really depends on how complex the inputs can be.
Console.WriteLine Console.ReadLine and Console.ReadKey are your friends. ReadLine and ReadKey waits for user input. The string[] args will have all of your parameters such as 'help' in them. The array is created by separating the command line arguments by spaces.
switch (Console.ReadLine())
{
case "Help":
// print help
break;
case "Other Command":
// do other command
break;
// etc.
default:
Console.WriteLine("Bad Command");
break;
}
If you're looking to parse commands that have other things in them like parameters, for example "manipulate file.txt" then this alone won't work. But you could for example use String.Split to separate the input into a command and arguments.
A sample:
static void Main(string[] args)
{
Console.WriteLine("Welcome to test console app, type help to get some help!");
while (true)
{
string input = Console.ReadLine();
int commandEndIndex = input.IndexOf(' ');
string command = string.Empty;
string commandParameters = string.Empty;
if (commandEndIndex > -1)
{
command = input.Substring(0, commandEndIndex);
commandParameters = input.Substring(commandEndIndex + 1, input.Length - commandEndIndex - 1);
}
else
{
command = input;
}
command = command.ToUpper();
switch (command)
{
case "EXIT":
{
return;
}
case "HELP":
{
Console.WriteLine("- enter EXIT to exit this application");
Console.WriteLine("- enter CLS to clear the screen");
Console.WriteLine("- enter FORECOLOR value to change text fore color (sample: FORECOLOR Red) ");
Console.WriteLine("- enter BACKCOLOR value to change text back color (sample: FORECOLOR Green) ");
break;
}
case "CLS":
{
Console.Clear();
break;
}
case "FORECOLOR":
{
try
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
case "BACKCOLOR":
{
try
{
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
default:
{
Console.WriteLine("!!! Bad command");
break;
}
}
}
}
This is very simplistic, but might meet your needs.
// somewhere to store the input
string userInput="";
// loop until the exit command comes in.
while (userInput != "exit")
{
// display a prompt
Console.Write("> ");
// get the input
userInput = Console.ReadLine().ToLower();
// Branch based on the input
switch (userInput)
{
case "exit":
break;
case "help":
{
DisplayHelp();
break;
}
case "option1":
{
DoOption1();
break;
}
// Give the user every opportunity to invoke your help system :)
default:
{
Console.WriteLine ("\"{0}\" is not a recognized command. Type \"help\" for options.", userInput);
break;
}
}
}
There is a C# nuget package called 'ReadLine' by 'tornerdo'. The statement ReadLine.Read(" prompt > "); prompts the user within options provided in CustomAutoCompletionHandler.PossibleAutoCompleteValues.
Additionally, you can change the CustomAutoCompletionHandler.PossibleAutoCompleteValues for each prompt. This ensures that the user get to choose an option from available\ supported list of options. Less error prone.
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(" Note! When it prompts, press <tab> to get the choices. Additionally, you can use type ahead search.");
Console.ForegroundColor = ConsoleColor.White;
// Register auto completion handler..
ReadLine.AutoCompletionHandler = new CustomAutoCompletionHandler();
CustomAutoCompletionHandler.PossibleAutoCompleteValues = new List<string> { "dev", "qa", "stg", "prd" };
var env = CoverReadLine(ReadLine.Read(" Environment > "));
Console.WriteLine($"Environment: {env}");
}
private static string CoverReadLine(string lineRead) => CustomAutoCompletionHandler.PossibleAutoCompleteValues.Any(x => x == lineRead) ? lineRead : throw new Exception($"InvalidChoice. Reason: No such option, '{lineRead}'");
public class CustomAutoCompletionHandler : IAutoCompleteHandler
{
public static List<string> PossibleAutoCompleteValues = new List<string> { };
// characters to start completion from
public char[] Separators { get; set; } = new char[] { ' ', '.', '/' };
// text - The current text entered in the console
// index - The index of the terminal cursor within {text}
public string[] GetSuggestions(string userText, int index)
{
var possibleValues = PossibleAutoCompleteValues.Where(x => x.StartsWith(userText, StringComparison.InvariantCultureIgnoreCase)).ToList();
if (!possibleValues.Any()) possibleValues.Add("InvalidChoice");
return possibleValues.ToArray();
}
}

Categories