Expanding array size

closed account (jLAfz8AR)
So for one small part of a project, I need to increase the size of an array by 5 spaces. When the code runs and the user enters whatever size and values they want for the array (I'm using 5 for the size and 1 2 3 4 5 as the values for this example), it should output 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, but I can't quite get it work properly.

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
#include <iostream>
using namespace std;
int main(){
int size;
char choice;

cout << "Enter the array size: ";
cin >> size;
int arr[size];

cout << "Enter the array values: ";
for(int i = 0; i < size; i++){
cin >> arr[i];
}

cout << "Your array: ";
for(int i = 0; i < size; i++){
cout << arr[i] << ", ";
}

cout << "\nIncrease size? (Y or N): ";
cin >> choice;

if(choice == 'y' || choice == 'Y'){
arr[size] = arr[size + 5];
cout << "The array is now: ";
for(int i = 0; i < size; i++){
cout << arr[i] << ", ";
}
}
else{
cout << "Goodbye!" << endl;
}

return 0;
}
First off things like:
1
2
cin >> size;
int arr[size];

Are not legal in C++, array sizes must be compile time constants.

The std::vector is the best choice to use if you want a dynamic "array".

As jlb said variable length arrays are illegal in C++. Use std::vector instead.
http://www.cplusplus.com/reference/vector/vector/
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
47
48
49
50
51
52
#include <iostream>
#include <vector>

int main()
{
   std::cout << "Enter the array size: ";
   int size;
   std::cin >> size;

   // create a sized vector, initial element values set to zero
   std::vector<int> vec(size);

   std::cout << "Enter the array values: ";

   // a regular for loop can easily access outside a vector's boundaries
   for (int i = 0; i < size; i++)
   {
      std::cin >> vec[i];
   }
   std::cout << '\n';

   std::cout << "Your array: ";

   // using iterators makes it harder to access elements outside a vector's boundaries
   // if you are not modifying the elements use const iterators
   for (std::vector<int>::const_iterator itr { vec.cbegin() }; itr != vec.cend(); itr++)
   {
      std::cout << *itr << ' ';
   }
   std::cout << "\n\n";

   std::cout << "Increase size? (Y or N): ";
   char choice;
   std::cin >> choice;
   std::cout << '\n';

   if (choice == 'y' || choice == 'Y')
   {
      vec.resize(size + 5);

      std::cout << "The array is now: ";

      // a range based for loop
      for (const auto& itr : vec)
      {
         std::cout << itr << ' ';
      }
      std::cout << "\n\n";
   }

   std::cout << "Goodbye!\n";
}
Enter the array size: 5
Enter the array values: 5 10 15 20 25

Your array: 5 10 15 20 25

Increase size? (Y or N): y

The array is now: 5 10 15 20 25 0 0 0 0 0

Goodbye!

The for loop iterator at line 26 could have been initialized using auto:
for (auto itr { vec.cbegin() }; itr != vec.cend(); itr++)
Topic archived. No new replies allowed.