# Thread: why doesn't this work?

1. ## why doesn't this work?

this is my 1st attempt with pointers and I can't get this program to tell if there not real or to produce roots. Any help would be great... cuz I know its not 0.000 0.000 thanks

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cmath>

using namespace std;

void readabc(double *a, double *b, double *c, char *real);

void quadroots(double *a,double *b,double *c,double *root1,double *root2, char *real);

int main()
{
double a, b, c, root1, root2;
char real;
quadroots(&a, &b, &c, &root1, &root2, &real);
real = b*b-4*a*c<0;
real==1;

root1=(-b+sqrt(b*b-4*a*c)/(2*a));
root2=(-b-sqrt(b*b-4*a*c)/(2*a));
return 0;
}

void readabc(double *a, double *b, double *c,char *real)
{
cout << "Enter a,b,c for a quad: " << endl;
cin >> *a >> *b >> *c;
return;
}

void quadroots(double *a,double *b,double *c,double *root1,double *root2, char *real)
{
if (!*real)
{
printf("roots are not real\n");
}
else
{
printf ("roots are %lf %lf\n", *root1, *root2);
}//check for real roots and calculate root1 and root2
}```

2. on line 15 you define 'char real;'

line 35: 'if (!*real)'
breakdown, real is a pointer to line 15.
*real, returns the value pointed to by real.
! <value>

if that value is 0, it will be true, else 1-255 will return false.

You never set the value of real, only the pointer to it.

Line 19 should produce a compiler warning,

Line 16 - 18 are not in the proper logic order for the math your trying to use.

and you might have un-used includes

3. Use std::cout for output instead of printf. There is no need for any type modifiers, just output it as you would any text.
So
printf ("roots are %lf %lf\n", *root1, *root2);
should be
std::cout << "roots are " << *root1 << *root2 << std::endl;

4. ok been working on it and now I just can't get it to give the correct roots.... can anyone help me?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cmath>

using namespace std;

void readabc(double *a, double *b, double *c, char *real);

void quadroots(double a,double b,double c,double *root1,double *root2, char *real);

int main()
{

double a, b, c, root1, root2;
char real;
quadroots(a, b, c, &root1, &root2, &real);

return 0;
}

void readabc(double *a, double *b, double *c,char *real)
{
cout << "Enter a,b,c for a quad: " << endl;

cin >> *a >> *b >> *c;
return;
}

void quadroots(double a,double b,double c,double *root1,double *root2, char *real)
{

if (b*b-4*a*c < 0)
{
printf("roots are not real\n");
return;
}

else
{
*root1=(-b+sqrt(b*b-4*a*c)/(2*a));
*root2=(-b-sqrt(b*b-4*a*c)/(2*a));
printf ("roots are %lf %lf\n", *root1, *root2);
}//check for real roots and calculate root1 and root2
return;
}```

5. ok I got it to work better but it is acting funny with 0 0 1 when it should say "Sorry the roots are not real!!" anyone know why it does that and how I can fix that?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cmath>

using namespace std;

void readabc(double *a, double *b, double *c, char *real);

void quadroots(double a,double b,double c,double *root1,double *root2, char *real);

int main()
{

double a, b, c, root1, root2;
char real;
quadroots(a, b, c, &root1, &root2, &real);

return 0;
}

void readabc(double *a, double *b, double *c,char *real)
{
cout << "Enter a,b,c for your quadratic you need to calculate:" << endl;
cout << "(note:you must type a enter b enter c enter) " << endl;
cin >> *a >> *b >> *c;
return;
}

void quadroots(double a,double b,double c,double *root1,double *root2, char *real)
{
double realnum = b*b-4*a*c;
if (realnum < 0)
{
printf("Sorry the roots are not real!!\n");
*real = 0;
return;
}

else
{
*real= 1;
*root1=(-b+sqrt(realnum))/(2*a);
*root2=(-b-sqrt(realnum))/(2*a);
printf ("roots are %lf %lf\n", *root1, *root2);
}//check for real roots and calculate root1 and root2
return;
}```

6. What result were you expecting to get when dividing by zero? You tend to need to avoid doing that.

7. Why have you not replaced the printfs?