Dynamicc Array Resizing

I am attempting to write a program that allows a user to input values into a dynamically allocated array. The user enters a letter when finished entering integers. A function is then called that recursively calculates and returns the sum of the values in the array. My code works as long as the array doesn't have to be resized. Once the user enters more than 5 values (the initial maxArraySize value) the program gets stuck and just accepts any input without doing anything. What am I missing?

As a side note, I would much rather complete this task using a vector, but the assignment specifies that vectors are NOT allowed.

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
         case 2: std::cout << "Enter an integer or enter X to stop entering integers.\n";
                for (int i = 0; i < maxArraySize; i++)
                {
                        std::cin >> myArray[i];                 //Stores integer in array
                        //Termintes if an integer is not entered.
                        if(!std::cin)
                        {
                           std::cout << "Finished entering integers.\n";
                           std::cin.clear();
                           std::cin.ignore();
                           break;
                        }

                        //Continues if an integer is entered
                        else
                        {
                           currentArraySize++;                  //Increments size counter

                           //Resizes array if necessary
                           if((currentArraySize == maxArraySize))
                           {
                                maxArraySize = currentArraySize + 5;    //Adds 10 to the max array size
                                int *largerArray = new int[maxArraySize];

                                //Copies each item from previous array into the new array
                                for(int n = 0; n < currentArraySize; n++)
                                {
                                        largerArray[i] = myArray[i];
                                }

                                delete [] myArray;              //Deletes previous array
                                myArray = largerArray;          //Points myArray to newly created array
                            }
                         }
                }
1
2
3
4
5
for(int n = 0; n < currentArraySize; n++)
{
         largerArray[i] = myArray[i];
 }


You're using 'n' as your counter but you're referencing your values with 'i'
It's amazing how many courses insist on teaching C++ backwards.
Thanks smarty. :)

I have updated the code a bit, but I still can't get it to work. Now when I execute the code it works the first time, but the second time it does not work. Any thoughts? I have updated the source code below.

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
       case 2: std::cout << "Enter an integer or enter X to stop entering integers.\n";
                for (int i = 0; i < maxArraySize; i++)
                {
                        std::cin >> myArray[i];                 //Stores integer in array

                        //Termintes if an integer is not entered.
                        if(!std::cin)
                        {
                           std::cout << "Finished entering integers.\n";
                           std::cin.clear();
                           std::cin.ignore();
                           break;
                        }

                        //Continues if an integer is entered
                        else
                        {
                           currentArraySize++;                  //Increments size counter

                           //Resizes array if needed
                           if(currentArraySize == maxArraySize)
                           {
                                maxArraySize *=2;               //Doubles maxArraySize

                                //Creates new larger array
                                int *largerArray = new int[maxArraySize];

                                //Loops to copy elements of myArray into largerArray
                                for (int a = 0; a < currentArraySize; a++)
                                {
                                   largerArray[a] = myArray[a];
                                }

                                //Deletes previous array
                                delete [] myArray;

                                //Points myArray to largerArray
                                myArray = largerArray;
                           }

                         }
                }
                //Prints sum of array values
                std::cout << "Sum of values in array:  " << sumArray(myArray, currentArraySize) << std::endl;

                break;
@Boilerplate - Agreed! This has been the source of many debates in the course so far.
Topic archived. No new replies allowed.