So I posted earlier but almost completely reworked my code. The problem is that the function that the Fish struct array is passed to only correctly displays the values from first iteration of the file. I suspect that this has to do with the loop I use to initialize the struct array (with values from the file) but I am at a loss here. Let me know what you guys think?
By the way, running the program looks like this:
------
Lets see if the function works!
Veiltail Betta
5
38
2.23
0
3
9.2423e-222
-------
Here is the main part of the file, I will post the function and text file I am reading from below as well.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
usingnamespace std;
struct Fish
{
string fishName;
int tank;
int quantity;
double price;
};
void showInventory(Fish [], int);
int menuFunc();
int main()
{
//Variable definitions
constint MAX_SIZE = 100; // The maximum array size
int arraySize = 0; //Holds the number of elements in the array
constint EXIT = 9; //Menu choice to quit program
int menuChoice; //Holds menu selection
ifstream arrayInit;
Fish myInventory[MAX_SIZE]; //Declaration of array of Fish structs
arrayInit.open("inventory.dat");
while (arrayInit)
{
getline(arrayInit, myInventory[arraySize].fishName);
arrayInit >> myInventory[arraySize].tank;
arrayInit >> myInventory[arraySize].quantity;
arrayInit >> myInventory[arraySize].price;
arraySize++;
}
cout << "Lets see if the function works!\n";
showInventory(myInventory, arraySize);
Switching between the extraction operator<< and getline() will often cause problems. The extraction operator is probably leaving the end of line character in the buffer which confuses getline() try ignoring everything after the last input, until you encounter the new line character.