Advertisement
Guest User

Untitled

a guest
May 30th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.26 KB | None | 0 0
  1. /****************************************************************
  2.  *                                                              *
  3.  *      Program 2 - First Skeleton                              *
  4.  *  The program is made modular by use of separate functions.   *
  5.  *  A switch statement is used for the menu.                    *
  6.  *  File input is used to read in numbers of an array.          *
  7.  *                                                              *
  8.  *  Author: David Hume                                          *
  9.  *  Date of Creation: September 29, 2009                        *
  10.  *                                                              *
  11.  *                                                              *
  12.  ****************************************************************/
  13.  
  14.  
  15. #include <iostream>
  16. #include <fstream>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20.    /* Function prototypes allow the main function to call          */
  21.    /* those functions without knowing their internal details.      */
  22.    /* The prototype describes the argument list and return value.  */
  23.  
  24. void printArray (double x[], int  n); // function prototype
  25. void displayMenu ();                  // function prototype
  26. void readArray (double x [], int &n); // function prototype
  27.  
  28. int main ()
  29. {
  30.    /* The following variables are visible in the main function.  */
  31.  
  32.    bool    processAnotherFile = true;
  33.    char    response;
  34.    bool    menuActive;
  35.    char    menuChoice;
  36.  
  37.    double   x[100];  //array of up to 100 numbers
  38.    int      n;  // counts array elts x[0],x[1],...,x[n-1]
  39.  
  40.    while (processAnotherFile)
  41.    {
  42.  
  43.       readArray  (x, n);  // call the readArray function
  44.  
  45.       menuActive = true;
  46.       while (menuActive)
  47.       {
  48.          displayMenu ();   // call displayMenu function
  49.          cin >> menuChoice; cin.ignore(80,'\n');
  50.          cout << endl;
  51.  
  52.          switch (menuChoice)
  53.          {
  54.             case '1': case 'P': case 'p':
  55.                printArray (x, n);
  56.                break;
  57.             case '2':
  58.                cout << "Menu Option 2 executed" << endl << endl;
  59.                break;
  60.             case '3':
  61.                cout << "Menu Option 3 executed" << endl << endl;
  62.                break;
  63.             case '4':
  64.                cout << "Menu Option 4 executed" << endl << endl;
  65.                break;
  66.             case '5': case 'Q': case 'q':
  67.                menuActive = false; // Finish this series of menu choices
  68.                break;
  69.             default:
  70.                cout << "       Invalid menu choice: try again!" << endl << endl;
  71.          }  // end switch statement
  72.       }
  73.      
  74.       cout << "Process another file? y/n : ";
  75.       cin  >> response; cin.ignore(80,'\n');
  76.       if (response == 'n' || response == 'N') processAnotherFile = false;
  77.       cout << endl << endl;
  78.    }  // end while (goAgain) loop
  79.  
  80.    return 0;
  81. }  // end main function
  82.  
  83. /****************************************************************
  84.  *                                                              *
  85.  *     This function handles details of reading a 1-dimensional *
  86.  * array of elements of type double.  After prompting for a     *
  87.  * filename, the file is opened, and the numbers are read and   *
  88.  * stored in an array as x[0],x[1],x[2],...,x[n-1].             *
  89.  *                                                              *
  90.  * First argument: the array where numbers are stored -- passed *
  91.  *    by reference (as are all arrays by default).              *
  92.  *                                                              *
  93.  * Second argument: n = number of elements in the array -- also *
  94.  *    passed by reference using the "&" notation.               *
  95.  *                                                              *
  96.  ****************************************************************/
  97. void readArray (double x [], int &n)
  98. {
  99.    int      i;
  100.  
  101.    char     filename[51]; // room for 50-character path/file name
  102.    ifstream inputFile;    // object declaration: input file stream
  103.  
  104.    cout << "Enter filename (and path, if needed):" << endl;
  105.    cin.getline (filename, 51);
  106.    cout << endl << endl;
  107.    
  108.    inputFile.open(filename);
  109.  
  110.    i = 0;
  111.    
  112.    while (inputFile.eof() == false)
  113.    {      
  114.       inputFile >> x[i];
  115.       if (!inputFile.eof())  // means that x[i] was successfully read
  116.          i++;  
  117.    }
  118.    inputFile.clear();  // clear flags associated with the file
  119.    inputFile.close();
  120.    n = i;  // n = number of elements in array
  121.  
  122. }  // end readArray function
  123.  
  124. /****************************************************************
  125.  *                                                              *
  126.  *     This function displays a 1-dimensional array of numbers  *
  127.  * of type double in six columns of field width 10 with two     *
  128.  * decimals to the right of the decimal point.                  *
  129.  *                                                              *
  130.  * First argument: the array of numbers is passed by reference, *
  131.  *    as is the case for all arrays.                            *
  132.  *                                                              *
  133.  * Second argument: n = number of elements in the array; this   *
  134.  *    argument is passed by value, meaning that the value comes *
  135.  *    in to the function, but cannot be sent back out.          *
  136.  *                                                              *
  137.  ****************************************************************/
  138. void printArray (double x[], int n)
  139. {
  140.    int i;
  141.    int columnCount;
  142.  
  143.    cout << endl << endl;
  144.    cout << "Echo of Array Values from Input File:" << endl << endl;
  145.    cout << setprecision(2) << fixed << right;
  146.    columnCount = 0;
  147.    for (i=0; i < n; i++)
  148.    {
  149.       cout << setw(12) << x[i];
  150.       columnCount++;
  151.       if (columnCount == 6)
  152.       {
  153.          columnCount = 0;
  154.          cout << endl << endl;
  155.       }
  156.  
  157.    }
  158.    cout << endl << endl;
  159.    cout << "Number of elements in array: " << n << endl << endl;
  160. }  // end printarray function
  161.  
  162.  
  163. /****************************************************************
  164.  *                                                              *
  165.  *         This function displays the main menu.  There are no  *
  166.  *   input arguments to the function, and no data is returned.  *
  167.  *                                                              *
  168.  ****************************************************************/
  169.  
  170. void displayMenu ()
  171. {
  172.    cout << "   *************************************************" << endl;
  173.    cout << "   *                                               *" << endl;
  174.    cout << "   *           M A I N    M E N U                  *" << endl;
  175.    cout << "   *                                               *" << endl;
  176.    cout << "   *   1) (P)rint Array                            *" << endl;
  177.    cout << "   *   2) Option 2                                 *" << endl;
  178.    cout << "   *   3) Option 3                                 *" << endl;
  179.    cout << "   *   4) option 4                                 *" << endl;
  180.    cout << "   *   5) (Q)uit: Finish processing this array     *" << endl;
  181.    cout << "   *                                               *" << endl;
  182.    cout << "   *************************************************" << endl;
  183.    cout << "       Enter choice: ";
  184. }  // end displayMenu function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement