Dynamic arrays, classes, pointer: What am I missing?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  class NewProductClass {
public:
	string TypeNewProduct;
	
	int BuildNewProduct(const int 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.
Thanks for the clarification.

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include<string>
#include <memory>
#include <vector>
using namespace std;
struct Orders
{
    string m_product;
    double m_quantity;

    Orders (const string& product, const double& quantity) : m_product(product), m_quantity(quantity){}
    void print_Orders()const
    {
        cout<<"Item ordered: "<<m_product<<"; "<<"Quantity: "<<m_quantity<<"\n";
    }
};

int main()
{
    bool fQuit = false;
    vector<unique_ptr<Orders>>vec_up_Orders;
    while(!fQuit)
    {
        cout<<"1. Enter orders \n2.Quit \n";
        int choice;
        cin>> choice;
        switch(choice)
        {
            case 1:
            {
                cout<<"Enter product name: \n";
                string name;
                cin>>name;
                cout<<"Enter product quantity: \n";
                double quantity;
                cin>>quantity;
                vec_up_Orders.emplace_back(unique_ptr<Orders>(new Orders(name, quantity)));
                break;
            }
            case 2:
                cout<<"No further orders, thank you \n";
                fQuit = true;
                break;
            default:
                cout<<"Invalid choice, try again \n";
                break;
        }
    }

    for(auto& itr : vec_up_Orders)
    {
        itr->print_Orders();
    }
}

Sample Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

Last edited on
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.
Topic archived. No new replies allowed.