Sqrt Problem Here

I've posted where the problem in my code is; really don't know what the problem is with it. Sorry if my code is a little confusing, I made it like this first so I can see if there are any problems with it before I compress it.


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Quadratic Formula
//First build 1/31/09

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main()
{
    //making variables up in here
    string sAnswer = "null";
    long double a = 0;
    long double b = 0;
    long double c = 0;
    long double num1 = 0;
    long double num2 = 0;
    long double num3 = 0;
    long double num4 = 0;
    
    labelA: //label for my goto because im too lazy for loops
    
    cout << "I can do the quadratic formula!" << endl;
    cout << "I just need you to input the variables." << endl;
    cout << "But first I want you to tell me if you want output" << endl;
    cout << "at each calculation so you can write it down and call it yours." << endl;
    cout << "Please type in a 'y' or a 'n' to indicate if I should tell you or not." << endl;
    
    cin >> sAnswer;
    
    cout << sAnswer << endl; //this line is for testing purposes
    cout << "I'm not going to give you the output just yet. This is an alpha build!" << endl; //delete this later
    //input if statement here
    
    cout << "Please tell me the value of a." << endl;
    cin >> a;
    cout << "Please tell me the value of b." << endl;
    cin >> b;
    cout << "Please tell me the value of c." << endl;
    cin >> c;
    cout << "Alrighty I'm working on it now, buddy." << endl;
    
    //the output for each calculation is to see where the problem lays in the program
    //the error is occuring at the square root calculation
    
    num1 = b * b;
    cout << "B squared is: " << num1 << endl;
    
    num2 = a * c * -4;
    cout << "-4ac is: " << num2 << endl;
    
    num3 = num1 + num2;
    cout << "b squared plus -4ac is: " << num3 <<  endl;
    
    num1 = sqrt(num3);
    cout << "Square root of b squared plus -4ac" << num1 << endl;
    
    num2 = b * -1;
    num4 = num1;
    num3 = num2 + num1;
    num1 = a * 2;
    num3 = num3/num1;
    cout << "First result (addition): " << num3 << endl;
    num3 = num2 - num4;
    num4 = a * 2;
    num3 = num3/num4;
    cout << "Second result (subtraction): " << num3 << endl;
    cout << "Go again? y/n?" << endl;
    cin >> sAnswer;
    cout << "Haha just kidding alpha build" << endl;
    cout << "End of test" << endl;
    system("PAUSE");
    return 0;
}    

As you can see I'm going to add a goto and the checks for it but thats after I get the math working right. Thanks for the help!
If num3 is <0, you can't do sqrt().
I don't understand what's with all those steps:
1
2
3
4
5
6
7
8
if (!a)
    //function is linear
num3=b*b-4*a*c;
if (num3<0)
    //function has no roots, IIRC
num3=sqrt(num3);
minus=(-b-num3)/2*a;
plus=(-b+num3)/2*a;
Last edited on
I had the steps expanded so I can output the result of each calculation, I was going to add an If statement at the beginning that would get rid of that option and then I would have something similar to your equation in the place of my expanded steps if the user didn't want to see the result at each calculation.
Line 12 kills me. Nice.
Would it make much of a difference performance wise if I didn't assign my variables values at conception?
It's generally a good idea to assign them at creation anyway, just to make sure they have a value. Also, what helios said is basically right, if num3 < 0 then you are no real roots and that will cause some problems.
Ok I'll include that in the next iteration of it.
Anything else wrong besides that?
Topic archived. No new replies allowed.