Need a fresh pair of eyes.

Anyone see how I can fix this? Everything is running together now.
#include <iostream>
#include <cmath>
using namespace std;

int findRoots (double a, double b, double c, double& result_1, double& result_2 )
{ cout << "Enter an integer for a to find the real roots of a quadratic equation\n";
cin >> a;
cout << "Enter an integer for b to find the real roots of a quadratic equation\n";
cin >> b;
cout << "Enter an intger for c to find the real roots of a quadratic equation\n";
cin >> c;
double root_1;
root_1 = (b*b)- 4*a*c;
if (root_1>=0)
{
result_1 = (-b + sqrt(root_1))/(2*a);
result_2 = (-b - sqrt(root_1))/(2*a);
return 1;
}
else
{
return 0;
}
}
int main()
int findRoots(double x)
{double x;
x = findRoots;
if(x>=0)
{cout << "The real roots of the quadratic equation are" << x;
}
else
{cout << "The results of the quadratic equation are imaginary.\n";
cout << "No real roots exist.";}
return 0;
}
You have defined a function with this first line:

int findRoots (double a, double b, double c, double& result_1, double& result_2 )

so we can see that the function takes five parameters. You then try to declare the function like this
int findRoots(double x)
Just one parameter? Is this an attempt at overloading functions? I expect not, since you're not that advanced in your understanding of functions.

and then you also try to do this.
x = findRoots; Wait, now you're trying to use it with no parameters?
What is findroots here? A function? A variable you never declared? You need to to go back and learn what a function is, and how they work.

Also,
1
2
int main()
int findRoots(double x)

Did you mean
1
2
3
int main()
{
int findRoots(double x);



Last edited on
First of all, why are you declaring your doubles a, b and c in your function itself when you only are using them to get input by cin.

1
2
3
int findRoots(double &result_1, double &result_2)
{
double a, b, c;


This would be a lot cleaner! Second of all, you need to look at your main in a lot cleaner way. Here is me retyping it so you can better see it using the code tags which helps readers see it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>
using namespace std;

int findRoots (double a, double b, double c, double& result_1, double& result_2 )
{
   cout << "Enter an integer for a to find the real roots of a quadratic equation\n";
   cin >> a;
   cout << "Enter an integer for b to find the real roots of a quadratic equation\n";
   cin >> b;
   cout << "Enter an intger for c to find the real roots of a quadratic equation\n";
   cin >> c;
   double root_1;
   root_1 = (b*b)- 4*a*c;
   if (root_1>=0)
   {
      result_1 = (-b + sqrt(root_1))/(2*a);
      result_2 = (-b - sqrt(root_1))/(2*a);
      return 1;
   }
   else
   {
      return 0;
   }
}
int main()
   int findRoots(double x)
   {
      double x;
      x = findRoots;
      if(x>=0)
      {
         cout << "The real roots of the quadratic equation are" << x;
      }
      else
      {
         cout << "The results of the quadratic equation are imaginary.\n";
         cout << "No real roots exist.";}
         return 0;
      }


Your main() has no brackets so I am not sure what you are trying to do there and your next line in your code is crazy, you declared the variable findRoots as an int that takes a double then tried to run it. After reading this code I am not sure you understand the basics of how C++ works and it will take a while to explain every mistake in here. So instead I will suggest starting over with your learning of C++ and post the code you are attempting to do.

To clarify, you function structure is crazy off, you are not using the function you created like it is suppose to be used. You created it to pass in 5 double variables and instead don't pass anything in if you were attempting to use it. In fact I can't even tell what you were trying to do because your function was designed to give you 2 results and you clearly were only looking for one when you ran it in the main function. I am completely confused on what you are trying to do here. I am not trying to insult you in any way, but rather trying to understand what this is and what it is meant to be. I designed the following code to be based on what I believe you were trying to do, but then again I am not entirely sure on it so I am not sure if it will even be of any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
using namespace std;

bool findRoots(double a, double b, double c, double &result1, double &result2)
{
   double root1 = (b*b)-(4*a*c);
   if(root1>=0)
   {
      result1 = ((-1 * b) + sqrt(root_1))/(2*a);
      result2 = ((-1 * b) - sqrt(root_1))/(2*a);
      return true;
   }
   result1 = 0;
   result2 = 0;
   return false;
}

void main()
{
   cout << "Enter an integer for a to find the real roots of a quadratic equation\n";
   cin >> a;
   cout << "Enter an integer for b to find the real roots of a quadratic equation\n";
   cin >> b;
   cout << "Enter an intger for c to find the real roots of a quadratic equation\n";
   cin >> c;
   double answer1, answer2;
   if(findRoots(a, b, c, answer1, answer2))
      cout << "The real roots of the quadratic equation are " << answer1 << " and " << answer2;
   else
      cout << "The results of the quadratic equation are imaginary.\n" << "No real roots exist.";
First of all, why are you declaring your doubles a, b and c in your function itself when you only are using them to get input by cin.


For the history of this troubled function (including the answer to this question), see here:

http://cplusplus.com/forum/beginner/54769/

void int main()
Dude, this is C++
Last edited on
I am trying to create a program that finds the real roots of a quadratic equation making use of a stand alone function to calculate the roots.
Ok, but before you can do that you must understand how functions work and how to use them. It is one of the basics of C/C++ programming. You created a function but then didn't use it at all like it was meant to be used, this was why I was completely confused. Did the code I re-wrote for you help you out at all?

Explaining a function, you have 3 parts. The return of the function, the name, and the input variables

1
2
void main()
int Something(int x, int y)


Here are 2 examples. The first part is the return. In these 2 examples I give, the first one returns nothing (void) and the second one returns an integer that is 32 bit (int). All functions have to return something, even if it returns nothing (which means it returns void).

The second part is the name. The first function is named main while the second one is named Something.

the third part is the input variables which are incased in parenthesis. you can have as many input variables as you like when you create the function but once you do, you must use that amount of variables. In the first function, it takes nothing. You could put void in there to also show this but it isn't needed here like it is in the return part of the function. The second function takes 2 variables, both integers. The function won't allow you to put strings, char*, __int64, or any other type of variable into it, only integers.

There are exceptions to the limit of the third part like the triple period (...) which allows for unlimited input or void pointers which allows any input but that is complicated for this discussion, so for now focus in on functions 101.

When you created findRoots, you created it to use 5 doubles but when you tried to use it, well you declared it in main as a return of int and as one variable of double and then trying to use it as such.

I am probably not explaining this well, which is why I suggested reading up on this matter because it is so basic that I will over complicate it with an explanation making it more of a headache for you in the long run.
Topic archived. No new replies allowed.