Answer the question
In order to leave comments, you need to log in
When displayed on the screen, one line copies the other, although they should be different. How to fix?
The problem is that people copies s when printing the entire array of structures. They are given different values.
#include <iostream>
using namespace std;
enum stran {
kanada, malasia, turciya,
};
union no {
int people;
int s : 10;
};
struct Gosvo
{
no a;
string name;
string sity;
stran b;
};
int main()
{
setlocale(LC_ALL, "Russian");
Gosvo f;
f.a.people = 9;
f.name = "Belarus";
f.sity = "Minsk";
f.a.s = 2;
Gosvo k;
k.a.people = 213;
k.name = "Brasiliya";
k.sity = "Brasilia";
k.a.s = 8;
Gosvo l;
struct Gosvo* gs;
gs = &l;
gs->a.people = 83;
gs->name = "Iran";
gs->sity = "Tegeran";
gs->a.s = 16;
int n;
cout << "Введите количество структур в массиве:";
cin >> n;
Gosvo *gosvos=new Gosvo[n];
gosvos[0] = f;
gosvos[1] = k;
gosvos[2] = l;
cout << "Что делать? Выберите цифру";
cout << "1.Сортировка массива по алфавиту";
cout << "2.Поиск элемента по номеру в массиве";
cout << "3.Изменение массива структур";
cout << "4.Удаление первого элемента массива структур";
cout << "5.Вывод массива структур";
int m;
cin >> m;
if (m == 1) {
//сортировка по алфавиту
for (int i = 0; i <= n; i++) {
for (int j = 0; j < n - 1; j++)
{
if (gosvos[j].name > gosvos[j + 1].name)
{
string b = gosvos[j].name; // создали дополнительную переменную
gosvos[j].name = gosvos[j + 1].name; // меняем местами
gosvos[j + 1].name = b; // значения элементов
}
}
cout << gosvos[i].name << endl; // выводим элементы массива
}
}
else if (m == 2) {
//найти элемент массива
cout << "По какому параметру будем искать(name или sity)?" << endl;
string parametr;
cin >> parametr;
if (parametr == "sity") {
cout << "Найти по номеру элемента в массиве. Введите номер(0,1,2)" << endl;
int nomber;
cin >> nomber;
cout << gosvos[nomber].sity;
}
else if (parametr == "name") {
cout << "Найти по номеру элемента в массиве. Введите номер(0,1,2)" << endl;
int number;
cin >> number;
cout << gosvos[number].name;
}
else {
cout << "Нет параметра!";
}
}
else if (m == 3) {
cout << "Какой элемент будем изменять(name или sity)?" << endl;
string element;
cin >> element;
if (element == "sity") {
cout << "Номер элемента в массиве. Введите номер(0,1,2)" << endl;
int nomber;
cin >> nomber;
cout << "На что меняем?";
string smena;
cin >> smena;
gosvos[nomber].sity = smena;
}
else if (element == "name") {
cout << "Найти по номеру элемента в массиве. Введите номер(0,1,2)" << endl;
int nomber;
cin >> nomber;
cout << gosvos[nomber].name;
}
else {
cout << "Нет такого!";
}
}
else if (m == 4) {
int r=n;
Gosvo *arr = new Gosvo[--r];
for (int i = 0; i < n - 1; i++) {
arr[i] = gosvos[i];
}
delete[] gosvos;
gosvos = arr;
cout << "Элемент удалён";
}
else if (m == 5) {
cout << gosvos[0].name << gosvos[1].name << gosvos[2].name << endl;
cout << gosvos[0].sity << gosvos[1].sity << gosvos[2].sity << endl;
cout << gosvos[0].a.people << gosvos[1].a.people << gosvos[2].a.people << endl;
cout << gosvos[0].a.s << gosvos[1].a.s << gosvos[2].a.s << endl;
}
else {
cout << "error";
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
Doesn't copy.
You have no - this is a union (union). In a union, all union members occupy the same memory, i.e. if you look at sizeof(no) you will see 4 bytes, i.e. memory is allocated as for one int, and you have 2 ints there. Those. what you wrote in the code is how it works.
Those. operations, for example:
f.a.people = 9;
f.a.s = 2;
f.a.people == 2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question