作为序言,我是编程新手。我正在写一个简单的购物清单程序,我有一个do-while循环菜单驱动程序。选择1时,它会提示用户输入4个数据,然后发送给我的Item Class构造函数以启动值。然后我将Item添加到List Class数组,然后它会返回菜单。当从菜单中选择3时,它应该显示列表但由于某种原因我得到了分段错误。我现在已经连续工作了2天,但无法理解(我的TA也不能)。我将数据硬编码到我的函数中以查看它们是否正常工作,我在显示列表,添加项目或删除项目方面没有任何问题。当我尝试使用菜单中的功能时,唯一的问题就出现了。请帮忙!!!
主要功能:
int main()
{
/*List sl1;
Item Item1("apple", "unit", 3, 2.00);
Item Item2("grapes", "Pounds", 4, 5.00);
Item Item3("banana", "unit", 6, 2.00);
Item Item4("pears", "ounces", 5, 1.00);
sl1.addItem(&Item1);
sl1.addItem(&Item2);
sl1.addItem(&Item3);
sl1.addItem(&Item4);
sl1.removeItem("banana");
sl1.removeItem("grapes");
sl1.removeItem("pears");
cout << "Total Price: " << sl1.totalPrice() << endl;
sl1.displayList();
*/
int menuChoice;
List sl;
do
{
cout << endl;
cout << "1. Add Item to Shopping List" << endl;
cout << "2. Remove Item from Shopping List" << endl;
cout << "3. Display Shopping List" << endl;
cout << "4. Quit" << endl << endl;
cout << "Please Select 1-4: ";
cin >> menuChoice;
if (menuChoice == 1)
{
string name;
string units;
int quantity;
double cost;
cout << endl;
cout << "Enter Item Name: " << endl;
cin.ignore(256, '\n');
getline(cin, name);
cout << "Enter Item Units (eg. Ounces, Cans): " << endl;
getline(cin, units);
cout << "Enter Number of Units Needed: " << endl;
cin >> quantity;
cout << "Enter Cost of Item Per Unit: " << endl;
cin >> cost;
Item Item1(name, units, quantity, cost);
sl.addItem(&Item1);
//sl.displayList();
}
else if (menuChoice == 2)
{
string name;
cout << "Please enter the name of the Item you wish to remove: " << endl;
getline(cin, name);
sl.removeItem(name);
}
else if (menuChoice == 3)
{
sl.displayList();
cout << "Total Price: " << sl .totalPrice() << endl << endl;
}
} while (menuChoice != 4);
return 0;
}
列表类:
List::List()
{
arrayEnd = 0;
for (int count = 0; count < 4; count++)
{
shoppingList[count] = NULL;
}
}
void List::addItem(Item* inputItem)
{
shoppingList[arrayEnd] = inputItem;
arrayEnd++;
}
void List::removeItem(string itemName)
{
int removedItem = 0;
for (int count = 0; count < arrayEnd; count++)
{
if (itemName == shoppingList[count]->getItemName())
{
shoppingList[count] = NULL;
removedItem = count;
}
}
for (int count = 0; count < arrayEnd; count++)
{
if (count > removedItem)
{
shoppingList[count - 1] = shoppingList[count];
shoppingList[count] = NULL;
arrayEnd = arrayEnd - 1;
}
}
}
double List::totalPrice()
{
double totalPrice = 0.0;
if (shoppingList[0] == NULL)
{
cout << "There are no items in the list" << endl;
}
else
{
for (int count = 0; count < arrayEnd; count++)
{
totalPrice += shoppingList[count]->getUnitPrice() * shoppingList[count]->getNumberToBuy();
}
}
return totalPrice;
}
void List::displayList()
{
if (shoppingList[0] == NULL)
{
cout << "There are no items in the list" << endl;
}
else
{
for (int count = 0; count < arrayEnd; count++)
{
cout << "Item: " << shoppingList[count]->getItemName() << endl;
cout << "Units: " << shoppingList[count]->getItemUnit() << endl;
cout << "Quantity: " << shoppingList[count]->getNumberToBuy() << endl;
cout << "Subtotal: " << shoppingList[count]->getUnitPrice()*shoppingList[count]->getNumberToBuy() << endl << endl;
}
}
}
答案 0 :(得分:3)
问题在于:
Item Item1(name, units, quantity, cost);
sl.addItem(&Item1);
Item1
位于if
块内,因此它将在块的出口处被破坏。因此,您添加了一个指向不再存在的对象的指针,以及bam,seg fault。从技术上讲,你已经离开了所谓的悬空指针。这是一个非常危险的野兽。
PS:如果我是你,我会改变我的TA,因为这是C ++用户应该注意的一些常见的C ++错误。