Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop program executing further in C#

Tags:

c#

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??/?

like image 601
Shalya Avatar asked Nov 28 '13 05:11

Shalya


People also ask

What is exit () in C language?

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.

How do you terminate a program?

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.

Which command terminates the execution of a program?

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.

What does exit 0 do in C?

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.


3 Answers

string key = Console.ReadKey().ToString();  //Read what is being pressed
if(key == "") {
    Console.WriteLine("User pressed enter!");
    return; //stop further execution
}
like image 72
Praveen Avatar answered Oct 16 '22 20:10

Praveen


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.

like image 25
Nitin Joshi Avatar answered Oct 16 '22 20:10

Nitin Joshi


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:

  • non-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.

like image 5
chwarr Avatar answered Oct 16 '22 20:10

chwarr