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!
#include<iostream>
#include<string>
#include<fstream>
usingnamespace 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;
}