Math equation coding help!

For my programming class, I am required to loop prompts for user input for the following equation:
x= -3b plus or minus sqrt of (650-7c^2)(end sqrt)/ a-4c
I need to prompt the user for a. If the user enters -999, the program must terminate without prompting for b and c.
If the user doesn't enter -999, i need to prompt for b and c on seperate lines and then print both values of x.
Other conditions are:
clear the screen at the beginning off the run
display two results properly labeled or a descriptive error message if the input is invalid (such as a negative in a sqrt and dividing by zero)
and repeat until -999 is entered for a.

Here is what i have so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <math.h>
#include <cstdlib>
int main ( )
{
    system("CLS");
    float a;
    float b;
    float c;
    
    std::cout << "Enter value a: ";
    std::cin >> a;
    if (a == -999){
        exit (0);}
    while (a !=-999.0)
        std::cout<< "Enter value b: ";
    std::cin >> b;
    std::cout << "Enter value c: ";
    std::cin >> c;
    


Please correct me if i'm wrong because this is very difficult for me. I appreciate all answers!
<cmath> .. math.h is a C not c++ header.

use double instead of float for most math. float has a terribly small number of bits for precision and suffers a lot of problems in complicated math (its ok here, but best practice going forward is use double).

the math function variable =s qrt(x); will be needed. Multiply is * ^ is the logical XOR command (exclusive or)(don't worry about this right now). pow(n, power) is the cmath power function BUT it is better to just multiply for small powers (N*N is better than pow(N,2)).

exit (0) is used for failures as an emergency stop all processing command. Use return (0) here.

it looks good so far, now you just need to do something like

double tmp = (b*b - 4*a*c);
if(tmp < 0)
...something else
tmp = sqrt(tmp);
and similar statements that cook up the answer, checking for problems along the way...

you have a solid start, keep at it and ask if you get stuck.

Looks like the same assignment has already been posted (by someone else):

http://www.cplusplus.com/forum/beginner/221501/

Hopefully what's there already will serve as a decent starting point.
Last edited on
This is what i have for this assignment, but it isn't working, no matter how i follow the solution above. I enter a b and c, but nothing is displayed after it. can anyone 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
#include <iostream>
#include <math.h>
using namespace std;
int main ( )
{
    float a;
    float b;
    float c;
    float x1;
    float x2;
    cout << "Enter value a: ";
    while (a != -999)
    {
        cin >> a;
        cout<< "Enter value b: ";
        cin >> b;
        cout << "Enter value c: ";
        cin >> c;
        if ((a - 7.0 * c) == 0.0)
            cout << "Cannot divide by zero" << endl;
        else if (650.0 -7.0 * c * c < 0.0)
            cout << "Radical cannot contain a negative. " << endl;
        else if (cin >> a >> b >> c)
            x1 = (-3.0 * b) + sqrt(650.0 - 7.0 * c * c) / (a - 4.0 * c);
            x2 = (-3.0 * b) - sqrt(650.0 - 7.0 * c * c) / (a -4.0 * c);
        cout << x1 << '\n'<< x2 << '\n';
        cout << "Enter a value: a ";
    }
}
Last edited on
closed account (48T7M4Gy)
DUPLICATE POSTS ARE BAD SARAH STOP DOING IT!
@kemort
sorry. I won't do this again. I am new here and i got confused with the posting process. Thank you for letting me know!
closed account (48T7M4Gy)
ok sarah, apology accepted, we move on. come back if you need any more help etc
Topic archived. No new replies allowed.