help with using structures and inputting a file

I wrote the code to a program that originally took information saved in a text file and stored it in an array. My professor wants us to use the same program but now instead of using a parallel arrays to store the information, he would like us to store the information from the text file into a structure and use it in the program. what im having trouble with currently is understanding how to save the information from the text file line by line into the structure..
using namespace std;

const int PRODUCT = 100;

struct Item {
double plucode;
string name;
double incremint;
double price;
double storage;
};

double userPounds, userPrice, userDiscount, userTotal, userInput, userTotalPounds, userFinal;
int number = 0;
bool trueValue, falseValue;
char again, sign, done;
int i = 0;
int currentItem;

int main(int argc, char** argv) {

ifstream inputFile("Inventory.txt");

sign = 'y', 'Y', 'n', 'N';
again = 'y', 'Y', 'n', 'N';

while (!inputFile.eof()) {

inputFile >> Item[number].plucode >> Item[number].name >> Item number].incremint >> Item[number].price >> Item[number].storage;

number++;
}
//i know this part is wrong but it was me trying different things to see if i could get it right. our book doesnt provide much information on how to do this step

inputFile.close();
Last edited on
You've almost got it.
You declared your structure, but haven't created any storage for it.

You need to add the following line:
 
Item item[100];


Then in your input statement, you need to reference item, not Item.
 
inputFile >> item[number].plucode >> item[number].name >> item number].incremint >> item[number].price >> item[number].storage;


PLEASE USE CODE TAGS (the <> formatting button) when posting code.

okay thanks for your help and creating the storage worked but now im having another issue, later on in the code when im trying to use the structures it wont compile. I tried switching Item to item there too but that didnt work.

1
2
3
4
5
6
7
8
9
for (int i = 0; i < PRODUCT; i++) {
                    if (userInput == Item.plucode[i]) {
                        currentItem = i;
                    }
                }
                cout << "How many pounds of " << Item.name[currentItem] << " would you like?" << endl;
                cin >> userPounds;

                userPrice = Item.price[currentItem] * userPounds;
Last edited on
That last line should be:
 
userPrice = item[currentItem].price * userPounds;


The subscript applies to the structure, not the price.
man your a life saver. thanks for the help
Ive gotten the progam to run correctly. Lastly Im trying to take the information stored in the structure and output it into another file. but since its several lines of information how would i create a loop that would output the entire data structure until theres nothing left?

outputFile << item[number].plucode << " " << item[number].name << " " << item[number].incremint << " " << item[number].price << endl;

I know im missing something in this part of the code as well. It will compile however when i check the output file only 0's come up.
bump
1
2
3
for (int i = 0; i < itemsStored; i++)
    outputFile << item[i].plucode << " " << item[i].name << " "     
    << item[i].incremint << " " << item[i].price << endl;
Topic archived. No new replies allowed.