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"); 

}

Recommended Answers

All 2 Replies

Just a note about formatting. Your code has a lot of commented lines and blank lines. This slows others and me from dissecting what's amiss here. You'll get more replies with clean code and telling what line you found that blew up.

Firstly, why do you allocate a dynamic array with size 0?

unsigned size = 0;

string * ptr = new string[size];

Secondly, if you're going to use std::string as you are now, then you must also include <string>.

Thirdly, your questions are hard to understand so the following code might be not what you want. Still hope the comments can be somehow useful for you tho.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    unsigned ctr = 0;
    unsigned size = 0;

    // Input the number of strings you wish to enter.
    cin >> size;

    // Since you use later on std::getline.
    cin.get();

    // Allocate dinamically a raw array of std::string(s) with a total of 'size' elements.
    string * ptr = new string[size];

    // Auxiliary array for inputing the std::string(s).
    string s;

    // Enter new input until the dynamic array is full.
    while (ctr < size && getline(cin, s))
    {
        // Normally store the std::string in the array.
        ptr[ctr] = s;
        ++ctr;
    }

    // Output the content of the raw array in the console.
    for (unsigned i = 0; i < size - 1; ++i)
        cout << ptr[i] << ", ";
    cout << ptr[size - 1] << '\n';

    // You absolutely MUST delete dynamic allocated pointers, either you get memory leaks (this case might not be the best example).
    delete[] ptr;
    return 0;
}
commented: Clean code is so much easier to read. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.