/* Random file generation Written by Bhaskar Bhattacharya Copyright 2016 Code used in lecture 4 */ #include #include #include #include using namespace std; #define SQUARE "square" #define RECTANGLE "rect" #define CIRCLE "circle" fstream fp; //Global file pointer variable can be accessed by everything after it void write_square(){ fp << SQUARE << " " << rand() % 100 << endl; } void write_rectangle(){ fp << RECTANGLE << " " << rand() % 100 << " " << rand() % 100 << endl; } void write_circle(){ fp << CIRCLE << " " << rand() % 100 << endl; } int main(){ // Open file fp.open("shapes.txt", ios::out); //Make sure the file has opened if (!fp){ cerr << "Oops couldn't open the file" << endl; return -1; } //Put the seed value srand(time(NULL)); //Start the loop for 100 random numbers for (int i = 0; i < 100; i++){ // Create a random number b/w 0 and 2 (sq, rect, circ) int idx = rand() % 3; //Switch accordingly and write switch (idx){ case(0) : write_square(); break; case(1) : write_rectangle(); break; case(2) : write_circle(); break; //Putting a default is good coding practice and should be used always default: break; } } //Close the file fp.close(); //Return with 0 return 0; }