Code is not running plz check

#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <math.h>


void main()
{

float a,b,c, res;

cout << "a=" ;
cin >> a;

cout << "b= ";
cin >> b;

cout << "c= ";
cin >> c;

res= sqrt ( b*b-4*a*c);
cout <<endl;
cout << "Quadratic answer = "<< res;

}

it compile and run but after entering values it gives an error !

using borland 4.5 compiler
using borland 4.5 compiler


Don't. Get a compiler that compiles C++ in accordance with the standard.

The problem with your code is that there is no check to ensure you're not asking the code to calculate the square root of a negative number. This code fixes that, but it does need a modern compiler.



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
#include <iostream>
#include <cmath>

using std::cout;
using std::endl;
using std::cin;

int main()
{

float a,b,c, res;

cout << "a=" ;
cin >> a;

cout << "b= ";
cin >> b;

cout << "c= ";
cin >> c;

 if (4*a*c > b*b)
   {
     cout << "Aborting attempt to find square root of negative number";
   }
 else
   {
     res= std::sqrt( b*b-4*a*c);
     cout << endl;
     cout << "Quadratic answer = "<< res;
   }
 return 0;
}


Last edited on
Yes got that one. I was giving the value of 'b' less than product of 'a' and 'c'.
But would you like to tell me that which compiler is best and modern compiler?

g++ is good on lunix .
i dont have lunix, i have windows
Then g++ is still good, though most people would probably suggest VC++ (which is by no means bad, however Microsoft seems to put very little effort in further developing it).
I use Mingw on Windows and that seems fine too.
Topic archived. No new replies allowed.