(Simple) Variable changing value after iterating array

I am trying to iterate and modify an array of integers the value of n changes after the iteration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int n;
    int arr[n];
    
    cin >> n;
    
    cout << n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    cout << n;
    
    return 0;

}


Input:
1
2
4
1 4 3 2


Output:
 
42


Any ideas? Thanks for the help.
Last edited on
Found the problem. The value of n was set after creating the array.

1
2
cin >> n;
int arr[n];   
Last edited on
Hello cppnewbie10110,

That is not your only problem. C++ does not allow a VLA, (Variable Length Array). The size of the array must be known at compile time. This can be with a constant number like "20" or a variable defined as a constant.

With what yo started with:
1
2
    int n;
    int arr[n];

"n" is defined and storage space is reserved, but the variable is uninitialized and you have no idea what that garbage value is. Then you try to make an array with some unknown value. Along with C++ not allowing VLSs a (0) zero length array or a negative number is not allowed. It must be a positive number greater than zero.

Now if you want to get a value for "n" and create a dynamic array that will work.

To be fair some compilers still allow VLAs, but do not count on that being the norm.

Next is the 2 "cin" statements. Each needs a prompt so the user will know what to enter. Or you will need to provide a printed copy of the source code, which not every user will know how to read, or printed instructions to tell the user what to enter and when.

Andy

Edit:
Last edited on
You fixed one problem, there is another.

Variable Length Arrays are not legal in C++, the size of an array must be known at compile-time. Not legal the way you create your array.

If your compiler allows you to determine the size of an array at run-time you are using an outdated compiler. You should get a newer compiler.
Last edited on
Thank you Handy Andy and Furry Guy. Very helpful.
There are 3 basic ways to work with a container.

1. declare the array larger than the size the user is going to input:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
   // create a larger than expected array on the stack
   int arr[100];

   std::cout << "How many ints to store? ";
   int arr_size;
   std::cin >> arr_size;

   std::cout << "You can store " << arr_size << " items.\n";
}


2. get the needed size from the user and create the array to be on the heap using new[]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
   std::cout << "How many ints to store? ";
   int arr_size;
   std::cin >> arr_size;
   std::cout << '\n';

   std::cout << "You can store " << arr_size << " items.\n";

   // create the array on the heap to the required size
   // it requires using a pointer
   int* arr = new int[arr_size];

   for (int i { }; i < arr_size; ++i)
   {
      std::cin >> arr[i];
   }
   std::cout << '\n';

   // with each new/new[] you have to delete/delete[]
   delete[] arr;
}


3. use std::vector, it can be sized at runtime:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>

int main()
{
   std::cout << "How many ints to store? ";
   int vec_size;
   std::cin >> vec_size;
   std::cout << '\n';

   std::cout << "You can store " << vec_size << " items.\n";

   // create a vector the required size
   std::vector<int> vec(vec_size);

   for (int i { }; i < vec_size; ++i)
   {
      std::cin >> vec[i];
   }
   std::cout << '\n';
}

http://www.cplusplus.com/reference/vector/vector/

Prefer using a vector instead of a regular array.
Topic archived. No new replies allowed.