In C++ can you allocate an array without telling it how big the array is going to be? I am simulating a grocery checkout program and I was going to use a multidimensional to achieve this because the input needs to be as following:
“Please enter product number and quantity” 110111 4 (first is product number, second is quantity).
This below throws an error because of the second value
#include <iostream>
#include <fstream>
#include "checkout.h"
using namespace std;
int main()
{
Checkout check;
int choice;
int array_size = 0;
int max = 10;
int* product_info = new int[max][1];
do{
cout << "Please Enter Item Number and Quantity: ";
cin >> product_info[array_size][array_size];
}while(!choice == 0);
system("pause");
return 0;
}
EDIT * I realize the array cin is messed up in this example.
A two dimensional array does not appear to be very efficient for this task, the array would be very sparse.
You could dynamically grow it as needed, but only in one dimension (that you can choose as coding time), the other one would have to be permanently fixed, which is at best a half solution.
In your program you use one dimension that is declared to have bound
1. This means that allowed indexes would range only from0to0and thus that dimension would not really exist.So you simply need something like a one dimensional array (indexed by product ID; the quantity will be placed inside the array rather than as a second dimension to it); and in your application, a
vectorin place of the array (as indicated in comments) will handle the dynamic aspects (reallocation) for you so that you will not even have to care how big the vector is at the moment.The following is how you can declare the structure, except that the POS code (the ID of the item) would normally be too large to fit into an
intin a real application, and the quantity might not always be genuinely integral.