First, this is for my CS296 class, so everything has to be in the main() function and I can’t use anything but loops, switch, and if statements.
Anyway, the program ask the user to enter the number of students in a class, followed by their names. It then displays which student should be at the front of the line and which student should be at the back of the line based on the students being lined up alphabetically from A-Z.
The program runs, but I feel like the design is poor. Specifically with how I’m using the count variable. I was hoping maybe to get some feedback regarding that. Thanks!
int main(void)
{
string front, back, student;
unsigned short count = 1, students;
cout << "Enter the number of students in the class. Valid range is 1 - 25." << endl
<< endl
<< "Students: ";
cin >> students;
while (students < 1 || students > 25)
{
cout << endl
<< "ERROR: Valid range is 1 - 25" << endl
<< endl
<< "Students: ";
cin >> students;
}
cout << endl
<< "Enter the name of student " << count << ": ";
cin.ignore();
getline(cin, student);
front = back = student;
for (count = 2; count <= students; count++)
{
cout << "Enter the name of student " << count << ": ";
getline(cin, student);
if (student < front)
front = student;
else if (student > back)
back = student;
}
cout << endl
<< front << " should be at the head of the line." << endl
<< back << " should be at the end of the line." << endl;
return 0;
}
No, that seems fine. Specs called for entering a count and then entering that many students which is exactly what you’ve done.
Don’t get me wrong, the program is bad but that’s because of ridiculous specs like “everything has to be in the main() function”, not anything to do with your implementation.
For this level of homework, I’d be happy getting something like that from my students. It would make a nice change from code that doesn’t even compile, single-character variable names everywhere, or an unformatted morass of incorrect indentation and mixed tabs/spaces 🙂