So I'm trying to write a program that keeps an inventory for a store. The code that I'm struggling with is a function that is supposed to add a fish to the inventory. I have to use a dynamic array of structures for the assignment so I'm unable to use vectors. What happens when I run the code is after I input the name, the program just crashes. It does not throw an error or anything, just straight up crash.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void addFish(myFish newFish[], int& size)
{
cout << "\nAdding one fish to the inventory.\n";
size++;
cout << "What is the name of the fish?\n";
getline(cin, newFish[size].fishName);
cout << "Which tank is the fish in?\n";
cin >> newFish[size].tank;
cout << "What is the quantity of the fish?\n";
cin >> newFish[size].quantity;
cout << "What is the price of the fish?\n";
cin >> newFish[size].price;
}
Here is the struct of myFish:
1 2 3 4 5 6 7
struct myFish
{
string fishName;
int tank;
int quantity;
double price;
};
Okay, I'm just confused on how I would go about making sure the fish array has enough memory to store the new fish array. Since the new fish array is one larger than the original fish array.
1. When you create the array make it so big that you are sure it will be big enough to hold all the fishes that will be added to it.
2. When a new fish is added, allocate a new array with the size +1, copy all the old fishes to the new array, add the new fish to the new array and get rid of the old array (using delete[]).
So I've changed the program around so it doesn't crash anymore. I set up a constant for the maximum number of fish and now it actually lets you fill in information on the fish. However, now when I try to display the fish, it just shows the contents being 0.
For example, if this is the first fish you are adding:
What is the fish name?
Bob
Which tank is it in?
2
What is the quantity?
3
What is the price?
2.50
After I display, it would show:
Name:
Tank:0
Quantity:0
Price:$0
Here's my code for displaying:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void displayInventory(myFish newFish[], int& size)
{
cout << "The following is the inventory of fish: \n\n";
for (int i = 0; i < size; i++)
{
cout << "Index Number: " << i << endl;
cout << "Name: " << newFish[i].fishName << endl;
cout << "Tank: " << newFish[i].tank << endl;
cout << "Quantity: " << newFish[i].quantity << endl;
cout << "Price: $" << newFish[i].price << endl;
}
return;
}
I know you say you have to use an array instead of using a vector.
But you also say you have to use a dynamic array.
Peter87's second answer is the way to go then. But be warned. Creating and deleting arrays can easily lead to memory leeks. if i where you i would use either a map or a vector.
as for your new problem. try creating your array outside the functions and add a pointer to the parameters instead of a new array :)
1 2 3 4 5 6 7 8 9
void displayInventory(myFish& newFish[], int& size)
{
your code
}
void addFish(myFish& newFish[], int& size)
{
your code
}
this way you are parsing a pointer to an external array instead of creating a coppy.
I think that's your problem.