I would like to know if this is a correct way to call and define a function for my program

#include <iostream>
#include <cmath>
using namespace std;
int math(double a, double b, double c);
int main( )
{
int math;
char a,b,c;
math = a + b + c;

return (0);
}
int math()
{
char ans = 'y';
  bool quad_answer = false;
 do {


double a, b, c;
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
double discriminant = (pow(b,2) - 4*a*c);

double positive_root = (((-b) + sqrt(discriminant))/(2*a));

double negative_root = (((-b) - sqrt(discriminant))/(2*a));


if (discriminant == 0)
    {
    cout << "\n\ndiscriminant ";

    cout << discriminant << endl;

    cout << "So there is one real root valued at.\n\n";
    }
else if (discriminant < 0)
    {
    cout << "\n\ndiscriminant ";
    cout << discriminant << endl;
    cout << "So there is two complex roots.\n\n";
    }
else
    {
    cout << "\n\ndiscriminant ";
    cout << discriminant << endl;
    cout << "So there are two real roots valued at.\n\n";
    }


cout << "The roots of the quadratic equation are x = ";
cout << negative_root;
cout << ", ";
cout << positive_root << endl<< endl;

if (quad_answer) cout << endl;
     cout << "Continue (y/n)? ";
     cin  >> ans;
  } while (ans == 'y');

return 0;


}

Recommended Answers

All 4 Replies

on line #7 it should say math(); not int math;
and, yes that is a proper way to make a function. you just werent calling it right :P

i change int math; to math(); but when i rebuild it i get error C2660: 'math' : function does not take 0 arguments
cpp(8): error C2144: syntax error : 'char' should be preceded by ';'
cpp(9): error C2659: '=' : function as left operand
i don't know if it is whether i used the char value or its something else

In line 4, you have decInline Code Example Herelared math() prototype as int math(double a, double b, double c);.
Inside main() you are declaring a integer variable with the same name. And without initializing you are using the varibles a, b, c. You don't need these.
Also in math() function definition, you have kept the arguments void, but in prototype it's said it will accept 3 double variables.

Change prototype to void math(); and in main() just call math(). And yes, if the return type is void, then omit the return (0) part in line 66.

The function 'math()' and variable 'math' are conflicting symbols. Change the name of one or the other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.