1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void add_data(int id, int* p_id, char* name, char** p_name, int num_datapoints)
{
cout << "num_datapoints = " << num_datapoints << endl;
cout << "in add_data, name: " << name << endl;
*(p_id + num_datapoints-1) = id;
*(p_name + num_datapoints-1) = name;
return;
}
void output_data(int* p_id, char** p_names, int num_datapoints)
{
cout << "id\tname\n";
for(int i = 0 ; i < num_datapoints ; i++)
{
cout << p_id[i] << "\t" << p_names[i] << endl;
}
return;
}
int main(void)
{
const int MAX(15);
int num_datapoints = 0;
int array_size = 5;
int* p_id = new int[array_size];
char** p_names = new char*[array_size];
for(;;)
{
int id = 0;
char* myname = new char[MAX];
cout << "Id number: ";
cin >> id;
if(!id)
break;
cout << "Name: ";
cin.clear();
cin.sync();
cin.getline(myname,MAX,'\n');
if(++num_datapoints > array_size)
{
array_size += 5;
int* p_temp_id = new int[array_size];
for(int i = 0 ; i < num_datapoints ; i++)
p_temp_id[i] = p_id[i];
delete p_id;
p_id = p_temp_id;
p_temp_id = nullptr;
}
add_data(id, p_id, myname, p_names, num_datapoints);
}
output_data(p_id, p_names, num_datapoints);
return 0;
}
|