Thread: some book code

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM
  3. exit program
    By spentdome in forum C Programming
    Replies: 4
    Last Post: 04-22-2002, 02:08 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM