Sunday, December 18, 2016

Programming Challenge 5.14 - Student Line Up

/* Student Line Up - A teacher has asked all her students to line up single
file according to their first name. For example, in one class Amy will be
at the front of the line and Yolanda will be at the end.

This program prompts the user to enter the number of students in the class,
then loops to read that many names. Once all the names have been read it
reports which student would be at the front of the line and which one would
be at the end of the line. It is assumed that no two students have the same
name.

Input Validation: No number less than 1 or greater than 25 for the number of
students are accepted. */

#include "Utility.h"

int main()
{
    /* Constants: Minimum number of student names, Maximum number of
       student names */
    const int STUDENT_NAMES_MIN = 1,
              STUDENT_NAMES_MAX = 25;

    /* Variables: First name, Last name, Name, First in line, Last in
       line */
    string firstName = " ",
           lastName = " ",
           studentName = " ",
           lineUpFirst = " ",
           lineUpLast = " ";

    /* Display: Information */
    cout << "\t\tStudent Line Up\n\n"
        << "Please enter the number of students in your class, followed\n"
        << "by their names. This program will then help you to determine,\n"
        << "which student has to stand at which position during role call.\n";

    /* Variables: Number of student names, Update (counter variable) */
    int numStudentNames = 1,
        update = 1;

    /* Input: Ask for the number of student names */
    cout << "\nEnter the number of names you wish to enter: ";
    cin >> numStudentNames;
    cin.ignore();

    /* While loop::Input Validation: As long as the number of student names
       is lower than STUDENT_NAMES_MIN (1), or greater than STUDENT_NAMES_MAX
       (25) this loop will iterate*/
    while (numStudentNames < STUDENT_NAMES_MIN ||
           numStudentNames > STUDENT_NAMES_MAX)
    {
        /* Display: Error message */
        cout << "It seems that you entered a value lower than 1, or\n"
             << "greater than 25. Please stay within the limit.\n";

        /* Ask for the number of student names again */
        cout << "Enter the number of names you wish to enter: ";
        cin >> numStudentNames;
        cin.ignore();
    }

        /* While loop: While update is smaller than or equal to the number
           of student names the user wishes to enter, this loop iterates */
    while (update <= numStudentNames)
    {
        /* Ask the user for the first and last names of her students */
        cout << "Enter first and last name: ";
        getline(cin, firstName), (cin, lastName);
        studentName = firstName, lastName;
       
        /* If Statement: If update is true, firstInLine and lastInLine
           get the variable name */
        if (update == 1)
        {
            lineUpFirst = studentName;
            lineUpLast = studentName;
         }

        /* Conditional Statement: Is the student name currently first in
           line smaller than the name the user entered? If so, first in
           line will get name */
        lineUpFirst = lineUpFirst < studentName ? lineUpFirst : studentName;
       
        /* Conditional Statement: Is the student name currently last in
           line greater than the name the user entered? If so, last in
           line will get name */
        lineUpLast = lineUpLast > studentName ? lineUpLast : studentName;
       
        update++;
    }

    /* Display: The names of the students who have to stand first and
       last in line */
    cout << "\nEvery morning, " << lineUpFirst << " stands first in line.\n";
    cout << "Every morning, " << lineUpLast << " stands last in line.\n";

    pauseSystem();
    return 0;
}

No comments:

Post a Comment