0

I'm trying to make a program to calculate the output of a 2nd degree equation, using functions, but Dev C++ keeps giving me the output "[Error] Id returned 1 exit status". I'm a newbie in C++, sorry in advance if I made some stupid errors.

#include <iostream>
#include <cmath>
using namespace std;

float equation1 (float, float, float);
float equation2 (float, float, float);

main()
{
    float a, b, c, Res1, Res2;
    cout << "Insert the parameters of the equation.\n";
    cin >> a >> b >> c;
    if (a == 0)
    {
        Res1 = b / c;
        cout << "It's a 1st degree eq. and the result is " << Res1 << endl;
    }
    else
    {
        Res1 = equation1 (a, b, c);
        Res2 = equation2 (a, b, c);
        cout << "The results of the eq. are " << Res1 << " and " << Res2 << endl;
    }
    system ("pause");
    return 0;
}

float equation1 (double a, double b, double c)
{
    float D, Res1;
    D = (b * b) - 4 * a * c;
    Res1 = (- b + sqrt(D)) / (2 * a);
    return Res1;
}

float equation2 (double a, double b, double c)
{
    float D, Res2;
    D = (b * b) - 4 * a * c;
    Res2 = (- b - sqrt(D)) / (2 * a);
    return Res2;
}
Sam
  • 3
  • 3
  • 1
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ May 15 '17 at 20:03
  • 1
    I think you probably meant to write `int main(){ ...}`. Secondly, the linker failed to find the definition of `equation1(float, float, float)`, likewise `equation2(float, float, float)`. Note that the defined funtion `float equation1(double, double, double)` overloads your first declaration. – WhiZTiM May 15 '17 at 20:04
  • @WhiZTiM What should I do to avoid the overload? – Sam May 15 '17 at 20:10
  • ... And after compiling you will probably notice that you are not checking for `c != 0` (you will get a division by zero in `Res1 = b / c;`) and for `D >= 0` when trying get its `sqrt()`. – Todor Simeonov May 15 '17 at 20:10
  • @Sam - you have `float` arguments in the upper declaration and `double` argument where the function bodies are. – Todor Simeonov May 15 '17 at 20:11
  • It is common practice to make the first letter of all variables be lowercase. It would be a good idea to make "Res1" be "res1" instead. – AppWriter May 15 '17 at 20:25

1 Answers1

0

Change the types in the function definitions. You are using double and then C++ expect to operate with float (less precision than double).

float equation1 (float a, float b, float c)
{
    float D, Res1;
    D = (b * b) - 4 * a * c;
    Res1 = (- b + sqrt(D)) / (2 * a);
    return Res1;
}

float equation2 (float a, float b, float c)
{
    float D, Res2;
    D = (b * b) - 4 * a * c;
    Res2 = (- b - sqrt(D)) / (2 * a);
    return Res2;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thank you all for the kind answers! I thought that "float" and "double" were treatable as the same things, my bad. – Sam May 15 '17 at 20:27