It is known that in a group of n people. Write a program to enter the name of the group of students and sort this list alphabetically.

#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { setlocale(0, "ru"); vector<string> students; string FIO; int n, j = 0, p = 0; cout << "Введите количество студентов в группе: "; cin >> n; cout << endl; for (int i = 0; i != n; i++) { cout << "Введите ФИО " << ++j << "го студента: "; getline(cin, FIO); students.push_back(FIO); } cout << "\nОтсортированный по алфавиту список студентов:" << endl; sort(students.begin(), students.end()); for (vector<string>::iterator it = students.begin(); it != students.end(); ++it) cout << ++p << ". " << *it << endl; system("pause"); return 0; } 

How to fix?

enter image description here

    1 answer 1

    The point is the translation of the line when entering the number of students, only the number was considered, and the character \n , no. For cleaning, before the cycle, you can use either the right solution:

     cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

    Or it is clear:

     getline(cin, FIO); 

    PS Correct and understandable - in this context is subjective.