/* Computer Science & Programming Jason M. Snouffer Wednesday, October 16, 2002 Lab 7 Problem 2 This program reads an integer from a file, and prints out a times table from 1 to 12 times that number. Test Suite: Input Output 1 1*1 = 1 1*2 = 2 1*3 = 3 1*4 = 4 1*5 = 5 1*6 = 6 1*7 = 7 1*8 = 8 1*9 = 9 1*10 = 10 1*11 = 11 1*12 = 12 -5 -5*1 = -5 -5*2 = -10 -5*3 = -15 -5*4 = -20 -5*5 = -25 -5*6 = -30 -5*7 = -35 -5*8 = -40 -5*9 = -45 -5*10 = -50 -5*11 = -55 -5*12 = -60 7 7*1 = 7 7*2 = 14 7*3 = 21 7*4 = 28 7*5 = 35 7*6 = 42 7*7 = 49 7*8 = 56 7*9 = 63 7*10 = 70 7*11 = 77 7*12 = 84 30 1*1 = 30 1*2 = 60 1*3 = 90 1*4 = 120 1*5 = 150 1*6 = 180 1*7 = 210 1*8 = 240 1*9 = 270 1*10 = 300 1*11 = 330 1*12 = 360 All of the sample inputs within this test suite were tested and performed as expected. */ #include #include #include using namespace std; /* This program is a simple demonstration of how to read and write * files in C++ using input and output streams. * * For now, don't worry about how this stuff works. All you need to * know, is that if you copy and paste everything except main in this * file, that you can write a main that uses these functions. * */ //----------------- open_output_file_stream --------------------- /* * This function returns an object of type ofstream that gives the * user a way to write to the file filename. If filename * is not a valid file, then this function halts the program using * the exit command. * * * pre: filename should be the name of a file in the same * folder as the program itself. * */ ofstream open_output_file_stream(char *filename); //----------------- open_input_file_stream --------------------- /* * This function returns an object of type ifstream that gives the * user a way to read from the file filename. If filename * is not a valid file, then this function halts the program using * the exit command. * * * pre: filename should be the name of a file in the same * folder as the program itself. * */ ifstream open_input_file_stream(char *filename); int main() { // special cin and cout streams are instances of a special type // I used the variable names panda_bear and sea_lion just so you'd // know that they didn't have to be anything special. These // are BAD variable names It would be much better to call // panda_bear in_stream and sea_lion out_stream ifstream input_stream = open_input_file_stream ("../Lab 7/number_in.txt"); int num; input_stream>>num; int i; for(i = 1; i <= 12; i++) { cout<open(filename); if (out->fail()) { cout << "ERROR: Couldn't open " << filename << ", halting program!\n"; exit (EXIT_FAILURE); } else { cout << "Successfully opened " << filename << "!" << endl; } // if we get to here, we successfully opened the file for writing return (*out); } //----------------- open_input_file_stream --------------------- /* * This function returns an object of type ifstream that gives the * user a way to read from the file filename. If filename * is not a valid file, then this function halts the program using * the exit command. * * * pre: filename should be the name of a file in the same * folder as the program itself. * */ ifstream open_input_file_stream(char *filename) { ifstream *in = new (ifstream); in->open(filename); if (in->fail()) { cout << "ERROR: Couldn't open " << filename << ", halting program!\n"; exit (EXIT_FAILURE); } else { cout << "Successfully opened " << filename << "!" << endl; } // if we get to here, we successfully opened the file for writing return (*in); }