-1

I need to make a dynamic array in C++, and ask a user to input a name until the user types exit.

It should keep asking for more and more names, recording them to a dynamic string array, then randomly choosing as many names as the user wants from the list.

I should be able to figure out the random numbers part, but the continuous input is giving me issues. I'm not sure how to have the length variable continue changing values.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int length;

    string* x;
    x = new string[length];
    string newName;

    for (int i = 0; i < length; i++)
    {
        cout << "Enter name: (or type exit to continue) " << flush;
        cin >> newName;
        while (newName != "exit")
        {
            newName = x[i];
        }
    }
    cout << x[1] << x[2];

    int qq;
    cin >> qq;
    return 0;
}

Any help would be greatly appreciated.

aaron
  • 39,695
  • 6
  • 46
  • 102
  • 2
    `length` doesn't have a value yet you think you can make an array of "undefined length" - I don't see any attempt to dynamically resize the array. Your while loop inside the for loop is nonsense. – John3136 Nov 30 '17 at 03:58
  • Consider `std::vector`. –  Nov 30 '17 at 04:01
  • Just to polish it little bit: `const int length=3;` and `x[i]=newname` and `cout< – macroland Nov 30 '17 at 04:11

1 Answers1

-1

A few errors:

  1. length is never assigned a value
  2. You're overwriting newName with an unassigned value x[i] of the array in newName = x[i];
  3. x is never dynamically reassigned with a new array of different length

Let's consider a solution:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int length = 2; // Assign a default value
    int i = 0;      // Our insertion point in the array

    string* x;
    x = new string[length];
    string newName;

    cout << "Enter name: (or type exit to continue) " << flush;
    cin >> newName; // Dear diary, this is my first input

    while (newName != "exit")
    {
        if (i >= length) // If the array is bursting at the seams
        {
            string* xx = new string[length * 2]; // Twice the size, twice the fun

            for (int ii = 0; ii < length; ii++)
            {
                xx[ii] = x[ii]; // Copy the names from the old array
            }

            delete [] x; // Delete the old array assigned with `new`
            x = xx;      // Point `x` to the new array
            length *= 2; // Update the array length
        }

        x[i] = newName; // Phew, finally we can insert
        i++;            // Increment insertion point

        cout << "Enter name: (or type exit to continue) " << flush;
        cin >> newName; // Ask for new input at the end so it's always checked
    }

    cout << x[1] << x[2]; // Print second and third names since array is 0-indexed

    int qq;
    cin >> qq; // Whatever sorcery this is
    return 0;
}

Addressing the errors mentioned:

  1. length is assigned a default value at the start
  2. Reverse the direction to get x[i] = newName;
  3. x is dynamically assigned a new array of exponentially-increasing length

Happy learning!

aaron
  • 39,695
  • 6
  • 46
  • 102
  • Hey, thanks, that works perfect, my mind's been a bit all over the place with coding (my class went from c++ to matlab to python in 1 semester). The qq is just to hold window with the in/outputs so I can see it. – Christian Miller Nov 30 '17 at 22:07