2 Replies - 342 Views - Last Post: 04 October 2010 - 04:57 PM Rate Topic: -----

#1 Essel  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

Exit sub or code

Posted 04 October 2010 - 04:42 PM

Please i am testting a condution and if the result is false i want to exit if a==0.How do i do that i c# this is my code

  Console.WriteLine("Please Enter A");
            a = int.Parse(Console.ReadLine());
            if (a == 0)
            {
                Console.WriteLine("Please Zero is not required");
                Console.ReadLine();
                exit (1);
            }

            else
            {
                Console.WriteLine("Please Enter B");
                b = int.Parse(Console.ReadLine());

                Console.WriteLine("Please Enter C");
                c = int.Parse(Console.ReadLine());

                double roots = ((b * B)/> - (4 * a * c));
                if (roots > 0)
                {
                    x1 = ((-b + (Math.Sqrt(b * b - 4 * a * c))) / (2 * a));
                    x2 = ((-b - (Math.Sqrt(b * b - 4 * a * c))) / (2 * a));

                    Console.Write("The roots are");
                    Console.Write((Math.Round(x1, 2)));

                    Console.Write("And");
                    Console.Write((Math.Round(x2, 2)));

                    Console.ReadLine();

                }
                else if (roots < 0)
                {
                    Console.WriteLine("The roots are Imaginary");
                    Console.ReadLine();
                }
                else if (roots == 0)
                {
                    Console.WriteLine("Have two real and equal roots");
                    Console.ReadLine();
                }




Is This A Good Question/Topic? 0
  • +

Replies To: Exit sub or code

#2 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Exit sub or code

Posted 04 October 2010 - 04:52 PM

Assuming this is a Console application, and that this code is inside your 'main' method, instead of calling 'exit(1)' you can just use:

return;



Does this fix it?

EDIT: Forgot that 'main' has no return type so changed suggestion accordingly >_<

This post has been edited by scalt: 04 October 2010 - 04:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Exit sub or code

Posted 04 October 2010 - 04:57 PM

return in general... return a default value for the type your function is... if void just say 'return'.


void foo() { return; }

int foo() { return 0; }

bool foo() { return false; }

object foo() { return null; }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1