Array question

This is going to sound silly to be honest given I've done more complex programs and post here from time to time, but I need quick help with something. I've been trying to initialize a basic array with the limit being 5 and set size equal to 5 and put that in the array. I looked around and looked like it worked for others, but not me. Am I overthinking this? It's a little embarrassing for me to ask this. By the way the code is of course not finished since I've been trying to fix this little issue. I could eliminate the size and manually set everything to 5 but that's a little sloppy.
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
  #include <iostream>
using namespace std;


class CheckedArray 
{
private:
	int size = 5;
	int arr[size];
	
public:
	CheckedArray();
	void info();
};

void CheckedArray::info()
{
	cout << "Enter in size of an array no more than 5";
	for (int i = 0; arr < size; i++)
	{
		cin >> arr[i];
	}
	if (arr > size)
	{

	}
	
}


int main()
{
	CheckedArray enter;
	enter.info;

}
Last edited on
Regular arrays on the stack need to know their size at compile time, you can't create one at run time.

You can create an array on the heap at run time. int* arr = new int[some_size];

Or use std::vector.
some compilers allow it as a language extension. That is why it works for others. It is bad practice to rely on compiler errors or extensions to get working code, doubly so when there are legal alternatives that work on all compilers.
i am very new to C++ but hopefully i can help anyway.

maybe you could use an array with 5 elements and only allow access to the elements in the range chosen by the user?
you could do that by discarding anything that tries to access elements higher than the selected length.
that works, but it is the 1990/C way. Vectors are the C++ way.
yea i have been using vectors and i would do it like that too tbh.
you could just create a loop that iterates the amount of times input by the user, then use push_back on each iteration to create each element.

not sure if thats the best way though, but it should work for what you need.
Furry Guy wrote:
Or use std::vector.

why vector? why not std::array?
Correct me if im wrong, but i believe std::array needs to know the length at compile time, so would be the same as the above code. Vectors do not, so you could add elements dynamically dependant on user input.
Correct me if im wrong, but i believe std::array needs to know the length at compile time, so would be the same as the above code.

Yes both "raw" arrays and std::array<> must have compile time constants for their sizes.

Vectors do not, so you could add elements dynamically dependant on user input.

Correct std::vectors can have elements added with push_back(), or you can use a non-compile time variable to initialize the vector to a given size.

why vector? why not std::array?

A std::array size must be known at compile time, so it has the same disadvantage as a regular array. Lines 8-9 declare a Variable Length Array, something the C++ standard doesn't allow. (compiler language extensions aren't portable)

It can't be sized or resized at run-time, as the OP's code snippet SEEMS to want to do (line 18).

I've been trying to initialize a basic array with the limit being 5 and set size equal to 5 and put that in the array.

The problem(s) boil down to line 8 doesn't declare data member size as a static const, and line 18 appears to ask the user for a size for the array instead of asking for data for an array of size 5.

Where is the ctor's definition that was declared on line 12?

The name of an array resolves to a pointer to the first element of the pointer. Lines 19 and 23 can't compare a int* to an int.

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
#include <iostream>

class CheckedArray
{
private:
   static const int size = 5;
   int arr[size];

public:
   // CheckedArray(); // no ctor definition
   void info();
};

int main()
{
   CheckedArray enter;

   enter.info();
}

void CheckedArray::info()
{
   // this statement is misleading
   std::cout << "Enter in size of an array no more than 5:\n";

   // this is filling the array with data,
   // not asking for some information about the array's size
   for (int i = 0; i < size; i++)
   {
      std::cin >> arr[i];
   }

   // maybe this is what you intend?
   std::cout << "The size is: " << size << '\n';
}


To use a std::array still requires the size data member to be declared as static const.

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
#include <iostream>
#include <array>
class CheckedArray
{
private:
   static const int size = 5;
   std::array<int, size> arr;

public:
   // CheckedArray(); // no ctor definition
   void info();
};

int main()
{
   CheckedArray enter;

   enter.info();
}

void CheckedArray::info()
{
   // this statement is misleading
   std::cout << "Enter in size of an array no more than 5:\n";

   // this is filling the array with data,
   // not asking for some information about the array's size
   for (int i = 0; i < size; i++)
   {
      std::cin >> arr[i];
   }

   // maybe this is what you intend?
   std::cout << "The size is: " << size << '\n';
}


Using a std::vector makes it easy to resize at run-time the container's size, if that is something the OP wants. A std::vector can be declared with a data member that isn't declared static const,

OR declare the size data member as static const.

No need for the "extra" size data member with std::vector or std::array they know their size already.
push-back triggers an internal very slow performing resize if you do too many of them in a loop.
pre-allocate vectors with reserve to avoid that if you KNOW you are about to spam push-back in a loop, take a stab at a good default size from what you expect the program to run usually.

if you expect it to be a fixed size, but allow growth if it over-runs it, you can do that too..
vector <int> x(100); //this will act just like int x[100] except it can grow if you run out later. This actually allocates the spaces, so x[99] is valid and exists, and push back creates x[100]. reserve makes space for push-back to activate without growth, but x[99] does not exist if you used reserve.
Last edited on
Topic archived. No new replies allowed.