I am trying to create a class that builds a dynamically allocated array to hold both a new product item and a quantity of that item. I just cannot seem to make it work. I also do not know how to be able to access that new array outside of the class. I an a new student to c++ can someone please give me some direction?
class NewProductClass {
public:
string TypeNewProduct;
int BuildNewProduct(constint n) {
//int k = 0;
for (int product = 0; product < n +1; product++) {
//QuantityNewProduct = new int [product];
// int k = k++;
int *QuantityNewProduct;
cout << endl << "Please type the name of the product you would like to see. : ";
for (int namesProduct = 0; namesProduct < n + 1; namesProduct++) {
cin >> QuantityNewProduct[namesProduct];
};//end namesproduct loop
for (int listNewProducts = 0; listNewProducts < n + 1; listNewProducts++) {
cout << "You wish to see " << n << " " << QuantityNewProduct[listNewProducts] << " for your next visit.";
};//end list loop
};// end for product loop
return 0;
}; // end BuildNewProduct Function
};// End NewProduct Class
Can you please explain, in simple English, what the BuildNewProduct() method seeks to do? I am sorry but after trying to decipher your code over several attempts I'm none the wiser. Just in plain words, what is it that you're trying to achieve with this method. And then let's see if this forum can help you or not. Thanks
That is probably a bad sign on my part. I want to ask the user to enter a product name and a quantity and repeat that process until the user specifies they are finished. With that information I want to create a array that automatically sizes itself to hold all the values i.e. Product names and quantities. I need this to be accessed by a arrow pointer from Main() instead of a dot separated object method. The above code was my attempt at some type of a loop to fill a array. But honestly I am having a hard time following understanding these techniques. This part of a assignment I have and I am stuck.
Below I've used std::unique_ptr instead of C-style pointers. You can do the same with C-style pointers if you wish but then you must remember to delete the objects pointed to before the pointers go out of scope. Please read, research and if something is still unclear then do come back here. Program uses C++11 features:
1. Enter orders
2.Quit
1
Enter product name:
Eggs
Enter product quantity:
12
1. Enter orders
2.Quit
1
Enter product name:
Milk
Enter product quantity:
5
1. Enter orders
2.Quit
2
No further orders, thank you
Item ordered: Eggs; Quantity: 12
Item ordered: Milk; Quantity: 5
Thank you for your help! This gave me some good direction. I will learn more about vectors and some of the techniques you used. Thank you for taking the time to provide a example.