How to alphabetize a list of names

I am in a C++ at my school we are just getting into loops. I have been tasked with creating a program that can read in a list of names one at a time (up to 25) and then display them in alphabetical order. This creates two problems for me, because these two things have not yet been addressed yet in this class:

1. I do not know how to alphabetize strings. We haven't discussed anything like that in class yet.
2. I don't know how I'm going to put each name in a different variable. At first I thought I could just create a variable that gets named differently based on the iteration of the loop, but I have no idea how to do that. Then I thought I would have to just declare 25 variables at the start, but then I realized I have no idea how each loop would put the input in the right variable.

At any rate, I'm really confused and have no idea what exactly to do about either of these problems. Problem #2 I have not seen a solution for when searching (not sure what to search for) and Problem #1 I've heard of a few methods but none of them really make sense with what we've learned so far.

Any help would be great. Thanks!

Oh and here's my code so far:
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int numStudents, count=0;

	cout << "This program will keep track of students and\n" <<
			"organize students in alphabetical order.\n" <<
			"How many students are in the class? ";
	cin >> numStudents;

	while ((numStudents>25) || (numStudents<1))
	{	
		cout << "Please enter a number of students between 1 and 25. ";
		cin >> numStudents;
	}

	do
	{
		count++;
		string newStudent;
		cout << "Enter student " << count  << "'s first name: ";
		cin >> newStudent;
	}while (count<numStudents);

	

	system("pause");
	return 0;
Last edited on
I suggest you use a vector of strings. You can then input them like this:

1
2
3
4
5
6
7
8
9
        vector<string> students;
        do
	{
		count++;
		string newStudent;
		cout << "Enter student " << count  << "'s first name: ";
		cin >> newStudent;
                students.push_back(newStudent);               
	}while (count<numStudents);

Last edited on
I wish I knew what that was (a vector). Basically we've covered only the very very basics of C++ and have only recently started getting into if-statements and loops. I don't doubt that it would work, I just think there SHOULD be a solution in what we've already learned. No guarantees that it actually is.
Have you done arrays?
If you're in basic c++ and have not learned about arrays and functions, more than likely your instructor wont ask you to alphabetize names. I believe that learning comes later.
Topic archived. No new replies allowed.