// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24,29,30 // lab8 #include #include #include #include #include using namespace std; /* This program implements a function that takes an integer and an ostream * and prints out the value and the digits to the specified ostream. * It also reads integers from a user-specified file and uses the above * function to print out the digits to a specific location. * The user should be able to choose to print to the screen (cout) or a file. * If they choose to print to a file, they are able to specify the name of the * file to print to. */ // the length of the largest number (e.g 1020 => 4) const int MAX_NUMBER = 4; void getInStream(ifstream &); void getOutStream(ofstream &); int getOutputMethod(); void printC(int number, ostream &allout); void main () { // the input filestream & the number read at each line of the file ifstream infilestream; int number; // the number read from a line string outfile; // the name of the output file. int outmethod; ofstream outstream; cout << "Number Splitting v2 by Demetris Zeinalipour - Lab 8\n\n"; outmethod = getOutputMethod(); // this function does all the work related to opening the in file. getInStream(infilestream); if (outmethod == 1) { // open the file getOutStream(outstream); } // repeat this loop until you reach the end of the input file which while (!infilestream.eof()) { // read integer from the file infilestream >> number; // print out the number read from the input file if (outmethod == 1) { // File printC(number, outstream); } else if (outmethod == 2) { // Screen printC(number, cout); } } // close any opened files infilestream.close(); outstream.close(); } /* this function takes by reference an ifstream. It tries * to connect to the given file. If this operation * becomes sucessful it quits. */ void getInStream(ifstream& infilestream) { string filename; // repeat the loop until the user enters the correct filename while (true) { /* get the filename */ cout << "Please enter the name of the INPUT file: " ; cin >> filename; cout << "\nOpening \'" << filename << "\'..."; /* open file in read mode */ infilestream.open(filename.c_str()); // if the file was opened succesful then break this loop // else display error message and let him try again if (infilestream) { cout << "done\n"; break; } else { cout << "failed\n"; cout << "File could not be opened\n" << endl; } } // while } /* this function is sililar to getInStream, with the difference * that it tries to connect to the given output file. * If this operation becomes sucessful it quits. */ void getOutStream(ofstream& outfilestream) { string filename; do { cout << "Please enter the name of the OUTPUT file: "; cin >> filename; cout << "\nOpening outfile \'" << filename << "\'..."; /* open outfile */ outfilestream.open(filename.c_str()); if (!outfilestream) { cout << "failed!\n"; cout << "File could not be opened!\n" << endl; } else { cout << "done!\n"; break; } } while (true); } /* This method allows you to choose between the 2 available * output methods. It thens returns * @ return : 1 => use that file for output. * : 2 => use screen for output. */ int getOutputMethod() { int method; // select output method do { cout << "Please select Output Method (1=File, 2=Screen) :"; cin >> method; // error checking if (method!=1 && method!=2) { cout << "Error - Only 1-2 are accepted Values!\n\n"; } else { break; } } while (true); return method; } /* Visit http://www.cplusplus.com/ref/iostream/ostream/index.html for * more information on the Stream Hierarchy */ void printC(int number, ostream &allout) { int power; // variable to be used to define digit int digit; // the digit of a number. Each number will have 4 digits allout << setiosflags(ios::left); allout << setw(7) << number; // split and print each digit of this number for (int i=MAX_NUMBER-1; i>=0; i--) { power = (int)pow(10,i); digit = number / power; number %= power; allout << setw(7) << digit; } allout << endl; }