// On my honor: // // - I have not discussed the C++ language code in my program with // anyone other than my instructor or the teaching assistants // assigned to this course. // // - I have not used C++ language code obtained from another student, // or any other unauthorized source, either modified or unmodified. // // - If any C++ language code or documentation used in my program // was obtained from another source, such as a text book or course // notes, that has been clearly noted with a proper citation in // the comments of my program. // // - I have not designed this program in such a way as to defeat or // interfere with the normal operation of the Curator Server. // // // Mapmiles.cpp: A Simple Map Mileage Program // // You should substitute the correct information in the following // lines: // // Programmers: N.D., C.W. & M.H. // Modifier: Dwight Barnette // Course: CS1044 // Compiler: Visual C++ version 6.0 // Platform: AMD FX-53 / Windows XP Pro Build 2600 SP 2 // Date: Aug. 4, 2006 // //****************************************************************** // Walk program // This program computes the mileage (rounded to tenths of a mile) // for distances between points in a city, given // the measurements on a map with the scale the number of points // and the map distance between each. //****************************************************************** // // Purpose of the program: // // This program reads information about a a map and an arbitrary number of // distances between locations from an input file named "inmap.dat": // // Line 1: // - number of locations // - map scale factor (miles per inch) // Line x: (all following lines) // - distance between locations (inches) // // and computes the correct mileage for: // // - map distances // - Total distance traveled. // // It then prints out a labeled table of results, showing for // each location distance the actual mileage to reach it. // In addition, the total distance to be traveled is computed and // printed. // // This is the #include section. These statements tell the compiler // to read and use variables and functions declared in the specified // files. // #include #include // For setprecision() using namespace std; // Every C++ program must have a function named main. This is the // beginning of the definition of the main function for this // simple program. In this case the whole program consists of just // the main function --- usually programs involve more than one // function. // int main( ) { // This section is where named constants are declared and given // their values. We could just use the numbers themselves in the // code that follows, but the names make the code more readable; // the name DEFSCALE carries meaning where the number 0.25 doesn't. // const double DEFSCALE = 0.25; // Default map scale (miles per inch) // This section is where the variables used in main() are declared. // Each variable has a type (integer, float, etc) but no particular // value at this point. The variable names like the constant names // used above are supposed to be meaningful, and each declared // variable is also given a short comment explaining what it will // be used for. // ifstream inMap; // input file stream ofstream outMap; // output file stream double mapScale, // Map scaling factor (miles per inch) distance, // Map distance between locations (inches) totMiles, // Total of rounded mileages miles; // An individual rounded mileage int numPoints, // Number of locations on map to visit visited; // Number of locations' distances converted // This is the initialization section of main(). We must make sure that // every variable is given a value before it is used in a calculation // or printed. Some variables get their values by being read from the // input file, or assigned so they don't need to be initialized here. visited = 0; // Initialize counters and running totals totMiles = 0.0; // to zero. mapScale = DEFSCALE; // Initialize map scale factor to default value // The following four statements prepare the input and output files // for use. Don't worry too much about what's going on here just yet, // we'll cover the details shortly. // inMap.open("inmap.dat"); outMap.open("outmap.dat"); outMap.setf(ios::fixed, ios::floatfield); outMap.setf(ios::showpoint); // The following statments print the header info to output file. It's // rather cryptic at this point, but you'll see output is really very // simple in C++. outMap << "Dwight Barnette" << endl; //*** substitute your own name *** outMap << "Simple Map Distance Computations" << endl << endl; // This statement tries to read first line of data from the input file. If // something goes wrong, the variable inMap will be false (small lie) and // that will prevent us from reading further from the file and generating // nonsense. inMap >> numPoints >> mapScale; inMap.ignore(200, '\n'); // Skip to next line of input. // The next statments complete the printing of the header info // to the output file. outMap << "Map Scale Factor: " << setw(7) << setprecision(2) << mapScale; outMap << " miles per inch" << endl; outMap << endl; outMap << " Map Mileage" << endl; outMap << " Measure Distance" << endl; outMap << "============================================================"; outMap << endl; // This statement tries to read the second line of data from the input file. // If something goes wrong, the variable inMap will become false (another lie) and // that will prevent us from going into the loop that follows and generating // nonsense. if ( (inMap) && (numPoints > 0) ) inMap >> distance; // What comes next is a while loop. The body of the loop gets // executed over and over again until the expressions that follows // the while (inMap and visted less than numPoints in this case) becomes false. // Therefore, we'd better be sure that inMap will eventually become false, // or that visted will eventually become greater than or equal to numPoints, // otherwise the program will continue to execute the body of this // loop forever. while ( (inMap) && (visited < numPoints) ) { // The first part of the loop body (down to the comment with // all the hyphens) processes the data that was just read in. visited++; // Count number of locations. inMap.ignore(200, '\n'); // Skip to next line of input. // Compute miles for each distance on the map miles = double(int(distance * mapScale * 10.0 + 0.5)) / 10.0; totMiles = totMiles + miles; // Update total // --------------------------------- End of Processing Section // This section of the loop body prints the results calculated // above into the output file. outMap << "#" <> distance; } // End of while loop. // Once we've exited the loop, we're done reading and processing data for // each location. It's time to sum things up. The following statements // prints out the total distance in a nice form. First print a line to mark // the end of the table body: outMap << "============================================================"; outMap << endl; outMap << "Total Distance:" ; outMap << setw(7) << setprecision(1) ; outMap << totMiles << " miles" <