#include #include #include #include using namespace std; int main(){ //////////////////////////////////////////////////// // declare variables we are going to need // lots of these, since we have 5 items and 3 sizes // initialize to zero, since we will be keeping count //clams int smallclams = 0; int medclams = 0; int largeclams = 0; //urchins int smallurchins = 0; int medurchins = 0; int largeurchins = 0; //worms int smallworms = 0; int medworms = 0; int largeworms = 0; //snails int smallsnails = 0; int medsnails = 0; int largesnails = 0; //abalones int smallabalones = 0; int medabalones = 0; int largeabalones = 0; //mussels int smallmussels = 0; int medmussels = 0; int largemussels = 0; //unknown int smallunknown = 0; int medunknown = 0; int largeunknown = 0; // need variables to hold the input for each line char observer; char item; int size; // a counter for which line we are on for error reporting int counter = 0; // need the file objects too ifstream inputfile; ofstream outputfile; // and names for the files string inputfilename; string outputfilename; //////////////////////////////////////////////////// // Give the user an idea of what is happening cout << "This program will process observation data files from" << endl; cout << "sea otter feeding observations at Elkhorn Slough." << endl << endl; //////////////////////////////////////////////////// // get the file names to use, and make sure the files open // correctly // get a name to use for opening the input file cout << "Please enter the name of the file to use for input: "; getline(cin, inputfilename); // open the input file and make sure it happened correctly // notice that we have to conver to C-style strings inputfile.open(inputfilename.c_str()); if (!inputfile){ cerr<< "Unable to open input file." << endl; exit(1); } // get a name to use for opening the output file cout << "Please enter the name of the file to use for output: "; getline(cin, outputfilename); // open the output file and make it it was ok outputfile.open(outputfilename.c_str()); if (!outputfile){ cerr<< "Unable to open output file. How odd." << endl; exit(1); } cout << endl << endl; //////////////////////////////////////////////// // while there are still data elements coming in, // sort them by item and size // each time, add to the total for that item,and size // start reading items from the file inputfile >> observer >> item >> size; counter++; while (inputfile){ switch(item){ case 'c': case 'C': switch(size){ case 1: smallclams++; break; case 2: medclams++; break; case 3: largeclams++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; case 'u': case 'U': switch(size){ case 1: smallurchins++; break; case 2: medurchins++; break; case 3: largeurchins++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; case 'w': case 'W': switch(size){ case 1: smallworms++; break; case 2: medworms++; break; case 3: largeworms++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; case 's': case 'S': switch(size){ case 1: smallsnails++; break; case 2: medsnails++; break; case 3: largesnails++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; case 'a': case 'A': switch(size){ case 1: smallabalones++; break; case 2: medabalones++; break; case 3: largeabalones++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; case 'm': case 'M': switch(size){ case 1: smallmussels++; break; case 2: medmussels++; break; case 3: largemussels++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; case '?': switch(size){ case 1: smallunknown++; break; case 2: medunknown++; break; case 3: largeunknown++; break; default: cerr << "Error in size code in line " << counter << endl; }// end switch(size) break; default: cerr << "Error in item code in line " << counter << endl; } // end switch(item) // get the next line ready inputfile >> observer >> item >> size; counter++; } // end while(inputfile) //////////////////////////// // now output things nicely to the screen cout << endl << endl; cout << " " << setw(10) << "Small" << setw(10) << "Medium" << setw(10) << "Large" << endl; cout << setw(10) << "Clams" << setw(10) << smallclams << setw(10) << medclams << setw(10) << largeclams << endl; cout << setw(10) << "Urchins" << setw(10) << smallurchins << setw(10) << medurchins << setw(10) << largeurchins << endl; cout << setw(10) << "Worms" << setw(10) << smallworms << setw(10) << medworms << setw(10) << largeworms << endl; cout << setw(10) << "Snails" << setw(10) << smallsnails << setw(10) << medsnails << setw(10) << largesnails << endl; cout << setw(10) << "Abalones" << setw(10) << smallabalones << setw(10) << medabalones << setw(10) << largeabalones << endl; cout << setw(10) << "Mussels" << setw(10) << smallmussels << setw(10) << medmussels << setw(10) << largemussels << endl; cout << setw(10) << "Unknown" << setw(10) << smallunknown << setw(10) << medunknown << setw(10) << largeunknown << endl; //////////////////////////// // and now output to the file outputfile << " " << setw(10) << "Small" << setw(10) << "Medium" << setw(10) << "Large" << endl; outputfile << setw(10) << "Clams" << setw(10) << smallclams << setw(10) << medclams << setw(10) << largeclams << endl; outputfile << setw(10) << "Urchins" << setw(10) << smallurchins << setw(10) << medurchins << setw(10) << largeurchins << endl; outputfile << setw(10) << "Worms" << setw(10) << smallworms << setw(10) << medworms << setw(10) << largeworms << endl; outputfile << setw(10) << "Snails" << setw(10) << smallsnails << setw(10) << medsnails << setw(10) << largesnails << endl; outputfile << setw(10) << "Abalones" << setw(10) << smallabalones << setw(10) << medabalones << setw(10) << largeabalones << endl; outputfile << setw(10) << "Mussels" << setw(10) << smallmussels << setw(10) << medmussels << setw(10) << largemussels << endl; outputfile << setw(10) << "Unknown" << setw(10) << smallunknown << setw(10) << medunknown << setw(10) << largeunknown << endl; }