i wrote a code that will asks the user to enter a sentence and store the string in a dynamic array. copy the content of the old dynamic array into the new dynamic array and the while loop keeps going and asks the user to enter something new again and store in a new dynamic array. when i run it i am getting :
Unhandled exception at 0x0FB44B19 (vcruntime140d.dll) in countA.exe: 0xC0000005: Access violation writing location 0x2B5063A5.
i know that i can use vectors but for practice purpose , i am only allowed to use dynamic arrays.
# include <iostream>
#include <sstream> // used for getline
using namespace std;
int main()
{
char ch;
int ctr = 0;
string s;
string dayName[7];
unsigned size = 0;
string * ptr = new string[size];
// int ctr = 0;
while (getline(cin, s) )
//while (cin.get(ch))
{
//dayName[i] = s;
ptr[ctr] = s;
/*
if (ch == 'a')
{
++ctr;
}*/
//cout.put(ch);
//size*=2;
//++ctr;
string *temp = ptr;
ptr = new string[ctr + 1];
for (unsigned int i = 0; i < ctr; ++i)
{
ptr[i] = temp[i];
}
++ctr ;
}
/*
for (int j = 0; j< sizeof(dayName) / sizeof(dayName[0]); ++j)
{
cout << dayName[j] << "," ;
}
*/
for (int j = 0; j< size ; ++j)
{
cout << ptr[j] << ",";
}
//cout << ctr << endl;
system("pause");
}