string FirstName = Console.ReadLine();
if (FirstName.Length > 12)
{
Console.WriteLine(".......................................");
}
if(FirstName.Length<3)
{
Console.WriteLine("....................");
}
Console.WriteLine("...................");
string SecondName = Console.ReadLine();
if (SecondName.Length > 12)
{
Console.WriteLine(".............................");
}
if(SecondName.Length<3)
{
I want to stop the program if they press enter without putting a value,how to do it??/?
Exit() is a core function in the C/C++ programming language that is used to instantly end the calling process (function). It is possible to call from any function. It informs the operating system of the state of program termination by passing an int value. It is usually used when software crashes unexpectedly.
The usual way for a program to terminate is simply for its main function to return. The exit status value returned from the main function is used to report information back to the process's parent process or shell. A program can also terminate normally by calling the exit function.
You can terminate program execution by using the quit or kill commands. The quit command terminates both the debugger and the debugged process and returns you to the shell.
exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.
string key = Console.ReadKey().ToString(); //Read what is being pressed
if(key == "") {
Console.WriteLine("User pressed enter!");
return; //stop further execution
}
I think you want to have a non empty string value as an input from console and if input is empty then want to terminate your application. Use following code:
Console.WriteLine("Enter a value: ");
string str = Console.ReadLine();
//If pressed enter here without a value or data, how to stop the program here without
//further execution??
if (string.IsNullOrWhiteSpace(str))
return;
else
{
Console.WriteLine(string.Format("you have entered: '{0}'", str));
Console.Read();
}
If user will enter any kind of empty string or white spaces, the application will be terminated the moment he/she will press enter.
Console.ReadLine()
returns a string. If nothing is typed and the human just presses the enter key, we'll get an empty string.
There are a number of ways to test whether a string is "empty" for various definitions of empty. Some common definitions of empty and how to test for them:
null
and containing no data: myString.Length == 0
null
or containing no data: string.IsNullOrEmpty(myString)
null
, empty, or just whitespace: string.IsNullOrWhiteSpace(myString)
Environment.Exit()
will end the process with the exit code you specify.
Combine one of the tests above with Environment.Exit()
(and probably an if
) and you can stop the process when there's no "value or data".
Also note, that returning from Main
is another way to exit the process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With