Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************
- * *
- * Program 2 - First Skeleton *
- * The program is made modular by use of separate functions. *
- * A switch statement is used for the menu. *
- * File input is used to read in numbers of an array. *
- * *
- * Author: David Hume *
- * Date of Creation: September 29, 2009 *
- * *
- * *
- ****************************************************************/
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- /* Function prototypes allow the main function to call */
- /* those functions without knowing their internal details. */
- /* The prototype describes the argument list and return value. */
- void printArray (double x[], int n); // function prototype
- void displayMenu (); // function prototype
- void readArray (double x [], int &n); // function prototype
- int main ()
- {
- /* The following variables are visible in the main function. */
- bool processAnotherFile = true;
- char response;
- bool menuActive;
- char menuChoice;
- double x[100]; //array of up to 100 numbers
- int n; // counts array elts x[0],x[1],...,x[n-1]
- while (processAnotherFile)
- {
- readArray (x, n); // call the readArray function
- menuActive = true;
- while (menuActive)
- {
- displayMenu (); // call displayMenu function
- cin >> menuChoice; cin.ignore(80,'\n');
- cout << endl;
- switch (menuChoice)
- {
- case '1': case 'P': case 'p':
- printArray (x, n);
- break;
- case '2':
- cout << "Menu Option 2 executed" << endl << endl;
- break;
- case '3':
- cout << "Menu Option 3 executed" << endl << endl;
- break;
- case '4':
- cout << "Menu Option 4 executed" << endl << endl;
- break;
- case '5': case 'Q': case 'q':
- menuActive = false; // Finish this series of menu choices
- break;
- default:
- cout << " Invalid menu choice: try again!" << endl << endl;
- } // end switch statement
- }
- cout << "Process another file? y/n : ";
- cin >> response; cin.ignore(80,'\n');
- if (response == 'n' || response == 'N') processAnotherFile = false;
- cout << endl << endl;
- } // end while (goAgain) loop
- return 0;
- } // end main function
- /****************************************************************
- * *
- * This function handles details of reading a 1-dimensional *
- * array of elements of type double. After prompting for a *
- * filename, the file is opened, and the numbers are read and *
- * stored in an array as x[0],x[1],x[2],...,x[n-1]. *
- * *
- * First argument: the array where numbers are stored -- passed *
- * by reference (as are all arrays by default). *
- * *
- * Second argument: n = number of elements in the array -- also *
- * passed by reference using the "&" notation. *
- * *
- ****************************************************************/
- void readArray (double x [], int &n)
- {
- int i;
- char filename[51]; // room for 50-character path/file name
- ifstream inputFile; // object declaration: input file stream
- cout << "Enter filename (and path, if needed):" << endl;
- cin.getline (filename, 51);
- cout << endl << endl;
- inputFile.open(filename);
- i = 0;
- while (inputFile.eof() == false)
- {
- inputFile >> x[i];
- if (!inputFile.eof()) // means that x[i] was successfully read
- i++;
- }
- inputFile.clear(); // clear flags associated with the file
- inputFile.close();
- n = i; // n = number of elements in array
- } // end readArray function
- /****************************************************************
- * *
- * This function displays a 1-dimensional array of numbers *
- * of type double in six columns of field width 10 with two *
- * decimals to the right of the decimal point. *
- * *
- * First argument: the array of numbers is passed by reference, *
- * as is the case for all arrays. *
- * *
- * Second argument: n = number of elements in the array; this *
- * argument is passed by value, meaning that the value comes *
- * in to the function, but cannot be sent back out. *
- * *
- ****************************************************************/
- void printArray (double x[], int n)
- {
- int i;
- int columnCount;
- cout << endl << endl;
- cout << "Echo of Array Values from Input File:" << endl << endl;
- cout << setprecision(2) << fixed << right;
- columnCount = 0;
- for (i=0; i < n; i++)
- {
- cout << setw(12) << x[i];
- columnCount++;
- if (columnCount == 6)
- {
- columnCount = 0;
- cout << endl << endl;
- }
- }
- cout << endl << endl;
- cout << "Number of elements in array: " << n << endl << endl;
- } // end printarray function
- /****************************************************************
- * *
- * This function displays the main menu. There are no *
- * input arguments to the function, and no data is returned. *
- * *
- ****************************************************************/
- void displayMenu ()
- {
- cout << " *************************************************" << endl;
- cout << " * *" << endl;
- cout << " * M A I N M E N U *" << endl;
- cout << " * *" << endl;
- cout << " * 1) (P)rint Array *" << endl;
- cout << " * 2) Option 2 *" << endl;
- cout << " * 3) Option 3 *" << endl;
- cout << " * 4) option 4 *" << endl;
- cout << " * 5) (Q)uit: Finish processing this array *" << endl;
- cout << " * *" << endl;
- cout << " *************************************************" << endl;
- cout << " Enter choice: ";
- } // end displayMenu function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement