Array Error

Pages: 12
Apr 13, 2021 at 4:42am
So I'm writing code that will prompt a user for the size they desire for an array, then I have to create an array of that size

later i will have to loop asking them to input a value for each element, but I think i have that down.

i have a few error messages popping up, and i'm not sure why. what errors do you see?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;

int main() 
{
	int arraySize;
	const int userArray;

	cout << "\nThis program creates an array of various numbers and will sort them." << endl;

	cout << "\nHow large would you like the array?" << endl;
	cin >> arraySize;

	int userArray = arraySize;
	const int userArray[arraySize];

Apr 13, 2021 at 5:30am
You should tell us what the errors are (copy them). Learning how to interpret error messages that a compiler tells you is a vital skill to learn in C++ programming.

const int userArray;You can't have an uninitialized const variable. It has to be assigned some value.

int userArray = arraySize; const int userArray[arraySize];Now, you're trying to re-declare the variable called 'userArray' in two more places. You can't re-use a variable name if it's already in use within the same scope.

const int userArray[arraySize];Having array lengths that aren't known at compile-time is not standard. You should instead either use a vector, or allocate a large enough array to handle what the user needs.

For example...
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
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{
	cout << "\nThis program creates an array of various numbers and will sort them." << endl;

	cout << "\nHow large would you like the array? ";
	int arraySize;
	cin >> arraySize;
	
	int userArray[10000];

	for (int i = 0; i < arraySize; i++)
	{
		userArray[i] = (1 + i * i * 41) % 13; // just some numbers 
	}
    
	std::sort(userArray, userArray + arraySize);

	// print
	for (int i = 0; i < arraySize; i++)
	{
		std::cout << userArray[i] << '\n';   
	}
}
Last edited on Apr 13, 2021 at 5:32am
Apr 13, 2021 at 9:48am
As Ganado points out above, the size of an array has to be known to the compiler at compile time. It can't be set at run-time. If the size of the array is to be determined at run-time, then usually a std::vector would be used.

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

int main()
{
	size_t arraySize {};

	std::cout << "\nThis program creates an array of various numbers and will sort them.\n";
	std::cout << "\nHow large would you like the array? ";
	std::cin >> arraySize;

	std::vector<int> userArray(arraySize);

	std::cout << "Please enter " << arraySize << " numbers:\n";

	for (auto& a : userArray)
		std::cin >> a;

	std::sort(std::begin(userArray), std::end(userArray));

	std::cout << "\nThe sorted numbers are:\n";

	for (const auto& a : userArray)
		std::cout << a << ' ';

	std::cout << '\n';
}

Apr 13, 2021 at 3:04pm
Hello luckylukebrooks,

The comments should help to explain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;  // <--- Best not to use.

int main()
{
    int arraySize{};  // <--- ALWAYS initialize your variables.
    const int userArray;  // <--- Being defined as a constant it needs a value. But defined as a constant it can not be changed in the program.

    cout << "\nThis program creates an array of various numbers and will sort them.\n";  // <--- Prefer the new line (\n) over the function "endl".

    cout << "\nHow large would you like the array? ";  // <--- Writing your prompt this way puts the "cin" on the same line.
    cin >> arraySize;

    int userArray = arraySize;
    const int userArray[arraySize];  // <--- Not standard C++. May not work with some compilers.
}


Based on you code I think this is what you you are trying to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;  // <--- Best not to use.

int main()
{
    int arraySize{};
    int* userArray{};

    cout << "\nThis program creates an array of various numbers and will sort them.\n";

    cout << "\nHow large would you like the array? ";
    cin >> arraySize;

    userArray =  new int[arraySize];

    // <--- Other code here.

    delete[] userArray;

    return 0;  // <--- Not required, but makes a good break point.
}


Andy
Apr 13, 2021 at 4:27pm
Unless there is a specific requirement to use new it's always better to use a container such as std::vector - or at a minimum std::unique_memory. A raw pointer should always be the last resort.
Apr 13, 2021 at 4:33pm
@ Handy Andy

A couple questions:

1. what does the star mean in line 8?

2. what does new int mean on line 15?

3. What is that delete on line 19?

I apologize, my teacher hasn't gone over that yet
Apr 13, 2021 at 4:38pm
@ seeplus

for this assignment my teacher doesn't want us to use a vector, not sure why
Apr 13, 2021 at 4:47pm
Andy's code is using dynamic memory - which you say you haven't covered. You're left with defining an array as large as might be used as mentioned by Ganado above. Not the best way.
Last edited on Apr 13, 2021 at 4:56pm
Apr 13, 2021 at 4:51pm
I agree it's not the best way -- it's just, you start to become conditioned to just expect students to not be able to use vector, unfortunately (even though something like a C# List/Java ArrayList would be perfectly acceptable, in any other language; C++ gets special unfair treatment).

Actually, a lot of the time I assume that the instructor expects students to use VLAs, even though they aren't standard and don't even compile in msvc.
Last edited on Apr 13, 2021 at 4:54pm
Apr 13, 2021 at 4:54pm
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
#include <iostream>
#include <algorithm>

int main()
{
	constexpr size_t MAXARR {200};

	int userArray[MAXARR] {};
	size_t arraySize {};

	std::cout << "\nThis program creates an array of various numbers and will sort them.\n";
	std::cout << "\nHow large would you like the array (between 1 and " << MAXARR << ") ? ";
	std::cin >> arraySize;

	if (arraySize > MAXARR) {
		std::cout << "Maximum size is " << MAXARR << '\n';
		arraySize = MAXARR;
	}

	std::cout << "Please enter " << arraySize << " numbers:\n";

	for (size_t i = 0; i < arraySize; ++i)
		std::cin >> userArray[i];

	std::sort(userArray, userArray + arraySize);

	std::cout << "\nThe sorted numbers are:\n";

	for (size_t i = 0; i < arraySize; ++i)
		std::cout << userArray[i] << ' ';

	std::cout << '\n';
}

Apr 13, 2021 at 5:01pm
here is the code i have currently, the only error it says it has is the i on line 19

"expression must have pointer to object type"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() 
{
	int arraySize{};
	int userArray{};

	cout << "\nThis program creates an array of various numbers and will sort them." << endl;

	cout << "\nHow large would you like the array? ";
	cin >> arraySize;

	userArray = arraySize;
	const int userArray{};


	//make user input values
	for (int i = 0; i < arraySize; ++i)
	{
		cout << "Please input an integer." << endl;
		cin >> userArray[i];
	}

}
Last edited on Apr 13, 2021 at 5:02pm
Apr 13, 2021 at 5:25pm
Please read my first post again. Perhaps I wasn't clear, so I will reiterate more succinctly.

int userArray{};

const int userArray{};
(1) You are re-declaring the userArray variable. This is not allowed.
(2) This is not an array, but an int.

An array would look something like:
int userArray[1000];
Apr 13, 2021 at 5:29pm
This is pretty good, you seem to understand the concepts. Just pay close attention to your (syntactic) spelling.

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
#include <iostream>
using namespace std;
int main() 
{
	int arraySize{};
        // wrong: can't make an array of size arraySize
        //   until you know what the size will be.  
        //   Don't declare it until you have the size.
	// int userArray{}; 

	cout << "\nThis program creates an array of various numbers and will sort them." << endl;

	cout << "\nHow large would you like the array? ";
	cin >> arraySize;
        // Now the size is known. Declare an array of 
        // arraySize ints:
        int userArray[arraySize] {}; 

        // wrong: see above
	// userArray = arraySize;
        // const int userArray{};
	
	//make user input values
	for (int i = 0; i < arraySize; ++i)
	{
		cout << "Please input an integer." << endl;
		cin >> userArray[i];
	}
}
Last edited on Apr 13, 2021 at 5:30pm
Apr 13, 2021 at 5:31pm
Yeah that's a better way of putting it. Let us know if the above code compiles (assuming you're using GCC/clang/MinGW).
Apr 13, 2021 at 6:05pm
will this still work if the user is choosing the size of the array?
Apr 13, 2021 at 6:37pm
If you can compile it, and run it without it crashing, that is good evidence that it works*. Of course, if you enter a huge number, or a negative number, then bad things might happen.

*"working" is not the same thing as "correct", but it's a good enough approximation for this particular thread.
Last edited on Apr 13, 2021 at 7:18pm
Apr 16, 2021 at 3:19am
When I compile this code, it says there's an error in the array. says needs constant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() 
{

	int arraySize;

	cout << "\nThis program creates an array of various numbers and will sort them." << endl;

	cout << "\nHow large would you like the array? ";
	cin >> arraySize;
	
	int userArray[arraySize]{};

	//make user input values
	for (int i = 0; i < arraySize; ++i)
	{
		cout << "Please input an integer." << endl;
		cin >> userArray[i];
	}

}
Apr 16, 2021 at 8:56am
This is effectively the same code as post #1 for which Ganado stated "Having array lengths that aren't known at compile-time is not standard".Don't you read/understand the replies provided?

Let me repeat -

YOU CANNOT SPECIFY AN ARRAY SIZE AT RUN TIME. THE SIZE MUST BE KNOWN AT COMPILE TIME.

Some compilers allow this, but THIS IS NOT STANDARD
Apr 16, 2021 at 9:53am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main() 
{
	int arraySize;
	cout << "\nHow large would you like the array? ";
	cin >> arraySize;
	int * userArray = new int[arraySize]{};     // <***** not constant, so you need dynamic memory; i.e. "new"

	for ( int i = 0; i < arraySize; ++i )
	{
		cout << "Please input element " << i << ": ";
		cin >> userArray[i];
	}
	
	// ... Do something with your array
	
	delete [] userArray;                        // <***** nobody else can use it 'til you give it back
}
Apr 16, 2021 at 12:02pm
If you really want a run-time sized array and don't/wouldn't use std::vector, then prefer std::unique_ptr (via std::make_unique) rather than new/delete.

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

int main()
{
	size_t arraySize {};

	std::cout << "How large would you like the array? ";
	std::cin >> arraySize;

	const auto userArray {std::make_unique<int[]>(arraySize)};

	for (size_t i = 0; i < arraySize; ++i) {
		std::cout << "Please input element " << i << ": ";
		std::cin >> userArray[i];
	}

	std::cout << '\n';

	for (size_t i = 0; i < arraySize; ++i)
		std::cout << userArray[i] << ' ';

	std::cout << '\n';

	// ... Do something with array
}

Last edited on Apr 16, 2021 at 12:03pm
Pages: 12