我试图使用strcpy_s
,但我有此错误:strcpy_s未处理的异常?
Unhandled Exception...
struct Item {
//Item's
int code; // Code
char* name[20];
int amount; //Amount in stock
int minAmount; //Minimum amount
float price; //Price
};
重要的线是开始,并与"@@@@@@@@@"
旁边的线。 (spot = 0
,name
字符串被接收,store
被初始化在main()
中)。
//add an item to store
void addItem(Item* store, int maxItems, int &numItems)
{
if (maxItems == numItems)
{
cout << "ERROR \n";
return;
}
int spot = numItems; // our item's spot in store[]
int code; // inputted code
//Item's attributes' input
cout << "enter code : \n"; //code
cin >> code;
store[spot].code = code; //Code
cout << "enter name : \n"; //Name
_flushall();
char* name = new char[20];
gets_s(name, 20);
numItems++; //forward the number of items
strcpy_s(*store[spot].name, 20, name); //Name UNHANDLED EXCEPTION @@@@@@@@@@@@@@@@@@@@@@@@@@@@
cout << "enter amount : \n"; //Amount in stock
do
{
cin >> store[spot].amount;
if (store[spot].amount < 0) //not negative
cout << "ERROR \n";
} while (store[spot].amount < 0);
cout << "enter minimum amount : \n"; //Minimum amount for orders
do
{
cin >> store[spot].minAmount;
if (store[spot].minAmount < 0) //not negative
cout << "ERROR \n";
} while (store[spot].minAmount < 0);
cout << "enter price : \n"; //Price
do
{
cin >> store[spot].price;
if (store[spot].price < 0) //not negative
cout << "ERROR \n";
} while (store[spot].price < 0);
}
更改'if(maxItems == numItems)'为'if(maxItems> = numItems)' – Rabbid76
该命令应检查我们是否已达到最大项目,所以我认为没关系......] – Yair
删除'*' - >'strcpy_s(store [spot] .name,20,name);' – BLUEPIXY