help with using structures and inputting a file

Oct 15, 2012 at 12:05am
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 Oct 15, 2012 at 12:13am
Oct 15, 2012 at 12:13am
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.

Oct 15, 2012 at 12:24am
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 Oct 15, 2012 at 12:24am
Oct 15, 2012 at 12:37am
That last line should be:
 
userPrice = item[currentItem].price * userPounds;


The subscript applies to the structure, not the price.
Oct 15, 2012 at 12:42am
man your a life saver. thanks for the help
Oct 15, 2012 at 1:24am
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.
Oct 15, 2012 at 2:10am
bump
Oct 15, 2012 at 4:00am
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.