Here is the assignment:
Your goal is to write a program that displays a series of whole numbers from input in reverse order. Your program will prompt the user for the number of values in this list, which it will use as the size for a dynamic array declared after this prompt.
The array size is unknown, the value is the pointer, and the sub has to be assigned before the loop.
Here are the steps:
- Declare variables, but don't "allocate the memory the memory for the pointer yet". Do this after prompting the user to enter the values.
- Prompt user to enter the numbers of values to be listed. (There has to be a message for the user in case they enter a negative number). Then use the keyword
newfor the pointer. - Prompt the user to enter the values
- Display the values in reverse.
- Use the keyword
deletefor the dynamic array.
While I'm trying to run the program, the error was:
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
for(int sub = 0; sub < size; size--)
--------------------------------------^
error: lvalue required as decrement operand
for (int sub = 0; sub > size; size--)
------------------------------------------------------^
Also, I am not sure what the keyword new does.
#include <iostream>
using namespace std;
int main()
{
int size, array;
cout << "How many values would you like to enter? ";
cin >> array;
int value;
int *array = new int[size];
if (size > 0)
{
for (int sub = 0; sub < size; size++)
{
cout << "Enter value #" << size << ": ";
cin >> value;
}
while (size > 0);
}
else
{
while (size < 0)
{
cout << "Size must be positive." << endl;
cout << "How many values would you like to enter? ";
cin >> size;
}
}
cout << "Here are the values you entered in reverse order: \n";
for (int sub = size - 1; sub >= 0; size--)
{
cout << "Value #" << size << " :" << value << endl;
}
delete[] array;
return 0;
}
PS: I know size is supposed to be unknown, but I've encountered another error saying
storage size of ‘size’ isn’t known
So, I add numbers to avoid that error.
Edit:So I changed the code thanks to @MikeCAT, but this error said terminate called after throwing an instance of 'std::bad_array_new_length what(): std::bad_array_new_length. This was because I enter a negative number for the size, which was supposed to happen for the if statement. Also, I need the size to start at 1 after the user enters how many values they want to enter, but the size always starts at the number that was entered.
size[10]is out-of-range for 9-elementsize. Consider usingstd::vector.sizenot strictly fitting that is an other issue, but that is beside the point)newyou can look at: en.cppreference.com/w/cpp/language/new. It is a bit heavy but it should give a good idea on how dynamically allocated arrays/memory works inc++storage size of 'size' is unknown: You can NOT have variables on the stack (i.e. locally within a function), whose size is not known at compilation. Some compilers allow it but it is not standardc++. Again to do that you neednew.new int[size], does a couple of things. Allocates memory forsizenumber ofints. Returns the pointer to the start of the allocated memory (makes sure alignment is fine, initialises some time, etc.). This memory is not deallocated. You needdeletefor thatterminate called after throwing an instance of 'std::bad_array_new_length what(): std::bad_array_new_length.