Untitled
unknown
plain_text
2 years ago
1.6 kB
7
Indexable
#include <iostream>
using namespace std;
//#define DEBUG
struct {
int size = 1;
int *age = new int[size];
std::string *names = new std::string[size];
} Data;
int main() {
int choice = 1;
do {
cout << "Enter name: "; cin >> Data.names[Data.size-1];
cout << "Enter age: "; cin >> Data.age[Data.size-1];
cout << "Continue typing? (1-yes/0-no): "; cin >> choice;
if (choice == 1) {
Data.size++;
#ifdef DEBUG
cerr << "1 + The data size is equal to: " << Data.size << endl;
#endif
int *temp_age = Data.age;
string *temp_names = Data.names;
#ifdef DEBUG
cerr << "2 + The index entry was completed successfully!" << endl;
#endif
Data.age = nullptr;
Data.names = nullptr;
#ifdef DEBUG
cerr << "3 + The memory has been successfully cleared!" << endl;
#endif
Data.age = new int[Data.size];
Data.names = new string[Data.size];
#ifdef DEBUG
cerr << "4 + Memory has been successfully allocated!" << endl;
#endif
for (int i = 0; i < Data.size - 1; i++) {
Data.age[i] = temp_age[i];
Data.names[i] = temp_names[i];
}
temp_age = nullptr;
temp_names = nullptr;
#ifdef DEBUG
cerr << "5 + The old data has been successfully written to the new pointer!" << endl;
#endif
}
} while (choice == 1);
for (int i = 0; i < Data.size; i++) {
cout << "Name: " << Data.names[i] << ", age: " << Data.age[i] << endl;
}
return 0;
}Editor is loading...
Leave a Comment