Thread: Statement that will tell the program to end?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    51

    Statement that will tell the program to end?

    I'm a noob at C and I need to know how to end the program in the middle so it won't do any more steps. I'm not using loops so break won't work. Please help!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up exit() function; IRCC in the stdlib.
    Note: make sure the return statement is not enough to solve your problem before using exit.

    Edit: C++ docs on exit http://www.cplusplus.com/reference/c.../cstdlib/exit/

    Tim S.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by stahta01 View Post
    Look up exit() function; IRCC in the stdlib.
    Note: make sure the return statement is not enough to solve your problem before using exit.

    Tim S.
    Thanks, but the exit statement isn't working. Here is my code:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    
    {
          int a, b, c, X, D, x1, x2;
          
          printf ("This program solves a quadratic equation.\n");
          printf ("Enter the value of A: ");
          a = GetInteger();
          printf ("Enter the value of B: ");
          b = GetInteger();
          printf ("Enter the value of C: ");
          c = GetInteger();
          if (a = 0)
          if (b = 0)
          {
                printf ("no solution.");
                exit();
                }
                else
                {
                    X = - c / b;
                    printf ("The equation is not quadratic and the solution is: %d\n", X);
                    exit();
                    }
          D = b * b - 4 * a * c;
          if (D < 0)
          {
                printf ("There is no real solution.\n");
                exit();
                }
                if (D = 0)
                {
                      x1 = - b / 2 * a;
                      printf ("One solution: %d", x1);
                      exit();
                      }
          x1 = (- b + sqrt(D)) / 2 * a;
          x2 = (- b - sqrt(D)) / 2 * a;
          printf ("Two solutions: %d and %d", x1, x2);
          getchar();
          }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    From the link provided by stahta01:

    Code:
     void exit ( int status );
    
    status
    Status value returned to the parent process. Generally, a return value of 0 or EXIT_SUCCESS indicates success, and any other value or the constant EXIT_FAILURE is used to indicate an error or some kind of abnormal program termination.
    Error from my compiler:

    Code:
     insufficient number of arguments to 'exit'
    Now put them together, and you'll find the answer.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is a difference between = (assignment) and == (equality test). All your "if" statements need to be using == and not =.

    Also, you should #include the stdlib header if you want to use exit.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You do not need exit here: you should either structure your code such that returning at the end of main will do, or use return statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by hk_mp5kpdw View Post
    There is a difference between = (assignment) and == (equality test). All your "if" statements need to be using == and not =.

    Also, you should #include the stdlib header if you want to use exit.
    I'm getting the error "too few arguments to function 'exit'."

    Matticus, I don't understand.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by laserlight View Post
    You do not need exit here: you should either structure your code such that returning at the end of main will do, or use return statements.
    I haven't learned return statements yet. How would I structure it right?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by PYROMANIAC702
    I haven't learned return statements yet. How would I structure it right?
    Consider the structure:
    Code:
    if x
        if y
            #1
        else
            #2
    else
        if z
            #3
        else
            #4
    Despite no return statements or exit, at the end of the code, either #1, #2, #3 or #4 would have been executed, and none of the other three.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by PYROMANIAC702 View Post
    I'm getting the error "too few arguments to function 'exit'."

    Matticus, I don't understand.
    Did you try passing an argument to the function? "exit()" expects an argument, as described in the description I quoted.

    If you don't know how to pass arguments to a function, then you have a lot more studying ahead of you.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by Matticus View Post
    Did you try passing an argument to the function? "exit()" expects an argument, as described in the description I quoted.

    If you don't know how to pass arguments to a function, then you have a lot more studying ahead of you.
    No, I do not... As I already stated, I am a programming noob. I'm only in my first class.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by PYROMANIAC702 View Post
    No, I do not... As I already stated, I am a programming noob. I'm only in my first class.
    Well, I won't give away the prize, but it will be difficult to give you a clue without practically giving you the answer. Hopefully, this analogy will help you understand.

    Code:
      printf ("There is no real solution.\n");
    
      // "printf()" is a function.
      // "There is no real solution.\n" is the argument passed to that function.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by PYROMANIAC702
    No, I do not... As I already stated, I am a programming noob. I'm only in my first class.
    That is fine. I am going to reiterate: the use of exit here is the wrong approach. Consider how to structure your if statements such that you get the result that you want.

    The way I see it, if you want to use exit, it is because some error happened that you cannot recover from, so your only option is to end the program there and then. In this case, you are just defining the logic by which you compute the solution(s) to the equation.

    You should eventually learn how to call a function and use return statements, but if you use exit as you intend here, I am really going to facepalm.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by PYROMANIAC702 View Post
    I haven't learned return statements yet.
    DO NOT try to use exit! If you not NOT know how to use return you have no business using exit().

    After you fix your if statements using "==" instead of "=", try doing what laserlight suggested and rewrite your if statement.

    Tim S.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by laserlight View Post
    That is fine. I am going to reiterate: the use of exit here is the wrong approach. Consider how to structure your if statements such that you get the result that you want.

    The way I see it, if you want to use exit, it is because some error happened that you cannot recover from, so your only option is to end the program there and then. In this case, you are just defining the logic by which you compute the solution(s) to the equation.
    Ok, I did what you told me too and it "kind of" worked. I can't do a continuous if, else if , else because then it won't work. Here's what I did, which still doesn't work in one occasion.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    
    {
          int a, b, c, X, D, x1, x2;
          
          printf ("This program solves a quadratic equation.\n");
          printf ("Enter the value of A: ");
          a = GetInteger();
          printf ("Enter the value of B: ");
          b = GetInteger();
          printf ("Enter the value of C: ");
          c = GetInteger();
          if (a == 0)
          if (b == 0)
          {
                printf ("no solution.");
                }
                else
                {
                    X = - c / b;
                    printf ("The equation is not quadratic and the solution is: %d\n", X);
                    }
          D = b * b - 4 * a * c;
          if (D < 0)
          {
                printf ("There is no real solution.\n");
                }
                else if (D == 0)
                {
                      x1 = - b / 2 * a;
                      printf ("One solution: %d", x1);
                      }
          else
          {            
          x1 = (- b + sqrt(D)) / 2 * a;
          x2 = (- b - sqrt(D)) / 2 * a;
          printf ("Two solutions: %d and %d", x1, x2);
                 }
          getchar();
          }
    If I enter 0, 3, -6 it gives me two answers: "The equation is not quadratic and the solution is: 2" and "Two solutions : 0 and 0." I have no clue how to fix this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with if statement program plz
    By allen9190 in forum C Programming
    Replies: 3
    Last Post: 12-28-2009, 06:18 PM
  2. help with c++ program statement
    By robasc in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2009, 08:57 AM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. Program not seeing the if statement
    By adjac in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2004, 08:04 PM
  5. help with program using switch statement
    By Aysha in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 04:43 PM