Putting the answer of a equation into command in

I am teaching myself C++, the problem I have to answer is
In Mathematics, the quantity of b^2-4ac is called the "discriminate." Write a program that asks the user for values of a, b , and c and displays no roots if the discriminant is negative, one root if the discriminant is zero and two roots if the discriminate is positive.

My code looks like this so far.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
double a,b,c,d;
cout<<"Enter the Value for a:"<<endl;
cin>>a;
cout<<"Enter the Value for b:"<<endl;
cin>>b;
cout<<"Enter the Value for c:"<<endl;
cin>>c;

cout<<(b*b-4*a*c)<<endl;




if ( < 0)
cout<<"No Roots"<<endl;

if ( = 0)
cout<<"One Root"<<endl;

if ( > 0)
cout<<"Two Roots"<<endl;




system("PAUSE");
return 0;
}

I need to know how to decide a variable by an equation. I hope that makes sense
You should put the result of that equation in another variable. Then you can check to see if that variable is positive/negative.
I don't understand what you mean
could you show me an example?
What he means is..

get values for a b c and then declare a new variable to hold the answer.

i.e.

instead of cout << (B*B-4*A*C) << endl;

do..
1
2
3
4
5
double answer = (B*B-4*A*C);
cout << answer << endl;

if (answer < 0)
cout << "No roots"..

etc.
(Note: use <cstdlib> for C++, not <stdlib.h>)

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 <cstdlib> //(Header changed)

using namespace std;

int main(int argc, char *argv[])
{
    double a, b, c, d;
    cout << "Enter the Value for a:" << endl;
    cin >> a;
    cout << "Enter the Value for b:" << endl;
    cin >> b;
    cout << "Enter the Value for c:" << endl;
    cin >> c;

    //Your fine up to here...
    d = b*b - 4*a*c; //Store the discriminate into variable d
    cout << d << endl; //(Line changed)

    if ( d < 0 ) //I.e. "if the discriminate is less then 0"
        cout << "No Roots" << endl;

    if ( d == 0) //(If statement changed)
        cout << "One Root" << endl;

    if ( d > 0 ) //(If statement changed)
        cout << "Two Roots" << endl;

    system("PAUSE"); //Why is this line here? Do you need it?
    return 0;
}
Last edited on
if ( d = 0) //(If statement changed)

You probably meant ==, not =
OK thanks now i understand.

the system ("Pause");
is what you use to keep the program from closing after its completed the last line of code, as it is a console application.
I'm pretty sure Mathhead knows why you were using a system call. It's just that it's a really BAD idea: http://cplusplus.com/forum/beginner/1988/
Topic archived. No new replies allowed.