/*This is Lab 3 assist program. It does not do Lab 3 specs BUT it will help ALOT!!! Not many comments shown in order to require students to think Use an input file with 6 columns of numbers as required in Lab 3 Write up. Place file in correct path. Name it lab3_data.txt */ #include #include // For the files!!!! #include // For manipulators & formatting options using std::cin; using std::cout; using std::endl; using std::setw; using std::ios; using std::ifstream; using std::ofstream; const int MAX = 50; int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[]); void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], int counter); void PrintTotalsAndSummary(ofstream& out, int totalRecords); int main() { int pick[MAX]; int drop[MAX]; int psgr[MAX]; float dist[MAX]; float fare[MAX]; float toll[MAX]; int record_counter(0); ifstream inFile; // Notice how this automatically opens the file ofstream outFile; outFile.open("C:\\TEMP1\\lab3_Report.txt"); char file = 'a'; //sets variable to change input file cout << "Please choose a file to open. Press 's' for small.txt, or press 'l' for large.txt: "; //Prompts user for nput file choice cin >> file; //Reads in users choice for input file cout << endl; //Prints new line switch (file) { //switch statement for variable file case 's': //case when user inputs s for small inFile.open("C:\\TEMP1\\small.txt"); //open small.txt break; //end case case 'l': //case when user inputs l for large inFile.open("C:\\TEMP1\\large.txt"); //open large.txt break; //end case } if (inFile.is_open()) //If the infile is open { record_counter = ReadData(inFile, outFile, pick, drop, psgr, dist, fare, toll); //Do the ReadData function and set it as new variable record_counter inFile.close(); //Close the input file if (outFile.is_open()) //If the output file is open { WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, record_counter); //Do the WriteOutptFile function PrintTotalsAndSummary(outFile, record_counter); //Do the PrintTotalsAndSummary function outFile.close(); //Close the output file } else //Otherwise, do this { cout << "Trouble Opening Output File"; //Print text cout << "\n\n\t\t ** About to EXIT NOW! ** "; //Print text } } else //Otherwise, do this { cout << "Trouble Opening Input File"; //Print text cout << "\n\n\t\t ** About to EXIT NOW! ** "; //Print text } return 0; //Return empty value and end stream } int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[]) //Function to ReadData from the input file { int counter = 0; //Set variable counter inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; //Priming Read while (!inFile.eof()) //While not the end of the in file { cout << setiosflags(ios::left) << setw(5) << pick[counter] << resetiosflags(ios::left) << setw(10) << drop[counter] << resetiosflags(ios::left) << setw(12) << psgr[counter] << resetiosflags(ios::left) << setw(14) << dist[counter] << resetiosflags(ios::left) << setw(14) << fare[counter] << resetiosflags(ios::left) << setw(14) << toll[counter] << endl; //Print infile data as a table counter++; //Add 1 to counter inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; } return counter; //Return variable counter } void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], int counter) //Function to write the output file { outFile << " Here is the Output File" << endl; //Print text to the out file for (int r = 0; r <= counter - 1; r++) { outFile << setiosflags(ios::left) << setw(5) << pick[r] << resetiosflags(ios::left) << setw(10) << drop[r] << resetiosflags(ios::left) << setw(12) << psgr[r] << resetiosflags(ios::left) << setw(14) << dist[r] << resetiosflags(ios::left) << setw(14) << fare[r] << resetiosflags(ios::left) << setw(14) << toll[r] << endl; } } void PrintTotalsAndSummary(ofstream& outFile, int totalRecords) { // To screen cout << "\n\n\t** Total Records: " << totalRecords << " **\n" << "\t\t The End \n"; // To file outFile << "\n\n\t** Total Records: " << totalRecords << " **\n" << "\t\t The End \n"; }