Student Line Up

Having issues putting the names in alphabetical order, I'm trying to figure out how approach it but to no avail. Can some some one help me out. Heres the question to the problem:

"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. Write a program that prompts the user to enter the number of students in the class, then loops to read in that many names. Once all the names have been read in it reports which student would be at the front of the line and which one would be at the end of the line. You may assume that no two students have the same name.

Input Validation: Do not accept a number less than 1 or greater than 25 for the num- ber of students."

PLEASE HELP!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{
    string students;
    int numStudents;
    
    cout << "How many students do you have? ";
    cin >> numStudents;
    
    if (numStudents < 1 || numStudents > 25)
    {
        cout << "Cannot have less than 1 student or more than 25 students..." << endl;
        cout << "Exiting..." << endl;
        exit(0);
    }
    
    
    ofstream outputFile("Names.txt");
    for (int i = 1; i <= numStudents; i++)
    {
        cout << "Enter the name of your students: ";
        cin >> students;
        outputFile << students << endl;
    }
    outputFile.close();
    
    ifstream inFile("Names.txt");
    for (int i = 1; i <= numStudents; i++)
    {
        inFile >> students;
        
        students.compare(students);
        cout << students << " ";
    }
    inFile.close();
    
    
    
    
    
    
    
    
    return 0;
}
This sounds like you need to use a container to hold the student names, a vector perhaps, or an array. Store the names in the vector, then after you are done entering the names sort them and then print them. By the way I don't see anything in the assignment description stating you need to use a file.

I was just trying to get in some practice with files. But I haven't gotten to arrays yet, and I'm following the "Starting Out With C++" textbook. I have only covered up to chapter 5(nested loops) so I assume it can be done without arrays and vectors. Would you have any other solutions? Thanks.
Just do what is required of the assignment, no more and no less. As @jlb has said you need to put all the names into a container e.g. vector and then use a sort function from the STL to sort the contents of the container into alphabetical order and display the name of the student at the beginning and the name at the end of the sorted list.
Since you haven't learned about vectors or arrays, the best way to do this is to just compare strings. There is no requirement that the others after the first and before the last need to be sorted correctly. You need to compare names to each other. Basically you will have a string that holds the first person in line and one to hold the last person in line. The first person will be alphabetically less than everyone else while the last person will be the higher than everybody else. Just read the first name assign that name to both the first and last variables then compare the rest of the file to that name, swapping the values as necessary.
A teacher has asked all her students to line up single file according to their first name.

Without either a vector or an array this gets very complicated. I suggest it's time to learn to use a vector or an array.

The other option would be to read the file and locate the beginning student, write that name to your output file. Re-read the file from the beginning to locate the next student, write that name to the file, repeat until finished.





Last edited on
I'll give the string comparisons a shot. If not, I'll just return to this problem when I've read and understood vectors, arrays, and sorting. Thanks for the input y'all!
Topic archived. No new replies allowed.