-
some book code
I am using a book assigned for a class which seems to mix C style and C++ style code and calls it C++ , for ex. the following
Code:
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
struct point { float x; float y; };
float randFloat()
{ return 1.0*rand()/RAND_MAX; }
int main(int argc, char *argv[])
{ float d = atof(argv[2]);
int i, cnt = 0, N = atoi(argv[1]);
point *a = new point[N];
for (i = 0; i < N; i++)
{ a[i].x = randFloat(); a[i].y = randFloat(); }
for (i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
if (distance(a[i], a[j]) < d) cnt++;
cout << cnt << " pairs within " << d << endl;
}
does this "iostream.h" come with any compiler standard? I get file not found, only the regular <iostream> exists on my computer, using gcc ( i think )
If I change it to <iostream> , as I *think* it should be, then of course I get the missing namespace error for cout ... ok then if I add the using namespace std statement that he might (?) have left out ... then I get a conflict with the distance function already being defined in std...
My question is, is it possible that this code actually compiled on SOME major compiler that I don't know of?
-
iostream.h was used before C++ got standardised. But that was in 1998.
So yeah, it's obsolete now.
> I am using a book assigned for a class which seems to mix C style and C++ style code and calls it C++
I call it road-kill.
There are unfortunately just as many bad books as there are bad teachers.
The first 3 includes should be
Code:
#include <cmath>
#include <iostream>
#include <cstdlib>
Pre-ISO C++ headers lose the .h
ISO-C headers lose the .h, and begin with a 'c'
Finally for ease of porting old code (or laziness in writing new code), you add
Code:
using namespace std;