Thursday, December 29, 2016

Programming Challenge 5.25 - Using Files - Student Line Up

Example File: LineUp.txt

/* Using Files - Student Line Up - This is a modified version of the
   program written for Programming Challenge 5.14. In this version,
   the names are stored in, and retrieved from, the file "LineUp.txt".

   Once all the names have been read in, the program reports which student
   would be at the front of the line and which one would be at the end of
   the line. For testing purposes, names should be added, or changed.
  
   Example: Put Abel between Bill and Mary */

#include "Utility.h"

int main()
{
    /* Declare: File stream variable */
    ifstream studentLineUp;

    /* Variable: Student name, Line up first, Line up last */
    string studentName = " ",
           lineUpFirst = " ",
           lineUpLast = " ";
   
    /* Variable: Student counter (counter variable), Update (counter
       variable) */
    int studentCounter = 0;
    int update = 1;

    /* Open: File "LineUp.txt" */
    studentLineUp.open("LineUp.txt");

    /* If statement::Error handling: If the file was opened successfully,
       the file will be processed */
    if (studentLineUp)
    {
        /* For loop: As long as student names are read in from "LineUp.txt"
           this loop will iterate */
        for (studentCounter; studentLineUp >> studentName; studentCounter++)
        {
                /* Nested While loop: While update is true, and student
                   names are read in from "LineUp.txt", this loop will
                   iterate */
                while (update == 1)
                {   
                    /* lineUpFirst and lineUpLast get studentName */
                    lineUpFirst = studentName;
                    lineUpLast = studentName;
                   
                    update++;
                }
               
                /* Conditional statement: Is the student currently first in line
                   smaller than the name read in from "LineUp.txt"? If so, first
                   in line will get student name */
                lineUpFirst = lineUpFirst < studentName ? lineUpFirst : studentName;

                /* Conditional statement: Is the student name currently last in
                   line greater than the name read in from "LineUp.txt"? If so,
                   last in line will get student name */
                lineUpLast = lineUpLast > studentName ? lineUpLast : studentName;
        }

        /* Display: Information */
        cout << "\t\tStudent Line Up\n\n"
             << "This program helps you to determine which student has to\n"
             << "stand at which position during roll call every morning.\n\n";

        /* Display: The name of the student who stands first in line, and
           the name of the student standing last in line every morning at
           roll call */
        cout << "Every morning, " << lineUpFirst << " will stand first in line.\n";
        cout << "Every morning, " << lineUpLast << " will stand last in line.\n";
    }

    else
    {
        /* Display: Error message */
        cout << "Error: For some reason the file 'LineUp.txt' could not be\n"
             << "opened. Please make sure that the file exists, that it has\n"
             << "the correct filename, and that it has not been accidentally\n"
             << "moved or has been deleted from the program folder.\n"
             << "Please close the program, check that the above conditions\n"
             << "are met, and start this program again.\n";
    }

    /* Close: File "LineUp.txt" */
    studentLineUp.close();

    pauseSystem();
    return 0;
}

No comments:

Post a Comment