Michael Hamilton
Michael Hamilton

Reputation: 13

Storing data from a text file in an array of structures C++

I am attempting to read data from a text file into an array of structures. The first iteration of the for-loop reads and displays everything correctly until it reaches the Boolean value, and every iteration after that does not display as expected. Is the bool value at the end causing the entire rest of the file to be read incorrectly? Or perhaps an issue stemming from getline?

int main()
{
    groceryProduct inventoryDatabase[25];
    ifstream fin("inventory.txt");
    if (!fin)
    {
        cout << "File could not be located.";
    }
    string itemName;

    for (int index = 0; index < 25; index++)
    {
        getline(fin, inventoryDatabase[index].itemName, '\n');
        fin >> inventoryDatabase[index].itemNumber;
        fin >> inventoryDatabase[index].itemPrice;
        fin >> inventoryDatabase[index].membershipPrice;
        fin >> inventoryDatabase[index].payByWeight;

        cout << inventoryDatabase[index].itemName << endl;
        cout << inventoryDatabase[index].itemNumber << endl;
        cout << inventoryDatabase[index].itemPrice << endl;
        cout << inventoryDatabase[index].membershipPrice << endl;
        cout << inventoryDatabase[index].payByWeight << endl;
    }
    return 0;
};

The structure:

struct groceryProduct
{
    double itemPrice;
    double membershipPrice;
    double itemWeight;
    int itemQuantity;
    string itemNumber;
    string itemName;
    bool payByWeight;
};

The output:

Apple
P0000
0.85
0.8
204 (expected output of 'false' instead of 204)

Output for every iteration of loop after first iteration:

-9.25596e+61
-9.25596e+61
204

Thank you, and please let me know if you require any more information.

File:

Apple
P0000
.85
.80
false
Orange
P0001
.95
.85
false
Lemon
P0002
.65
.60
false

Upvotes: 0

Views: 1508

Answers (2)

CodeMouse92
CodeMouse92

Reputation: 6898

Here are a couple of things I see that may be causing your problem.

1) An array is not "magically filled" with data. You have an uninitialized array, meaning that the data inside of it does not yet exist. At all.

What you have to do to remedy this is to add a new instance of the struct to the array at the start of each loop iteration.

How'd I spot that? Good rule of thumb: if it's weird, it's memory-related. Make sure you've initialized everything.

2) I've seen weird things happen when you use getline and << next to each other. Is there a particular reason you are using getline over <<?

(I would need to re-research how to work around that weirdness. I used to hit it a lot in my C++ class way back when.)

3) What 1201ProgramAlarm said is absolutely correct.


Side note: Do NOT get into the habit of throwing double around because "I want to be able to arbitrarily throw a large value in there." It's a bad habit that wastes space, as double is twice as large as float.

Learn the difference between float and double - you will almost never need double outside of scientific situations, because it is for numbers with a LOT of decimal places. (That's oversimplifying it.) If you're using double over float all the time, you're using twice the memory you need - 32 bits per variable extra, in fact. It adds up. (And people wonder why modern programs need 8GB of RAM to do the same thing as their 100MB-RAM-using predecessors...)

Prices always have two (rarely three) decimal places, so float should fit that perfectly in all cases. Same with weights.

Upvotes: 0

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

You need to tell your stream that the bool values are text with

fin >> boolalpha >> inventoryDatabase[index].payByWeight

You're seeing garbage data after the first bool input because failbit gets set in the stream and no further inputs will work until it is reset. This results in you array's data staying uninitialized.

Upvotes: 2

Related Questions