C#, Skipping Console.ReadLine(); Console.Read(); while in loop - c#

If I run my code and insert correct value first time program works fine and does its job, but if I input wrong path and allow for loop to spin second time it skips path=Console.ReadLine(); but it does not skip j = (char)Console.Read(); same thing persist through out the remaining code.
do
{
Console.WriteLine("Insert path:");
path = Console.ReadLine();
temp1 = CheckPath(path); //checks if inserted value is legit
if (temp1 == false)
{
Console.WriteLine("\nDo you want to skip this step(by default directory will be set to Desktop)? Y/N ");
j = (char)Console.Read();
if (j.Equals('Y') || j.Equals('y'))
{
path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
temp1 = true;
}
}
//User inputs y/Y loop will end and exit with either path chosen by user or with desktop path
} while (!temp1);
path = Console.ReadLine(); is being skipped if user fails to insert correct path. Been looking for the solution since yesterday and I have failed to find identical problem on the web. Link to full code: Code.

The call isn't being skipped - the problem is that Console.Read() will only return after the user has hit return - although it will only consume the first character it reads. So suppose that (when prompted about skipping) the user enters:
Nfoo
and then hits return... the value of path in the next iteration will be foo.
The simplest fix is probably to convert your Console.Read() call into Console.ReadLine() and just handle the situation where the user types more than one character.

It's much more useful to use Console.ReadKey for that - it will read exactly one key, and will not require you to press enter.

Related

C# - Passively checking for key press

I am not using windows forms so this is not a duplicate of Capture keystroke without focus in console. Please remove the duplicate label or direct me somewhere else
So have been away from C# for a long time and trying to get back into it. I am messing with a small console app that requires inputting text from the user. The whole program works fine but now I want to add a check to see if escape is ever pressed.
I originally used ReadKey, but that just checks the current key which has two problems.
1. it uses the key pressed, so strings are missing a character (the one which was checked)
2. it is only in the moment. I want it to be passively waiting until its pressed
What would be the best way to do this?
ex:
I type the string "Hello World!"
If I press the desired key(lets say escape) at any time, I want it to react. Otherwise the string should be entered like normal
edit
example of made up dictionary program (yes, I know there is already a class for this)
while (Console.ReadKey().Key != ConsoleKey.Escape)
{
string entry = Console.ReadLine();
if (!entry.Contains(","))
{
...
}
else
{
...
}
}
Thank you all very much for your time.
Not sure what you're getting at but you can use this to detect if the Escape key was pressed.
if (Console.KeyAvailable)
if (Console.ReadKey(true).Key == ConsoleKey.Escape)
{
// Do something
}
}
Or alternatively use a loop that breaks when Escape is entered:
var x = Console.ReadKey();
while (x.Key.ToString() != "Escape")
x = Console.ReadKey();

Adding values to a string array in C#

I am building a calendar. Here I want to add members to my meeting. After every name I want to press enter to add it. When I press, for example X I want to leave the "add member" process and go to next step, for example, "adding a meeting title".
How can I add members to my array? How can I go to the next step when I'm done entering names? This code doesn't work:
Console.Write("Enter members here: ");
memberList = Console.ReadLine();
string[] memberList;
You'll need to use a conditional loop to accomplish this, a while loop would work pretty good.
Also, using a List<string> would be better in this scenario since you (i) need to add things to it, and (ii) you don't know how big to make the array when you first declare it since you don't know how many names the user will enter.
Something like:
var names = new List<string>();
var input = Console.ReadLine();
while (input.ToUpper() != "X")
{
names.Add(input);
input = Console.ReadLine();
}
foreach (var name in names)
{
Console.WriteLine(name);
}
If you're wanting to move to the next step immediately after an user presses X (without them having to press Enter), you can look into using Console.ReadKey but it'd be more complicated since you'd have to collect one character at a time to get the name and check if they key pressed is Enter, in which case you'd move on to the next name. There's also the complexity of known when a "X" is just part of someone's name, e.g. Xavier, or whether meant to move on to the next step.
Assuming the user types in the following: "Jake, Julia, Craig"
string[] memberlist = Console.ReadLine().Split(',');
Eventually you need to .Trim() on every name cause of the whitespace after the comma.
Edit: For a "multiline" solution look at #Jeff Bridgman post.

Sum up two steps into one step

In a class in a console application I wrote a method that reads many integer inputs and sums them up. The inputs calls for another method written in another class (Input.ReadIntegerConsole()) that's supposed to read the input, and then make the operations of the first class continue if the input is an integer, or otherwise to write "invalid choice", this way:
private void ReadInputAndSumNumbers()
{
Console.Write("\nNumber you choose? ");
int.TryParse(Console.ReadLine(), out numberChosen);
numberChosen = Input.ReadIntegerConsole();
sum += numberChosen;
etc. The problem is that this way, when I run the application, the console asks: Number you choose? then whatever I input, the program does nothing. At this point I will put another input and only now the ReadIntegerConsole method starts: if it is an integer the operations continue, otherwise the program displays "invalid choice". How to do it so the program does everything in one step instead of two steps, so it reads my input and immediately verifies if it is an integer or not and decides to continue or to display "invalid choice"? How to do it without having to write two inputs? I know for sure that the Input.ReadIntegerConsole code is written correctly and that's why I didn't display it here. I tried writing in many different parts of the class that numberChosen = Input.ReadIntegerConsole() but it has never worked. I tried also writing numberChosen=int.Parse(ConsoleReadLine())=Input.ReadIntegerConsole() but of course it doesn't work.
Console.Write("\nNumber you choose? ");
int.TryParse(Console.ReadLine(), out numberChosen);
numberChosen = Input.ReadIntegerConsole();
sum += numberChosen;
You didn't provide the implementation of Input.ReadIntegerConsole but its a fair guess that it also calls Console.ReadLine and parses it into an int (same as the line above it). So basically, your code is doing:
Print a message
Wait for user input, parse into an int and store in numberChosen
Call Input.ReadIntegerConsole which likely waits for user input, parses it into an int and returns it, and store it in numberChosen
Add the number to sum
So when you input the first number, it does exactly what you think, but Input.ReadIntegerConsole asks for another input and overwrites the original one (hence if you enter junk, it says "invalid value"). This second read from the console explains why the program appears to do nothing, since its waiting for more input.
Removing the manual Console.ReadLine gets rid of the redundant read/parse, which is why it fixed your code. Its not that it works without Console.ReadLine, its that the method you are using hides a call to it.
If you're asking what I think you're asking, I think you want this:
private void ReadInputAndSumNumbers()
{
Console.Write("\nNumber you choose? ");
int numberChosen;
if(int.TryParse(Console.ReadLine(), out numberChosen)) {
sum += numberChosen;
}
else {
Console.WriteLine("Invalid choice.");
}
}
int.TryParse is a method with an out parameter, which means the result of parsing will be assigned to numberChosen. It returns a bool, however: true if successful (string passed is parseable into an int), or false if unsuccessful. So what it's saying is:
If int.TryParse can create an int from the input string, the variable numberChosen has the result stored when the method finishes; add numberChosen to sum.
else, it was unsuccessful, and write "Invalid choice".
Out parameters can get a little confusing, but they're powerful. But if I did a poor job explaining, here are some links: Int32.TryParse, out (C# reference)

C# Console Application - Calling Main method or Class?

I have exercise/drill/homework assignment to create a C# program to compute the value of a number raised to the power of a second number. Read the two numbers from the keyboard.
Ask the user for an integer.
Print the integer back to the screen and asked if it is correct.
If the integer is correct, continue on.
If the integer is incorrect, start program from the beginning.
I have two questions:
Can and how do I programmatically clear the console window?
Start over, do I call the Main method or the Class?
How do I do either, calling the main method or the class?
Here's what I've written so far:
using System;
using System.Text;
namespace CalcPowerOfNums
{
class Program
{
//Declaring the two main string variables to be used in our calculation.
string firstUserString;
string secondUserString;
static void Main(string[] args)
{
//Ask the user for the first number.
Console.WriteLine("Enter your first number and press the Enter/Return key");
string firstUserString = Console.ReadLine();
//Make sure this number is correct.
Console.WriteLine("You want to find the power of {0}?\n" , firstUserString);
//Declaring, Initializing string variables for user answer.
string firstAnswer = "";
//Make user confirm or deny their choice.
Console.WriteLine("Press the lowercase letter y for yes");
Console.WriteLine("Press the lowercase letter n for no");
Console.ReadKey();
//If user answer is yes, move on… It user answer is no, start program over.
do
{
if (firstAnswer == "y")
continue;
if (firstAnswer == "n")
}
Looking at the Console class, you will find a Clear method that will clear the console screen. As to calling Main, that will be called automatically by default in a console project as long as you have declared it. You can have a look in your project properties at the Startup Object setting.
When you say "start the program over" I am assuming you mean clear the window and ask for the input again, not reload the entire process.
You can use Console.Clear() to clear the console window. The main method is called automatically from Program.cs. Just place your main code in a while loop and loop until you get the desired input. If you don't get the desired input, just issue a Console.Clear() and ask again until you do.
I have two questions: Can and how do I programmatically clear the console window?
Yes, by calling Console.Clear.
Do I call the Main method or the Class?
You cannot invoke a class, and you should never call main directly. Just put a do/while loop around it with 'is correct' as the condition:
do {
...all regular code...
} while(firstAnswer == 'y');
What about:
Console.Clear();
?
you should make functions for this. Put the enter name part in a function then call this function at the start of the Main function and in the if statements.

C# Saving "X" times into one .txt file without overwriting last string

Well, now i have a new problem.
Im writing code in C#
I want to save from textBoxName into group.txt file each time i enter string into textbox and click on save button. It should save at this order (if its possible to sort it like A-Z that would be great):
1. Petar Milutinovic
2. Ljiljana Milutinovic
3. Stefan Milutinovic
4. ... etc
I cant get it to work, i tried to use tehniques from my first question, and no solution yet :(
This is easy one i guess, but im still a beginer and i need this baddly...
Try to tackle this from a top-down approach. Write out what should happen, because it's not obvious from your question.
Example:
User enters a value in a (single-line?) textbox
User clicks Save
One new line is appended to the end of a file, with the contents of the textbox in step 1
Note: each line is prefixed with a line number, in the form "X. Sample" where X is the line number and Sample is the text from the textbox.
Is the above accurate?
(If you just want to add a line to a text file, see http://msdn.microsoft.com/en-us/library/ms143356.aspx - File.AppendAllText(filename, myTextBox.Text + Environment.NewLine); may be what you want)
Here's a simple little routine you can use to read, sort, and write the file. There are loads of ways this can be done, mine probably isn't even the best. Even now I'm thinking "I could have written that using a FileStream and done the iteration for counting then", but they're micro-optimizations that can be done later if you have performance issues with multi-megabyte files.
public static void AddUserToGroup(string userName)
{
// Read the users from the file
List<string> users = File.ReadAllLines("group.txt").ToList();
// Strip out the index number
users = users.Select(u => u.Substring(u.IndexOf(". ") + 2)).ToList();
users.Add(userName); // Add the new user
users.Sort((x,y) => x.CompareTo(y)); // Sort
// Reallocate the number
for (int i = 0; i < users.Count; i++)
{
users[i] = (i + 1).ToString() + ". " + users[i];
}
// Write to the file again
File.WriteAllLines("group.txt", users);
}
If you need the file to be sorted every time a new line is added, you'll either have to load the file into a list, add the line, and sort it, or use some sort of search (I'd recommend a binary search) to determine where the new line belongs and insert it accordingly. The second approach doesn't have many advantages, though, as you basically have to rewrite the entire file in order to insert a line - it only saves you time in the best case scenario, which occurs when the line to be inserted falls at the end of the file. Additionally, the second method is a bit lighter on the processor, as you aren't attempting to sort every line - for small files however, the difference will be unnoticeable.

Categories

Resources