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>
usingnamespace std;
int main()
{
int arraySize;
constint 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;
constint userArray[arraySize];
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.
constint userArray;You can't have an uninitialized const variable. It has to be assigned some value.
int userArray = arraySize;constint 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.
constint 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.
#include <iostream>
#include <algorithm>
usingnamespace 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';
}
}
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.
#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 (constauto& a : userArray)
std::cout << a << ' ';
std::cout << '\n';
}
#include <iostream>
usingnamespace std; // <--- Best not to use.
int main()
{
int arraySize{}; // <--- ALWAYS initialize your variables.constint 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;
constint 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:
#include <iostream>
usingnamespace 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 = newint[arraySize];
// <--- Other code here.
delete[] userArray;
return 0; // <--- Not required, but makes a good break point.
}
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.
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.
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.
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;
constint userArray{};
//make user input values
for (int i = 0; i < arraySize; ++i)
{
cout << "Please input an integer." << endl;
cin >> userArray[i];
}
}
#include <iostream>
usingnamespace 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];
}
}
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.
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];
}
}
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
#include <iostream>
usingnamespace std;
int main()
{
int arraySize;
cout << "\nHow large would you like the array? ";
cin >> arraySize;
int * userArray = newint[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
}
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.
#include <iostream>
#include <memory>
int main()
{
size_t arraySize {};
std::cout << "How large would you like the array? ";
std::cin >> arraySize;
constauto 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
}