Help with struct Arrays and File Operations

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct Fish
{
    string fishName;
    int tank;
    int quantity;
    double price;
};

void showInventory(Fish [], int);
int menuFunc();



int main()
{
    //Variable definitions
    const int MAX_SIZE = 100;   // The maximum array size
    int arraySize = 0;          //Holds the number of elements in the array
    const int 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);

1
2
3
4
5
6
7
8
9
10
11
void showInventory(Fish tempArray[], int arraySize)
{
	for (int count = 0; count < arraySize; count++)
	{
		cout << tempArray[count].fishName << endl;
		cout << tempArray[count].tank << endl;
		cout << tempArray[count].quantity << endl;
		cout << tempArray[count].price << endl;
	}
	
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Veiltail Betta
5
38
2.23
Fancy Guppies
2
100
1.59
Red Cap Oranda Goldfish
2
25
6.39
Twinbar Solar Flare Swordtail
1
75
3.99
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.


Topic archived. No new replies allowed.