I have written the following program.
It collects inventory information and stores it in a file.
I need to write a separate program that will access that file and give me the totals of the information stored in it ie. (quantity,wholesalecost,retailcost). What I'm specifically having trouble with is how to read out the information in file and store it in to a local variable? Any help would be much appreciated!
Thanks,
-D

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;
const int DESC_SIZE = 31;  // Description size

// Declaration of InventoryItem structure
struct InventoryItem
{
   char desc[DESC_SIZE];
   int qty;
   double price;
   double retailprice;
   string dateadded;
};

void menufunction(int menu,InventoryItem &record, long recNum);
enum menu{addnew,display,edit};

int main()
{
   long recNum=0;           // To hold a record number
   InventoryItem record ;   // 
   int selection=0;			// int for enum. representation
	
 while(1)
 {
	 cout << "\n   Inventory Selection Menu\n";
	 cout << " ----------------------------\n";
	 cout << " #0 Add New Item to Inventory\n";
	 cout << " #1 Display Item Information\n";
	 cout << " #2 Edit Item Information\n\n";
	 cout << " Enter Selection 0-2: ";
	 cin >> selection;
	 //selection--;
	 cout << "\n";
	if (selection > 2)
		break;
	menufunction(selection,record,recNum);
 }
	cout << " That was not a Valid menu Option\n";

   return 0;
}
void menufunction(int menu,InventoryItem &record, long recNum)
{
	char again;// stores the y to continue collecing inventory items

	// Open the file in binary mode for input and output
   fstream inventory("Inventory.txt",ios::in | ios::out | ios::binary);

   // sets pointer to start writing at end of file 
   inventory.seekp(0,ios::end);

switch(menu)
   {
   case addnew	: 
	 {
		do
		{
		   cin.ignore();
		   cout << " Enter Description: ";
		   cin.getline(record.desc, sizeof record.desc);
		   cout << " Enter Quantity on hand: ";
		   cin >>record.qty;
		   cout << " Enter Wholesale cost: ";
		   cin >> record.price;
		   cout << " Enter Retail cost: ";
		   cin >> record.retailprice;
		   cout << " Enter Date added to Inventory: ";
		   cin >> record.dateadded;
	   
		   // Write the current contents of records
		  inventory.write(reinterpret_cast<char *>(&record),sizeof(record));

		  // prompt user to continue adding info.
		  cout << "\n Do you want to enter another Inventory Item? \n";
		  cout << " Press 'Y' to continue, or any key to return to menu: ";
		  cin >> again;
		  //cin.ignore();
		} while (again =='Y'|| again == 'y');
	 }
		break;
	   
   case display :  // Get the record number of the desired record.
	   {
		cout << " Which record do you want to view? \n";
		cout << " Enter numeber between 0-?: ";
		cin >> recNum;

		// Move to the record and read it.
		inventory.seekg(recNum * sizeof(record), ios::beg);
		inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

		// Display the record contents.
		cout << "\n        Item Information \n"; 
		cout << " ----------------------------\n";
		cout << " Description: ";
		cout << record.desc << endl;
		cout << " Quantity on hand: ";
		cout << record.qty << endl;
		cout << " Wholesale cost: ";
		cout << record.price << endl;
		cout << " Retail cost: ";
		cout << record.retailprice << endl;
		cout << " Date added to inventory: ";
		cout << record.dateadded << endl;
		cout << "\n\n";
	   }
		break;
	   
   case edit :	   // Get the new record data.
	   {
	    cout << " What record number do you want to edit? ";
		cin >> recNum;

		// Move to the record and read it.
		inventory.seekg(recNum * sizeof(record), ios::beg);
		inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

		cout << "\n      Current data.\n";
		cout << " ----------------------------\n";
		// Display the record contents.
		cout << " Description: ";
		cout << record.desc << endl;
		cout << " Quantity on hand: ";
		cout << record.qty << endl;
		cout << " Wholesale cost: ";
		cout << record.price << endl;
		cout << " Retail cost: ";
		cout << record.retailprice << endl;
		cout << " Date added to inventory: ";
		cout << record.dateadded << endl;

		
		cout << "\n      Enter the new data.\n";
		cout << " ----------------------------\n";
		cout << " Description: ";
		cin.ignore();
		cin.getline(record.desc, DESC_SIZE);
		cout << " Quantity on hand: ";
		cin >> record.qty;
		cout << " Wholesale cost: ";
		cin >> record.price;
		cout << " Retail cost: ";
		cin >> record.retailprice;
		cout << " Date added to inventory: ";
		cin >> record.dateadded; 

		 // Move back to the beginning of this record's position.
		inventory.seekp(recNum * sizeof(record), ios::beg);

		// Write the new record over the current record.
		inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
	   }
		break;
	   
   }
   // Close the file.
   inventory.close();
}
Attachments
// This program allows the user to enter, edit, or display a specific record.
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;
const int DESC_SIZE = 31;  // Description size

// Declaration of InventoryItem structure
struct InventoryItem
{
   char desc[DESC_SIZE];
   int qty;
   double price;
   double retailprice;
   string dateadded;
};

void menufunction(int menu,InventoryItem &record, long recNum);
enum menu{addnew,display,edit};

int main()
{
   long recNum=0;           // To hold a record number
   InventoryItem record ;   // 
   int selection=0;			// int for enum. representation
	
 while(1)
 {
	 cout << "\n   Inventory Selection Menu\n";
	 cout << " ----------------------------\n";
	 cout << " #0 Add New Item to Inventory\n";
	 cout << " #1 Display Item Information\n";
	 cout << " #2 Edit Item Information\n\n";
	 cout << " Enter Selection 0-2: ";
	 cin >> selection;
	 //selection--;
	 cout << "\n";
	if (selection > 2)
		break;
	menufunction(selection,record,recNum);
 }
	cout << " That was not a Valid menu Option\n";

   return 0;
}
void menufunction(int menu,InventoryItem &record, long recNum)
{
	char again;// stores the y to continue collecing inventory items

	// Open the file in binary mode for input and output
   fstream inventory("Inventory.txt",ios::in | ios::out | ios::binary);

   // sets pointer to start writing at end of file 
   inventory.seekp(0,ios::end);

switch(menu)
   {
   case addnew	: 
	 {
		do
		{
		   cin.ignore();
		   cout << " Enter Description: ";
		   cin.getline(record.desc, sizeof record.desc);
		   cout << " Enter Quantity on hand: ";
		   cin >>record.qty;
		   cout << " Enter Wholesale cost: ";
		   cin >> record.price;
		   cout << " Enter Retail cost: ";
		   cin >> record.retailprice;
		   cout << " Enter Date added to Inventory: ";
		   cin >> record.dateadded;
	   
		   // Write the current contents of records
		  inventory.write(reinterpret_cast<char *>(&record),sizeof(record));

		  // prompt user to continue adding info.
		  cout << "\n Do you want to enter another Inventory Item? \n";
		  cout << " Press 'Y' to continue, or any key to return to menu: ";
		  cin >> again;
		  //cin.ignore();
		} while (again =='Y'|| again == 'y');
	 }
		break;
	   
   case display :  // Get the record number of the desired record.
	   {
		cout << " Which record do you want to view? \n";
		cout << " Enter numeber between 0-?: ";
		cin >> recNum;

		// Move to the record and read it.
		inventory.seekg(recNum * sizeof(record), ios::beg);
		inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

		// Display the record contents.
		cout << "\n        Item Information \n"; 
		cout << " ----------------------------\n";
		cout << " Description: ";
		cout << record.desc << endl;
		cout << " Quantity on hand: ";
		cout << record.qty << endl;
		cout << " Wholesale cost: ";
		cout << record.price << endl;
		cout << " Retail cost: ";
		cout << record.retailprice << endl;
		cout << " Date added to inventory: ";
		cout << record.dateadded << endl;
		cout << "\n\n";
	   }
		break;
	   
   case edit :	   // Get the new record data.
	   {
	    cout << " What record number do you want to edit? ";
		cin >> recNum;

		// Move to the record and read it.
		inventory.seekg(recNum * sizeof(record), ios::beg);
		inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

		cout << "\n      Current data.\n";
		cout << " ----------------------------\n";
		// Display the record contents.
		cout << " Description: ";
		cout << record.desc << endl;
		cout << " Quantity on hand: ";
		cout << record.qty << endl;
		cout << " Wholesale cost: ";
		cout << record.price << endl;
		cout << " Retail cost: ";
		cout << record.retailprice << endl;
		cout << " Date added to inventory: ";
		cout << record.dateadded << endl;

		
		cout << "\n      Enter the new data.\n";
		cout << " ----------------------------\n";
		cout << " Description: ";
		cin.ignore();
		cin.getline(record.desc, DESC_SIZE);
		cout << " Quantity on hand: ";
		cin >> record.qty;
		cout << " Wholesale cost: ";
		cin >> record.price;
		cout << " Retail cost: ";
		cin >> record.retailprice;
		cout << " Date added to inventory: ";
		cin >> record.dateadded; 

		 // Move back to the beginning of this record's position.
		inventory.seekp(recNum * sizeof(record), ios::beg);

		// Write the new record over the current record.
		inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
	   }
		break;
	   
   }
   // Close the file.
   inventory.close();
}

I have written the following program.
It collects inventory information and stores it in a file.
I need to write a separate program that will access that file and give me the totals of the information stored in it ie. (quantity,wholesalecost,retailcost). What I'm specifically having trouble with is how to read out the information in file and store it in to a local variable?

I don't understand. You obviously know the format of the file and how it is written -- you created it. You know how to write the file in the proper format. Just reverse the write and make it a read.

By definition everything you read goes into a local variable.

heres what i did to finish this up..
It just took some thinking about..

case total : 
	   {
		   int count =0;
		   int qtotal=0;
		   double wholesaletotal=0;
		   double retailtotal=0; 
   			// Move to the begeining.
		   inventory.seekg(0, ios::beg);
		   while (1)
		   {
				// cout << "Now writing record " << count << endl;
		inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

			   if (inventory.eof())
			   {
				   break;
			   }
					qtotal += record.qty;
					wholesaletotal += record.price;
					retailtotal += record.retailprice;
					count++;
		   }
		   cout << " Total number of items in inventory:  " << qtotal << "\n";
		   cout << " Total wholesale value: " << wholesaletotal << "\n";
		   cout << " Total retail value: " << retailtotal << "\n";
		   cout << " Total number of inventory entries: " << count << "\n" ;
		   
	   }
This article has been dead for over six months. Start a new discussion instead.