having trouble with dynamic memory allocation

I am having trouble implementing dynamic memory allocation in to my program. I am trying to dynamically allocate memory for the products in my program. For some reason it will not compile. I keep getting these errors but can't find a way around it:
(65): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
(67): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
(119): note: see declaration of 'strcpy'

Any help will be greatly appreciated. Thank you in advance for any help.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  #include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
using namespace std;
const int MAX = 4;
// Customer class
class Customer
{
	string name;
	string address;
public:
	void setName(string);
	string getName();
	void setAddress(string);
	string getAddress();
};
// Function definitions of customer class
void Customer::setName(string name1)
{
	name = name1;
}
void Customer::setAddress(string address1)
{
	address = address1;
}
string Customer::getName()
{
	return name;
}
string Customer::getAddress()
{
	return address;
}


//main
int main()
{
	Customer a;
	string name;
	string address;
	cout << "Please enter your name." << endl;
	getline(cin, name);
	cout << "Please enter your address." << endl;
	getline(cin, address);
	cout << "hello " << name << ", please press enter when you are ready to move on to the list of products" << endl;
	cin.get();
	int total;
	//array for quantity
	int quantity[3];
	int i;
	for (i = 0;i < 3; i++)
		quantity[i] = 0;
	char selection = 'a';
	do
	{

		cout << "Please select from the three products below. Each item cost $1." << endl;
		//array for products
		char temp[100];
		char *products[MAX];
		cout << "enter the first product name" << endl;
		cin >> temp;
		int len = strlen(temp) + 1;
		char *newProduct = new char[len];
		strcpy (newProduct, temp);
		products[0] = newProduct;
		products[1] = "apples";
		products[2] = "bananas";
		products[3] = "exit";
		for (int i = 0; i < 4; ++i)
		{
			cout << "Option " << i + 1 << ": " << products[i] << endl;
		}
		cout << "Enter selection: ";
		// read user selection
		cin >> selection;


		switch (selection)
		{
		case '1':
			cout << "You selected - Apples." << endl;
			cout << "Please enter quantity. ";
			cin >> quantity[0];
			break;

		case '2':
			cout << "You selected - Bananas." << endl;
			cout << "Please enter quantity. ";
			cin >> quantity[1];
			break;

		case '3':
			cout << "You selected - Oranges." << endl;
			cout << "Please enter quantity. ";
			cin >> quantity[2];
			break;

		case 'E':
		case 'e':
			cout << "Order Summary:";
			break;

			//if  1, 2, 3 or E is not selected then go to the default case
		default: cout << "Invalid selection. Please try again.";
			// no break in the default case
		}
	} while (selection != 'E' && selection != 'e');

	cout << "\nYou ordered " << quantity[0] << " Apples" << " for a cost of $" << quantity[0] << endl;
	cout << "You ordered " << quantity[1] << " Bananas" << " for a cost of $" << quantity[1] << endl;
	cout << "You ordered " << quantity[2] << " Oranges" << " for a cost of $" << quantity[2] << endl;
	//using the quantity array to calculate total
	total = (quantity[0] + quantity[1] + quantity[2]);
	cout << "Your grand total is $" << total << endl;
	cout << "Thank you." << endl;
	

	cin.get();
	return 0;
}

(65): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
(67): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
(119): note: see declaration of 'strcpy'


http://stackoverflow.com/questions/32136185/difference-between-strcpy-and-strcpy-s

I keep getting these errors but can't find a way around it.

Did you read the error? When the compiler tells you to change strcpy to another name you MUST do it with obligation.
C4996 is not an error but a warning. It seems you treat in your project setting this warning as an error.

Something like this:
#pragma warning (error : 4996)

Or a compiler option: /we4996

See:

https://msdn.microsoft.com/en-us/library/thxezb7y.aspx
I did read the error and tried using strcpy_s and then it gave me another error: no instance of overloaded function "strcpy_s" matches the argument list. I did however figure it out I needed to add a second parameter for the size which I didn't know before hand. Thank you guys for the help.
Why are you allocating memory by hand at all? These are variable length strings. You should be using std::string.
I am doing an assignment for school and the assignment says "For the products, use an array of pointers to strings, and dynamically allocate space for each of the strings. To fill in the products array, read one product name from the user, check the length, and then allocate memory based on the length of the entered word."
Topic archived. No new replies allowed.