fork download
  1. //Cameron Pham CS1A Chapter 5, p. 296, #14
  2. //
  3. /******************************************************************************
  4. * ORGANIZE STUDENT LINE UP
  5. * ______________________________________________________________________________
  6. * This program accepts an integer value for a number of students in a class.
  7. * It will accept string values for names of the students, then organize them.
  8. * ______________________________________________________________________________
  9. * INPUT
  10. * Students // Total number of students in the class
  11. * Student name // Name of each student
  12. *
  13. * OUTPUT
  14. * Front student // Student at the front of the line
  15. * Back student // Student at the back of the line
  16. *
  17. * INPUT VALIDATION
  18. * Students > 1
  19. * Students < 25
  20. *******************************************************************************/
  21. #include <iostream>
  22. #include <string>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. // Initialize variables
  28. int students; // Total number of students in class
  29. string name; // Name of each student
  30. string frontStudent; // Student in the front of line
  31. string backStudent; // Student in the back of line
  32.  
  33. // Ask for number of students in class
  34. cout << "Enter the number of students in class.";
  35. cin >> students;
  36.  
  37. // Validate input
  38. while (students < 1 || students > 25)
  39. {
  40. cout << "Please enter a number between 1 and 25 students.";
  41. cin >> students;
  42. }
  43. cout << endl << "You entered " << students << " students." << endl;
  44.  
  45. // Loop through each student to get their name.
  46. for (int student = 1; student <= students; student++)
  47. {
  48. cout << "Enter the first name of the student " << student << ": ";
  49. cin >> name;
  50. cout << endl;
  51.  
  52.  
  53. // Initialize the front and back students according to the first student.
  54. if (student == 1)
  55. {
  56. frontStudent = name;
  57. backStudent = name;
  58. }
  59. else
  60. {
  61. // Current name comes before the first student alphabetically.
  62. if (name < frontStudent)
  63. {
  64. frontStudent = name;
  65. }
  66.  
  67. // Current name comes before the first student alphabetically.
  68. if (name > backStudent)
  69. {
  70. backStudent = name;
  71. }
  72.  
  73. }
  74. }
  75.  
  76. // Display the results.
  77. cout << endl << "The student at the front of the line is: " << frontStudent;
  78. cout << endl << "The student at the back of the line is: " << backStudent;
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0.01s 5320KB
stdin
8
Jason
Juno
Kasey
Cameron
Jewliana
Ryan
Grant
Shelly
stdout
Enter the number of students in class.
You entered 8 students.
Enter the first name of the student 1: 
Enter the first name of the student 2: 
Enter the first name of the student 3: 
Enter the first name of the student 4: 
Enter the first name of the student 5: 
Enter the first name of the student 6: 
Enter the first name of the student 7: 
Enter the first name of the student 8: 

The student at the front of the line is: Cameron
The student at the back of the line is: Shelly