So i have been working on this code for a while, and I am not very skilled. I have created two classes one defines an item for a shopping list, and the other creates an array of the objects and outputs them as a list. The program works in my windows command prompt, but segfaults on GNU command line when I am outputting the array using cout statements.
void List::createList(Item ** itemPtr, int size)
{
string** list1 = new string*[size];
for(int i = 0; i < 5; i++)
{
list1[i] = new string[5];
}
for (int i = 0; i < size; i++)
{
list1[i][0] = itemPtr[i]->getName();
list1[i][1] = itemPtr[i]->getUnit();
list1[i][2] = itemPtr[i]->getSTRnumToBuy();
list1[i][3] = itemPtr[i]->getSTRcost();
list1[i][4] = itemPtr[i]->getSTRextCost();
}
cout << endl << left << fixed << setw(15) << setprecision(2) << "Name";
cout << fixed << left << setw(15) << setprecision(2) << "Unit Type";
cout << fixed << left << setw(15) << setprecision(2) << "# of units";
cout << fixed << left << setw(15) << setprecision(2) << "Cost/Unit";
cout << fixed << left << setw(15) << setprecision(2) << "Total" << endl;
for (int i = 0; i < size; i++)
{
cout << fixed << left << setw(15) <<setprecision(2)<<endl<< list1[i][0];
cout << fixed << left << setw(15) << setprecision(2) << list1[i][1];
cout << fixed << left << setw(15) << setprecision(2) << list1[i][2];
cout << fixed << left << setw(15) << setprecision(2) << list1[i][3];
cout << fixed << left << setw(15) << setprecision(2) << list1[i][4];
}
}
newing a standard container usually means you are doing something wrong. Why not use vectors of strings and avoid the headaches?