///////////////////////////////////////////////////////// // // Assignment #2 - A Solution // C++ I/O and Default Parameters // Author: Michael A. Redmond // Class: CSC 162 Section 01 // Date: 09/03/01 // // Description: // The Monopolysoft Corporation has requested a reimplementation of // a small piece of Windows 2001 A Computer Oddity. This part involves // (a simplification of) reporting on the amount of disk space // available on removable disks. The program asks the user for the // number of disks, then for each disk, asks how many bytes each file // uses (obviously in the real thing, this would be pulled from the // file info the system has rather than requested of a user, but we // need to simplify). After the user has indicated that all files have // been entered (by giving a size of -1), the program asks what type // of disk is involved and reports the disk space used and available // // Assumptions: // We will not track more than 1000 files on any disk. // //////////////////////////////////////////////////////// #include #include #include using namespace std; const long meg = 1048576; // one mega byte // duplicate clogs to cout to capture output for putting assignment together // Get a valid long integer from user // // Given: // a value initially obtained from the user // (done this way so that initial response could be -1 without messing up this function) // a prompt to display to the user // largest valid value // smallest valid value // // Returns: // valid number entered // // Side Effects: // None. // long GetValidLong (long val, string prompt, long max, long min = 0) { // loop until a valid value entered while ((val < min) || (val > max)) { clog << "value must be between " << min << " and " << max << endl; clog << prompt << flush; cout << "value must be between " << min << " and " << max << endl; cout << prompt << flush; cin >> val; cout << val; } return val; } /* fills the array, returns number of files read in */ // Given: // an array to be filled with a set of files sizes // // Returns: // number of file sizes entered // // Side Effects: // The array is filled. // // duplicate clogs to cout to capture output for putting assignment together int GetValidFileSizes (long sizes[]) { int cnt = 0; long val; // get initial file size clog << "Please enter the next file size (or just -1 to end) " << flush; cout << "Please enter the next file size (or just -1 to end) " << flush; cin >> val; cout << val; // loop until user indicates that they are done entering files // (indicated by entering -1 for file size) while (val != -1) { // largest file size accepted is 1000 meg (almost a gig) sizes[cnt] = GetValidLong(val,"Please enter the next file size ",1000*meg); cnt++; // get next file size clog << "Please enter the next file size (or just -1 to end) " << flush; cout << "Please enter the next file size (or just -1 to end) " << flush; cin >> val; cout << val; } return cnt; } /* calculates the sum of values in an array (any array, any program) */ // // Given: // an array of long integers // the number of real values in the array (array could have extra space) // // Returns: // the sum of the values in the array // // Side Effects: // None. // long CalcTotal (long data[], int num) { int cnt; long sum = 0; // loop through array, accumulating total for (cnt = 0; cnt < num; cnt++ ) { sum += data[cnt]; } return sum; } // Display available space and used space // // Given: // an array of file sizes // the number of real values in the array (array could have extra space) // the amount of space on an empty disk // // Returns: // Nothing // // Side Effects: // Displays the space used, total and remaining // // NOTE: default value for space on disk is space on a floppy // void DisplaySpace (long sizes[], int num, long totalAvail = 1.4*meg) { long used = CalcTotal(sizes, num); cout << "Files use " << used << " bytes out of " << totalAvail << " available - " << totalAvail - used << " remain " << endl; return; } // MAIN executable starts here int main () { long currSizes[1000]; int filesOnDisk; //char answ; int answ; // find out how many disks we are working on clog << "How many disks being processed? " << flush; cout << "How many disks being processed? " << flush; int numDisk; cin >> numDisk; cout << numDisk; // loop through all disks for (int cnt = 0; cnt < numDisk; cnt++) { // fill array of file sizes via interaction with user clog << "Obtaining info for disk " << cnt+1 << endl; cout << "Obtaining info for disk " << cnt+1 << endl; filesOnDisk = GetValidFileSizes(currSizes); // loop to make sure user enters a valid kind of disk bool ok = false; while (!ok) { clog << "Is the disk a floppy (1), Zip 100 (2), or Zip 250 (3) " << flush; cout << "Is the disk a floppy (1), Zip 100 (2), or Zip 250 (3) " << flush; cin >> answ; cout << answ; // default to indicating an ok disk type - will be changed if not so ok = true; // display available space based on type of disk switch (answ) { //case 'F': case 1: // display for floppy DisplaySpace(currSizes,filesOnDisk); break; //case 'Z': case 2: // display for 100 Meg zip DisplaySpace(currSizes,filesOnDisk,100* meg); break; //case 'B': case 3: // display for 250 Meg zip DisplaySpace(currSizes,filesOnDisk,250 * meg); break; default: ok = false; } // end switch } // end while } // end for return 0; }