Question???

Hey,
This is what the Function suppose to do??? But I dont understand few things. For instance, what is the purpose of "char filename[MAX_FILEMANE_LENGTH+1]". Do I have to write it???? If I were to exclude that thing, will my program still work?? Secondly, Why do they write "cin>>setw(MAX_FILEMANE_LENGTH +1)"???? I thought that setw command was used to give spacing between characters. Thirdly, it says "actualItems=0". Is it trying to count something??? and If yes, what is it trying to keep an count off???? This one is just for clarification ; "data[actualItems].itemID". So, its trying to read information from an array called data. Which is of structure type. Since, actualItems is defined to be 0. So, its going to read row 0 of itemID from a file???? Lastly, it says "if (actualItems==maxItems)"..but we dont know the exact value of maxItems and maxItems variable isnt even in using call by reference. So, how do we know the exact value of maxItems.


// Complete the function below. Given an array of "invoiceData" structures
// and the length of this array, it reads data from a file into this array.
// Your function should obtain the name of the file from the user. It should
// return true on success and false on failure (in the event of any kind of
// problem, have your function simply output an error message and return
// false). It should "return" (call by reference) the actual number of
// items for which data was obtained.

bool readInvoiceData (invoiceData data[], int maxItems, int &actualItems) {
const int MAX_FILENAME_LENGTH = 60;
char filename[MAX_FILENAME_LENGTH + 1];
char temp[ITEM_ID_LENGTH + 1];
ifstream fin;

cout << "Enter name of data file: ";
cin >> setw(MAX_FILENAME_LENGTH + 1) >> filename;

fin.open(filename);

if (fin.fail()) {
cout << "Unable to open file: " << filename << endl;
return false;
}

actualItems = 0;

for (;;) {

// try to read item id;
fin >> setw(ITEM_ID_LENGTH + 1) >> invoice[actualItems].itemID;
if (fin.fail()) { // read failed for some reason
if (fin.eof()) {
return true; // we're at the end of the file
}
cout << "Error while reading file." << endl;
return false;
}

if (actualItems == maxItems) {
cout << "Data file is too long." << endl;
return false;
}

//read remaining data
fin >> data[actualItems].quantity >> data[actualItems].unit_price;
if (fin.fail()) {
cout << "Error in reading file or unexpected end of file." << endl;
return false;
}
actualItems++;
}
}
filename is an array of type character of 61 elements
Yes - you have to write it
No - it won't work as intended without it ( or 61)
setw() sets the field width to take the filename (nothing to do with spaces)
yes actualItems is keeping count in the invoice and data arrays. It doesn't seem to have been declared as an int. It is incremented in 3rd last line. It is initialised to 0 just as i is in a for loop very often.
Presumably maxItems is what will fit on the invoice which should be set in main(). Doesn`t seem like much help.
so setw will allow the filename to be certain amount of characters????? And filename has to fit in that much given space. right????
Topic archived. No new replies allowed.