Math Equation coding help again!

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.

I am receiving errors for lines 7 and 24 saying that at 7 the variable a will be uninitialized when used with main ( ) and that at 24 x1 will be uninitialized in general.

There is a thread with part of a solution, but it isn't working for me- maybe because I am using Xcode on mac instead of a windows program. This is what I have:
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 <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 (c > 0.0)
            cout << "Radical cannot contain a negative";
        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 ";
    }
}
closed account (48T7M4Gy)
Your program as written runs on Xcode on a mac so there are no problems there - I wouldn't have expected any as Xcode is as good as any IDE if not better.

I've done a small amount of tidying up including use of whitespace, initializing variables, removing duplication and rationalising the while loop. Look carefully at the changes I made. Your line 25 didn't make sense to me but you might have a special reason for asking for a,b and c twice/agin so you need to think carefully about it before accepting what I've done.

I haven't checked that you are getting the required result in terms of the original question.

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
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>

using namespace std;

int main ( )
{
    float a = 0.0; // <--- iniialize all variables
    float b = 0.0;
    float c = 0.0;
    float x1 = 0.0;
    float x2 = 0.0;
    
    while (a != -999)
    {
        cout << "Enter value a: ";
        cin >> a;
        
        cout<< "Enter value b: ";
        cin >> b;
        
        cout << "Enter value c: ";
        cin >> c;
        
        if (c > 0.0)
            cout << "Radical cannot contain a negative\n";
        
        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
        {
            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';
    }
    
    return 0;
}
@sarahelizabeth

For line 6, add '= -999;' after 'float a' to initialize it for the while loop starting at line 12.

I think you meant to use < instead of > on line 19, since line 20 is stating "Radical cannot contain a negative".

Line 25 is requesting inputs for a, b and c, BUT is located in an else if. Not good, IMO.

You could try 'else if ( a>0.0 && b>0.0 && c>0.0)'. But you should enclose lines 26, 27 and 28, in parenthesis,
1
2
3
4
5
{
 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';
}

otherwise only line 26 will not execute if line 25 is not met, and the rest of the program will, causing an error.
closed account (48T7M4Gy)
DUPLICATED POSTS SARAH ARE BAD
Q: In what situation does this program print something?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main ( )
{
  float foo;
  if ( foo != -999 )
  {
    std::cout << "Hello\n";
  }
  return 0;
}

A: Nobody knows. Why? The value of foo has not been set.

Tip: a conditional expression can have more than one subexpression.
WHILE ( can read foo AND foo is not -999 )



If the user enters -999, the program must terminate without prompting for b and c.
1
2
3
4
5
        cin >> a;
        cout<< "Enter value b: ";
        cin >> b;
        cout << "Enter value c: ";
        cin >> c;

Whoa! You got a from the user, but you ask for b and c no matter what value the a has.

1
2
3
4
5
6
7
    float a;
    float c;
   // code
    while (a != -999) // you compare floats
    {
      // code
        if ((a - 7.0 * c) == 0.0) // you compare doubles 

Equality of two floating point values is not trivial. Endless pages have been written on the subject.
Who says that the a, b, c must be floats? Can they be int? The division must be done with floats though.


You have to compute
1
2
r + sqrt( s ) / t
r - sqrt( s ) / t

... and you do that, but you do compute the r, s, t multiple times. Compute once, store result, and then use the result.


Xcode is not affecting anything.


There is one requirement on the task that is not related to C++:
clear the screen at the beginning off the run

That does not depend on Xcode. It does not depend on OS either. The required code does depend on the terminal that is used when running the program.
IMHO C++ beginner homework should never have such stuff.
@kemort

your code still asks for b and c when I enter -999.
Thank you to all who helped! My program is working how i want it to(with a few tweaks of my own), besides the clear screen, and i am going to present it to my professor shortly! I appreciate all the help!
Topic archived. No new replies allowed.