/* Copyright: 1999, All rights reserved. Program: stdlib Version: 3.1 Created: 9/26/99 Revised: 10/20/99 File: stdlib.cpp Programmer: C. S. Tritt, Ph.D. Course: CS-150 Assignment: Example Compiler: Microsoft Visual C++ 6.0 Target: Win32 Description: This program tests functions that should be declared in the and files. It also tests the assert mechanism for error handling and demonstrates the use of command line arguments. It contains a mix of C/C++ and some low level code due to the use of some old C style features. Revisions: There have been no major revisions of this program. Constants and Variables: See code for constant and variable descriptions. */ #define DEBUG // NDEBUG disables assert(). // Included files. #include // Required for srand(), rand() & system(). #include // Required for stream output. #include // Required for "endl." #include // Required for time(). #include // Required for assert(). #include // Required for char* to C++ string conversion and output. using namespace std; int main(int argc, char *argv[]) { // Display command line arguments. int i; for(i = 0; i < argc; i++) // Loop through argument array. { string arg = argv[i]; // Convert C "string" to C++ string. cout << "Argument " << i << " is "<< arg << endl; } // Get the current time in seconds since midnight 1/1/70. time_t when; // A time_t argument is required by time. int now = time(&when); // Now is current time. When is not used. cout << "The returned time was " << now << endl; // Seed random number generator with mod time and get two random numbers. /* The MSVC++ 6.0 random number genertor does not seem to be particularly good so I'll beef it up a bit by burning some calls to it. */ srand(now); int count = rand() % 73; for (i = 0; i < count; i++) rand(); int r1 = rand(); count = rand() % 67; for (i = 0; i < count; i++) rand(); int r2 = rand(); cout << "Random numbers should be between 0 and " << RAND_MAX << endl; cout << "Random numbers are: " << r1 << " and " << r2 << endl; // Tell user if NDEBUG is defined and do assert. #if defined(NDEBUG) cout << "NDEBUG defined. Assert disabled,\n"; #else cout << "NDEBUG not defined. Assert enabled.\n"; #endif cout << "About to assert(r1 < r2)\n"; assert(r1 < r2); cout << "Assertion must have been true or disabled.\n"; // Perform a system call. cout << "Now try a system call (in this case \"dir\").\n"; system("dir"); // Everything worked, but exit(0) causes a warning. return 0; }