Reading and sorting

Hi, I need help with an assignment. I need to read a list of names from a text file, sort the names alphabetically, and then output the names that would be first and last in the list. The number of names must be between 1 and 25. My professor forbids us from using anything we havent learned, so I am forced to use only if statements, loops,operators,cin/cout,and fstream. My code runs into problems when it reaches the string comparison, but I can't figure out why.
Any help would be appreciated!

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{

	//Intialize variables
	std::string firstEntry;
	std::string secondEntry;
	string first;
	string last;
	ifstream inputFile;
	string studentName;
	string filename;
	int students;

	//Ask for amount of students
	cout << "Please enter the number of students in the class.\n(The number must be a whole number between 1 and 25.)\n";
	cin >> students;
	

	//Input validation
	while (students < 1 || students > 25)
	{
		cout << "\nInvalid value. Please enter a value between 1 and 25.\n";
		cin >> students;
	}

	//Get file name from user
	cout << "Please enter the name of the file with the list of students\n";
	cin >> filename;

	//Open the file
	inputFile.open(filename);
	if (inputFile)
	{
		while (inputFile >> studentName)
		{
			studentName = firstEntry;
			inputFile >> studentName;
			studentName = secondEntry;
			do
			{
				if (firstEntry.compare(secondEntry) > 0)
				{
					firstEntry = first;
					secondEntry = last;
				}
				else
				{
					firstEntry = last;
					secondEntry = first;
				}
			} while (students = 30);

			if (firstEntry.compare(first) > 0)
				firstEntry = first;

			if (secondEntry.compare(first) > 0)
				secondEntry = first;

			if (firstEntry.compare(last) < 0)
				firstEntry = last;

			if (secondEntry.compare(last) < 0)
				secondEntry = last;
				
		}
		cout << first << " is the first student in line.";
		cout << last << " is the last student in line.";
	}
	else
	{
		cout << "Error opening the file.\nPlease restart the program and try again.";
		return 1;
	}

	inputFile.close();
	return 0;
}
Line 56 is supposed to be } while (students == 30); // Note: == .

'==' -> Comparing
'=' -> assigning

You will have an infinite loop since students = 30 is always true.

You may simplify the if. E.g.:
1
2
			if (firstEntry > first)
				firstEntry = first;
Topic archived. No new replies allowed.