problems with an equation solver program

Hi i am just starting out with c++ and i have a problem, i tried to make a program that solves second degree equtations of this form: ax^2+bx+c=0.
and here is the code:
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
#include <iostream>
#include <cmath>
using namespace std;
int main() {
     double a,b,c,delta,x1,x2,x;     //declaring variables
     cout << "This program solve the equation: ax^2 + bx + c=0"<<endl<<"Enter coefficient a:";
     cin >>a;
     cout << endl<<"Enter coefficient b:";
     cin >> b;
     cout << endl<<"Enter contstant c:";
     cin >> c;
     delta = b*b-4*a*c;
     cout << "delta = "<<delta<<endl;

     if (delta==0)
         x = (-b)/(2*a);
         cout<<"There is one solution: x = "<<x<<endl;
     if (delta > 0)
         x1 = (-b - sqrt (delta) )/(2*a);
         cout<<"First solution: x1 = "<<x1<<endl;
         x2 = (-b + sqrt (delta) )/(2*a);
         cout <<"Second solution: x2 = "<<x2<<endl;
     if (delta < 0)
         cout<<"There is no real solution, there is two complex solutions.";
     return 0;
}

when deta=0 it gives this output:

This program solve the equation: ax^2 + bx + c=0
Enter coefficient a:1

Enter coefficient b:2

Enter contstant c:1
delta = 0
There is one solution: x = -1
First solution: x1 = 1.91474e-307
Second solution: x2 = -1

i don't want the last 2 lines to appear.
and when delta > 0 the program shows this:

This program solve the equation: ax^2 + bx + c=0
Enter coefficient a:2

Enter coefficient b:5

Enter contstant c:2
delta = 9
There is one solution: x = nan
First solution: x1 = -2
Second solution: x2 = -0.5

i dont want "There is one solution: x = nan" to be shown.

while if delta<0 i have:

This program solve the equation: ax^2 + bx + c=0
Enter coefficient a:6

Enter coefficient b:2

Enter contstant c:5
delta = -116
There is one solution: x = nan
First solution: x1 = 1.91474e-307
Second solution: x2 = nan
There is no real solution, there is two complex solutions.


help and thanks in advance.
Braces. You're missing them. They look like this { and this }

1
2
3
if (delta==0)
         x = (-b)/(2*a);  THIS IS THE ONLY STATEMENT AFFECTED BY "if"
         cout<<"There is one solution: x = "<<x<<endl; THIS WILL ALWAYS BE EXECUTED.



In this one, both statements are now part of the "if" block
1
2
3
if (delta==0)
{  x = (-b)/(2*a);
    cout<<"There is one solution: x = "<<x<<endl;}

thanks
Topic archived. No new replies allowed.