How to removed the displayed text in a console application, c#? - c#

Suppose a user is asked for the manual. Now we all know that manuals are handy and extensive, how do I collapse/clear for instance so that the user can proceed to the next activity? By pressing C after pressing M, I want the user to immediately jump to the next acitivity.
How do I remove this?
Here is the code:
static void Main(string[] args)
{
Console.WriteLine("Do you want to play the game?\nPress Y to continue\tPress M to view the rules\tEnter any key to exit. ");
string iQuestion = Console.ReadLine();
if (iQuestion.Equals("y", StringComparison.OrdinalIgnoreCase))
{
ProjectExecution();
Console.WriteLine("\nWorld you like to try again?\nPress y to play again\tEnter any key to exit. ");
string ask = Console.ReadLine();
if (ask.Equals("y", StringComparison.OrdinalIgnoreCase))
{
ProjectExecution();
}
else
{
Console.WriteLine("Thank you for playing");
}
Console.Read();
}
else if (iQuestion.Equals("m", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Hwewewe\nPress C to continue");\\This is just an example
string mQuestion = Console.ReadLine();
if (mQuestion.Equals("c", StringComparison.OrdinalIgnoreCase))
{
Console.Clear();
ProjectExecution();
Console.Read();
}
}
else
{
Environment.Exit(10);
}
}
I am open to another solution.

Related

I don't know what I'm doing wrong in my C# code

I am attempting to make a simple program that counts up one each time I press spacebar. I am very very new to writing software and most of this is completely foreign to me, so if you understand how I am thinking incorrectly I will welcome your insight!
using System;
namespace Counter
{
class Program
{
static void Main(string[] args)
{
var tap = 0;
while (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
tap++;
Console.WriteLine();
}
}
}
This is what I tried. The console shows (0) and then closes when I press spacebar. I don't know how to stop it from closing the console, it's as if all it has in the code body is a variable and "Console.WriteLine()".
try this:
var tap = 0;
while (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
{
tap++;
Console.WriteLine($"Key is not Spacebar, tap count = {tap}");
}
Console.WriteLine("You just press spacebar, now press enter to exit this console app");
Console.ReadLine();//press enter to exit
static void Main(string[] args)
{
int tap = 0;
while (Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
tap++;
Console.WriteLine("You pressed 'Spacebar' " + tap + "-time(s) so far.");
}
Console.WriteLine("You didn't press 'Spacebar' this time!");
System.Threading.Thread.Sleep(3000); // wait 3 seconds so you actually have the chance to read the text in the console
}
Event though I would recommend something like this instead:
static void Main(string[] args)
{
int tap = 0;
while(true)
{
if(Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
tap++;
Console.WriteLine("You pressed 'Spacebar' " + tap + "-time(s) so far.");
}
else
{
Console.WriteLine("You didn't press 'Spacebar' this time!");
System.Threading.Thread.Sleep(3000); // wait 3 seconds so you actually have the chance to read the text in the console
break;
}
}
}
I don't know if this is how to resolve a question in stackoverflow, but the answer was to make an if/elseif loop where Spacebar was an input that was counted and Enter was a Boolean value that closed the program when triggered to True.
{
var tap = 0;
bool quit = false;
while (quit == false)
{
if (Console.ReadKey().Key == ConsoleKey.Spacebar)
{
tap++;
Console.WriteLine(tap);
}
else if (Console.ReadKey(true).Key == ConsoleKey.Enter)
{
quit = true;
}
}
}
With the ElseIf I was able to count every trigger of Spacebar, whereas previously I had that statement within the If loop and it waited on the next keypress to see whether it was the Enter key or not.
Thanks for the help!

How would I be able to make the program stop with only a specific input in C#

I am very new to C# and I have just created my first program, which is your classic "number guesser".
In my program, after the user has guessed the correct number, I want to give them an option where they can either type "Y" or "N" to continue or end the game.
Where my issue lies is, if the user were to type in the letter "G" at this stage, the program would continue and ask the user to input another number.
How do I enable my code to keep looping at this stage until either Y is pressed to continue the game or N is pressed to end the program?
using System;
// Namespace
namespace NumberGuesser
{
// Main Class
class Program
{
// Entry Point Method
static void Main(string[] args)
{
GetAppInfo(); // Run GetAppInfo function to get info
GreetUser(); // Ask for user's name and greet
while (true)
{
// Create a new Random object
Random random = new Random();
// Initial correct number
int correctNumber = random.Next(1, 11);
// Initial guess var
int guess = 0;
// Ask user for number
Console.WriteLine("Guess a number between 1 and 10:");
// While guess is not correct
while (guess != correctNumber)
{
// Get users input
string input = Console.ReadLine();
// Make sure it's a number
if (!int.TryParse(input, out guess))
{
// Print error message
PrintColourMessage(ConsoleColor.Red, "Please user an actual number.");
// Keep going
continue;
}
// Make sure the number guessed is between 1 - 10
if (guess > 10)
{
// Print error message
PrintColourMessage(ConsoleColor.Red, "Please enter a number from 1 to 10.");
// Keep going
continue;
}
// Cast to int and put in guess
guess = Int32.Parse(input);
// Match guess to correct number
if (guess != correctNumber)
{
// Print error message
PrintColourMessage(ConsoleColor.Red, "Wrong number, please try again.");
}
}
// Print success message
PrintColourMessage(ConsoleColor.Yellow, "You are CORRECT!!!");
// Ask to play again
Console.WriteLine("Play again? [Y or N]");
// Get answer
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
{
continue;
}
else if (answer == "N")
{
return;
}
else
{
return;
}
}
}
// Get and display app info
static void GetAppInfo()
{
// Set app vars
string appName = "Number Guesser";
string appVersion = "1.0.0";
string appAuthor = "Jack Thomas";
// Change text colour
Console.ForegroundColor = ConsoleColor.Green;
// Write out app info
Console.WriteLine("{0}: Version {1} by {2}", appName, appVersion, appAuthor);
// Reset text colour
Console.ResetColor();
}
// Ask user's name and greet
static void GreetUser()
{
// Ask users name
Console.WriteLine("What is your name?");
// Get user input
string inputName = Console.ReadLine();
Console.WriteLine("Hello {0}, let's play a game...", inputName);
}
// Print colour message
static void PrintColourMessage(ConsoleColor color, string message)
{
// Change text colour
Console.ForegroundColor = color;
// Prints message
Console.WriteLine(message);
// Reset text colour
Console.ResetColor();
}
}
}
Just instead of doing this:
// Ask to play again
Console.WriteLine("Play again? [Y or N]");
// Get answer
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
{
continue;
}
else if (answer == "N")
{
return;
}
do that:
// Ask to play again
Console.WriteLine("Play again? [Y or N]");
bool toContinue = false;
bool invalidResponse = true;
while(invalidResponse) {
// Get answer
string answer = Console.ReadLine().ToUpper();
if (answer == "Y")
{
toContinue = true;
invalidResponse = false;
}
else if (answer == "N")
{
invalidResponse = false;
}
}
if(toContinue != true) return;
I've created while loop wich loops while invalidResponse variable is true, invalidResponse is true when we not entered any of these characters "Y" or "N".
You can also make Console.WriteLine when user enters invalid response.
If user does not entered "Y" it sets toContinue to false and it ends program.

c# console: How to ReadLine without the need of pressing [Enter]

My c# console application is used as a login for my c# form application, the problem is, in my c# console app i haven't been able to figure out a way to ReadLine without the need of pressing Enter because i need to detect whether F2 or Enter is pressed then ReadLine without needing user to press Enter again. For example, if i wanted to detect if F2 is pressed i would need to wait until F2 is pressed until I'm able to ReadLine, Hopefully this question was worded in a way that it makes sense, I'm sure you can tell I'm quite a 'noob' at c#.
Example of my problem:
static void Main()
{
var KP = Console.ReadKey();
if (KP.Key == ConsoleKey.F2)
{
//User Presses F2
}
else if (KP.Key == ConsoleKey.Enter)
{
string UserName = ReadLineWithoutPressingEnter();//Just a example
//ReadLine without needing to press enter again
}
}
Thank you for your time.
Save the result from ReadKey and then just do a ReadLine:
public static void Main(string[] args)
{
var KP = Console.ReadKey();
if (KP.Key == ConsoleKey.F2)
{
return;
}
string UserName = KP.KeyChar + Console.ReadLine();
Console.WriteLine(UserName);
Console.ReadLine();
}
You've already found Console.ReadKey(). That's a start. You'll need to also build a state machine around this function to return a completed string at the end of the line, but this method is the key to making that work. Don't forget to handle things like backspace and delete.
Here is an example Try this
static void Main(string[] args)
{
ConsoleKeyInfo cki = new ConsoleKeyInfo();
int i = 0;
do
{
while (Console.KeyAvailable == false)
Thread.Sleep(250); // Loop until input is entered.
cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.F1)
{
Console.WriteLine("User Have Press F1");
//do some thing
}
if (cki.Key == ConsoleKey.Enter)
{
Console.WriteLine("User Have Press Enter");
//do some thing
}
if (cki.Key == ConsoleKey.A)
{
Console.WriteLine("User Have Press A");
//do some thing
}
} while (cki.Key != ConsoleKey.X);
}
This should work
static void Main(string[] args)
{
ConsoleKeyInfo e;
string userName = "";
while (true)
{
e = Console.ReadKey();
if (e.Key == ConsoleKey.Enter)
{
break;
}
else if (e.Key == ConsoleKey.F2)
{
//things to do when F2
}
userName += e.KeyChar;
}
Console.WriteLine("username: " + userName);
Console.Read();
}

How get out from class

I have created a small project . and here is it:
class Program
{
public static void Main()
{
Console.WriteLine("Welcome");
Console.WriteLine("1 to go to Data Files ");
Console.WriteLine("type quit to exit");
string input = Console.ReadLine();
if (input == "1")
{
Data go = new Data();
}
else if (input == "quit")
{
}
}
}
When user type quit. I want my program to exit. anyone help me, please
You just need something like this:
else if (input == "quit")
{
return;
}
Update: based on your comments I think you're looking for something like this:
class Program
{
public static void Main()
{
while(true)
{
Console.WriteLine("Welcome");
Console.WriteLine("1 to go to Data Files ");
Console.WriteLine("type quit to exit");
string input = Console.ReadLine();
if (input == "1")
{
Data go = new Data();
}
else if (input == "quit")
{
return;
}
else
{
Console.WriteLine("invalid option");
}
}
}
}
You just can use Environment.Exit(code). where code is integer representation of standard application-exit-codes like return 0 or return 1 in world of C++.

How to return to main menu with many menu inside?

If I have a lot of menu on my program how do i return on my main menu without using any loop.
Assume that I need "Y" for return,"N" for end program.
static void Main(string[] args)
{int choice;
Console.WriteLine("Main Menu");
Console.WriteLine("1. Rent");
Console.WriteLine("2. Return");
Console.WriteLine("3. Exit");
choice = Convert.ToInt32(Console.ReadLine());
while (choice!= 3)
{
if (choice ==1)
{...
//when complete all thing in choice 1
Console.WriteLine("Do you want to start over?(Y=Yes,N=No)");
// in this part i need to go back to main menu with "Y" input and close program with "N"input
}
if (choice ==2)
{...
//when complete all thing in choice 2
Console.WriteLine("Do you want to start over?(Y=Yes,N=No)");
// doing like choice 1 }
I would write it differently, but based on your code, this will get you what you want.
static void Main(string[] args)
{
int choice = 0;
while (choice != 3)
{
Console.WriteLine("Main Menu");
Console.WriteLine("Rent=1");
Console.WriteLine("Return=2");
Console.WriteLine("Exit=3)");
choice = GetUserChoice("What is your choice?", choice);
if (choice == 1)
{
//when complete all thing in choice 1
choice = GetUserChoice("Do you want to start over?(Y=1,N=3)", choice);
}
else if(choice == 2)
{
//when complete all thing in choice 2
choice = GetUserChoice("Do you want to start over?(Y=1,N=3)", choice);
}
}
}
private static int GetUserChoice(string question, int choice)
{
Console.WriteLine(question);
return Convert.ToInt32(Console.ReadLine());
}
Notice inside the choices when it asks about starting over, I would have the user enter 1 for Y(yes) and 3 for N(no).

Categories

Resources