Hello,
I am prompted to write a program that take an array of user inputted integers and then prints them in reverse order. I have written the code below thus far, which is a combination of two set of code I had written earlier that worked just, although I do understand their connection may be faulty. I am quite lost on how to move forward from this problem. The exact instructions are below:
"Write a program that prompts the user for 10 integers and stores them in a vector.
Thanks so much for your help! Unfortunately, I seem to be running into an issue I had with my previous code; debugging is aborted and the program forces a breakpoint at the end of line 14, with a pop up that says "Unhandled exception at 0x0F53CAB6 (ucrtbased.dll) in Lab19.exe: An invalid parameter was passed to a function that considers invalid parameters fatal." How do I remedy this?
The vector.reserve() only allocates memory, it doesn't actually create any elements. You would need to use push_back() to actually add the element to the vector. The other options are to create the elements when you declare the vector or resize() the vector to the proper size.
Ended up with the same forced break point.. putzed around with the code a bit more and found a solution that worked! Thank you both so much for your input. The final code is as follows:
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int INTEGERS = 10;
vector<int> userArray(INTEGERS);
int i;
cout << "Enter " << INTEGERS << " integers: ";
for (int i = 0; i < INTEGERS; i++) {
cin >> userArray[i];
}
for (int i = INTEGERS - 1; i >= 0; i--) {
cout << userArray[i] << " ";
}