1. ## C++ newbie :(

Hi, I just started taking C Language class and my first project is due Friday. Was gonna ask for help cos it seems my whole program is ok except i dont know why the compiler won't let me use the sqrt() function :@

Here is a "copy" of the program:
#include <iostream.h>
int main() {

int a,b,c,d,e,f,g;
cout << "*** Program to solve the ecuation Ax^2 + Bx + C = 0 ***\n"<< "Enter the value of the coefficients.\n";

//coeficientes
cout << "A=";
cin >> a;
cout << "B=";
cin >> b;
cout << "C=";
cin >> c;

cout << "*** The equation to solve is:" <<a<<"X^2 + "<<b<<"X + " <<c<<" = 0\n";

d = (b*b + 4*a*c);
e = (-(b));
f = 2*a;
g = sqrt(d);

if (d<0)
cout << "*** The equation has no real solutions.\n";

if (d>0)
cout << "*** The equation has two real solutions.\n";
cout << "The solutions are: " << -b + sqrt(d)/f <<"and" << -b - sqrt(d)/f; endl;

if (d=0)
cout << "*** The equation has only one real solution.\n";
cout << "The solution is: " << -b/f; endl;

return 0;
}

Be aware it may be full of error but heck its my first try :/
any help would be greatly appreciated

btw, the proggy im using is Microsoft Visual C++ 6.0

2. gah im such a newb

3. Add this to to the top of your program and the sqrt() should work:
Code:
`#include<math.h>`

4. ok after some alterations now it looks like this:

Code:
```#include <iostream.h>
#include <math.h>
int main() {

float a,b,c,d,e,f,g;
cout << "*** Program to solve the ecuation AX^2 + BX + C = 0 ***\n"<< "Enter the value of the coefficients.\n";

cout << "A=";
cin >> a;
cout << "B=";
cin >> b;
cout << "C=";
cin >> c;

cout << "*** The equation to solve is:" <<a<<"X^2 + "<<b<<"X + " <<c<<" = 0\n";

d = (b*b - 4*a*c);
e = (-(b));
f = 2*a;
g = e/f;

if (d=0)
cout << "*** The equation has only one real solution. \n" << "The solution is: " << g << "\n";

if (d<0)
cout << "*** The equation has no real solutions.\n";

if (d>0)
cout << "*** The equation has two real solutions.\n" << "The solutions are: " << (-(b) + sqrt(d))/f <<" and " << (-(b) - sqrt(d))/f<<"\n";

return 0;
}```
what's troubling me is that if d = 0 (a=1 b=2 c=1), nothing happens, the program just states that *** The equation to solve is 1X^2 + 2X + 1 = 0, completely ignoring my "if (d=0) command

5. You need to write "if (d==0)"

With only one equal sign you are assigning d to equal zero. Then since it equals zero the next two if statements trigger!

CODE TAGS: Check out the link at the top of the board that says "Posting code? read this first!" Code tags make your code more readable.

TITLE: Try to make your title more descriptive of your question. You should have titled your question "Square Root?" or just ()" A good title will get the attention of someone who can answer your question. The rumor is that some people don't read posts with vague titles.

[EDIT]
VERY GOOD! I see you used code tags in your next post!

7. = is the assignment operator
Perhaps what you really want to use is ==

EDIT: wow i'm slow

8. Since you are a newbie, I recommend a few things. Use more descriptive variable names. a, b, and c, are fine for the equaltion, but d, e, f, and g can be named better names. Also, you may want to add in some doubles instead of ints so that your solutions account for floating point numbers. Use brackets when you want more than one thing in an if statement. Only the line right after the if, if done without brackets, will execute with the if. for example:

Code:
```if (d>0)
cout << "*** The equation has two real solutions.\n";
cout << "The solutions are: " << -b + sqrt(d)/f <<"and" << -b - sqrt(d)/f; endl;```
the second cout will execute all the time.

also if you are going to store sqrt(d) in g, you may want to do:
Code:
```(e + g)/f
OR
(e / f) + (g / f)```
otherwise, you don't need the variables, in which you don't but if they are there, you might as well use them.

The way you have your code right now will only divide sqrt(d) by f, and not -b. In which the equation is:
-b +- sqrt(b*b - 4ac)
--------------------------
2a
*note that it is (b*b - 4ac) not +4ac

other than a few syntax errors, I hope this helps some.

edit: wow, I'm slow. okay, you can disregard this post. looks like you have fixed some of the errors.

9. alpha, i didnt quite get it when you explained the brackets after the if commands, you know, so it takes multiple lines instead of the next one.

also, what names would you suggest for d,e,f,g?

10. variable names like d, e, f and g are quite cryptic don't ya think?

11. Well, since it is a simple program, d, e, f, g; are fine, but more descriptive names are best when working with larger programs.
i.e. discriminate instead of d, negative_b, two_a, etc.

as for if statements:

say you have the following if statement:
Code:
```if(d == 0) {
std::cout << "Solution is: " << std::endl;
std::cout << "Two" << std::endl;
std::cout << "Example" << std::endl;
}```
the code in the if will only execute when d equals zero.
otherwise, if the code is like so:
Code:
```if(d == 0)
std::cout << "Solution is: " << std::endl;
std::cout << "Two" << std::endl;
std::cout << "Example" << std::endl;```
if d equals zero, all three lines will execute. if d does not equal zero, then the first line will not execute, but the next two lines will. so, if you want more than one line to execute with an if, then it is best to use brackets.