//Cameron Pham CS1A Chapter 5, p. 296, #14
//
/******************************************************************************
* ORGANIZE STUDENT LINE UP
* ______________________________________________________________________________
* This program accepts an integer value for a number of students in a class.
* It will accept string values for names of the students, then organize them.
* ______________________________________________________________________________
* INPUT
* Students // Total number of students in the class
* Student name // Name of each student
*
* OUTPUT
* Front student // Student at the front of the line
* Back student // Student at the back of the line
*
* INPUT VALIDATION
* Students > 1
* Students < 25
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Initialize variables
int students; // Total number of students in class
string name; // Name of each student
string frontStudent; // Student in the front of line
string backStudent; // Student in the back of line
// Ask for number of students in class
cout << "Enter the number of students in class.";
cin >> students;
// Validate input
while (students < 1 || students > 25)
{
cout << "Please enter a number between 1 and 25 students.";
cin >> students;
}
cout << endl << "You entered " << students << " students." << endl;
// Loop through each student to get their name.
for (int student = 1; student <= students; student++)
{
cout << "Enter the first name of the student " << student << ": ";
cin >> name;
cout << endl;
// Initialize the front and back students according to the first student.
if (student == 1)
{
frontStudent = name;
backStudent = name;
}
else
{
// Current name comes before the first student alphabetically.
if (name < frontStudent)
{
frontStudent = name;
}
// Current name comes before the first student alphabetically.
if (name > backStudent)
{
backStudent = name;
}
}
}
// Display the results.
cout << endl << "The student at the front of the line is: " << frontStudent;
cout << endl << "The student at the back of the line is: " << backStudent;
return 0;
}