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!
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!
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.
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(); }
From the link provided by stahta01:
Error from my compiler: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.
Now put them together, and you'll find the answer.Code:insufficient number of arguments to 'exit'
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
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Consider the structure:Originally Posted by PYROMANIAC702
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.Code:if x if y #1 else #2 else if z #3 else #4
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.Originally Posted by PYROMANIAC702
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.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(); }