Thanks for the help initially I wrote a different code with a structure but I must have been coding it wrong because it didn't work. I have rectified my code now and change the pointer of "size" to an address. The arrays are growing however it is not printing out the value of the grown array as it would if it were a pointer. Is this correct so far (I still need to sort out if a user puts in multiple values and update them and print them in ascending order) ? I am struggling with this one. Everyone I speak to says use vectors so I may skip ahead to that chapter but want to try at least (also classes have not been covered yet this is why I haven't used any).
Code:
#include <iostream>
#include <string>
using namespace std;
int *growArray (int *friends, int &cur_size)
{
int *new_friends = new int[(cur_size==0 ? 1 : cur_size*2)];
for (int i=0; i < cur_size; ++i) {
new_friends[i] = friends[i];
}
//*cur_size = (*cur_size==0 ? 1 : *cur_size*2); //added here
delete []friends;
return new_friends;
}
string *growArray2 (string *name1, int &cur_size2)
{
string *new_name2 = new string[(cur_size2==0 ? 1 : cur_size2*2)];
for (int i=0; i < cur_size2; ++i) {
new_name2[i] = name1[i];
}
//*cur_size2 = (*cur_size2==0 ? 1 : *cur_size2*2);
delete []name1;
return new_name2;
}
void printArray (string *name1, int *friends, int &cur_size3, int element_set)
{
cout << "the total size of the array is: " << cur_size3 << endl;
cout << "Values in the array: " << endl;
for (int i = 0; i < element_set; ++i)
{
cout << name1[i] << "[" << i << "] = " << friends[i] << " days since you last spoke to them" << endl;
}
}
int main()
{
string exit = "X";
string name;
int size = 10;
int noDays;
int next_element = 0;
int *friends = new int[size];
string *name1 = new string [size];
cout << "Please enter your friends name" << endl;
cin >> name;
cout << "Please enter no of days you last spoke to them" << endl;
cin >> noDays;
while(true) {
if (size==next_element+1) {
friends = growArray(friends, size); //changed here
name1 = growArray2(name1, size); //changed here
}
friends[next_element] = noDays;
name1[next_element] = name;
next_element++;
printArray(name1, friends, size , next_element);
cout << "Please enter your friends name or X to exit" << endl;
cin >> name;
if (name == exit) {
break;
}
cout << "Please enter no of days you last spoke to them" << endl;
cin >> noDays;
}
//printArray(name1, friends, size , next_element);
return 0;
}