output problem with cout <<

when I compile this program:

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
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
#include "math.h"
using namespace std;

double a,b,c,Discriminant,thingy;
int main()
{
    cout << "Please enter the first coefficient:";
    cin >> a;
    cout << "Please enter the second coefficient:";
    cin >> b;
    cout << "Please enter the third coefficient:";
    cin >> c;
    if (Discriminant < 0)
      {
                       cout << "\nNo Solutions.\n";
                       return 0;
                       
      }
      if (Discriminant == 0)
                       cout << "\nOne solution\n";
    Discriminant = ((0-b) + sqrt(Discriminant))/2;
    thingy = ((0-b) - sqrt(Discriminant))/2;
    thingy = 1.16315432;
    cout << "The answers are " << Discriminant << " and " << thingy;
    system("PAUSE");
    return 0;
}

I get this output:

Please enter the first coefficient:XXX
Please enter the second coefficient:XXX
Please enter the third coefficient:XXX
the answers are XXXX and X.#INDPress any key to continue...
I see.
I don't.

How is this a "problem with cout <<"?
Using 1; 1; 4 your code returns:

One solution
sh: PAUSE: not found
The answers are -0.5 and 1.16315

edit: however the solution is incorrect!
Last edited on
Maybe I'm not paying enough attention but where is the value of Discriminant declared? Not the variable I can see it up there as a global but it looks like you're leaving Discriminant up to What ever left overs are in that memeory space.

BTW this might explain the strange output.
Last edited on
also replace system("PAUSE") with something like:
cin.ignore();
cin.get();
firedraco wrote:
I see.


I lol'd
Try initializing all your variables, then it'll be easier to see where you went wrong (theoretically at least). Also, Computergeek01 is correct, I can't see where you tell the computer what Discriminant is equal to. One more mistake I see is that you declare thingy to be a value that isn't one of the solutions.
don't give up -- ask more questions!
initialise the value of discriminant and thingy
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
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
#include "math.h"
using namespace std;

double a,b,c,Discriminant,thingy;
int main()
{
    poop:
    cout << "Please enter the first coefficient:";
    cin >> a;
    cout << "\nPlease enter the second coefficient:";
    cin >> b;
    cout << "\nPlease enter the third coefficient:";
    cin >> c;
    Discriminant = (b*b) - 4*a*c;
    if (Discriminant < 0)
      {
                       cout << "\nNo Solutions.\n";
                       goto poop;
                       
      }
      if (Discriminant == 0)
                       cout << "\nOne solution\n";
    Discriminant = ((0-b) + sqrt(Discriminant))/2;
    thingy = ((0-b) - sqrt(Discriminant))/2;
    cout << "The answers are " << Discriminant << " and " << thingy;
    system("PAUSE");
    return 0;
}

this code now outputs at the end...
the solutions are XXX and XXXPress any key to continue...

Why would it output that if I don't tell it to output "Press any key to continue..."
thanks for the help with the #IND problem....
the system ("PAUSE") statement will prompt the output "Press any key to continue,,," reread cmccmc's post on alternative statements.


also system("pause>nul"); will output a pause without the prompt
Last edited on
you say the code outputs
the solutions are...

but your cout says cout << "The answers are " <<

So either the code or the output is lying.

If you get errors when compiling perhaps your IDE is grabbing an old exe to run.
Topic archived. No new replies allowed.