Как вывести по фамилии в алфавитном порядке и если повторяется то обнулить
Остальная часть кода ниже
Output structure records by surname in alphabeticalorder;if duplicates-NULL
struct St
for (i = 0; i < 3; i++)
{
cout << i << "\n Please input your personal data \n";
cin >> student[i].surname;
cin >> student[i].name;
cin >> student[i].gender;
cin >> student[i].age;
cin >> student[i].citizenship;
}
cout << "\n\n";
for (i = 0; i < 3; i++)
{
if (student[i].surname == student[i + 1].surname)
{
student[i].surname = '\0';
student[i].name = '\0';
student[i].gender = '\0';
student[i].age = '\0';
student[i].citizenship = '\0';
}
cout << student[i].surname << "\n";
cout << student[i].name << "\n";
cout << student[i].gender << "\n";
cout << student[i].age << "\n";
cout << student[i].citizenship << "\n";
Ответы (1 шт):
Автор решения: Матвей Суслов
→ Ссылка
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct St {
string surname;
string name;
string gender;
string age;
string citizenship;
};
bool comp(St& a, St& b) {
return a.surname < b.surname;
}
int main() {
St student[100];
int n = 3; // количество учеников
for (int i = 0; i < n; i++) {
cout << i << " Please input your personal data: \n";
cin >> student[i].surname;
cin >> student[i].name;
cin >> student[i].gender;
cin >> student[i].age;
cin >> student[i].citizenship;
}
sort(student, student + n, comp);
for (int i = 0; i < n - 1; ++i) {
if (student[i].surname != student[i + 1].surname) {
cout << student[i].surname << "\n";
cout << student[i].name << "\n";
cout << student[i].gender << "\n";
cout << student[i].age << "\n";
cout << student[i].citizenship << "\n";
}
}
cout << student[n - 1].surname << "\n";
cout << student[n - 1].name << "\n";
cout << student[n - 1].gender << "\n";
cout << student[n - 1].age << "\n";
cout << student[n - 1].citizenship << "\n";
return 0;
}