User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,066 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,299 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4723 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

random-access files

  #1  
Sep 21st, 2005
Hi, I'm doing this programming exercise for my C++ class. I'm using Microsoft Visual C++. for some reason, the program wouldn't save my data. Can someone please take a look at my code and tell me what I did wrong?

/*
Chapter 12 HOMEWORK
Dung Tran
CS 116 C++ programming
Chapter 12 Programming Exercise #11 Inventory Program
Requirements:
	Write a program that uses a structure to store these datas in a file:
		- item Description
		- Quantity on hand
		- wholesale cost
		- retail cost
	The program should have a menu that allows the usess to perform the following tasks:
		- add new records to the file
		- display any record in the file
		- change any record in the file
*/
#include <iostream>
#include <fstream>
using namespace std;

// Declaration of InventoryItem structure
struct InventoryItem
{
	char desc[51];		//item description
	int qty;			//quantity on hand
	float wholesale;	//wholesale price
	float retail;		//retail price
};

//Function Prototypes
int menu();
void AddRecord(fstream &);
void DisplayRecord(fstream &);
void ChangeRecord(fstream &);

int main()
{
	int choice;
	fstream inventory ("Inventory.dat", ios::out | ios::binary);
	InventoryItem record = {" ", 0, 0.0};
	
	//writing the blank records
	for (int count = 0; count < 5; count++)
	{
		inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
	}

	//inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
	
	inventory.close();
	
	inventory.open("Inventory.dat", ios::out | ios::binary);

	do
	{
		choice = menu();
		switch (choice)
		{
		case 1: AddRecord(inventory);
			break;
		case 2: DisplayRecord(inventory);
			break;
		case 3: ChangeRecord(inventory);
			break;
		case 4: cout << "Exiting Program...\n\n";
		}
	}while (choice != 4);

	inventory.close();
	return 0;
	
}	

int menu ()
{
	int selection;

	cout << "What would you like to do?\n";
	cout << "1) add a new record to the file\n";
	cout << "2) view a record in the file\n";
	cout << "3) change a record in the file\n";
	cout << "4) Exit Program\n\b";
	cout << "Please enter 1, 2, 3, or 4: ";
	cin >> selection;

	while (selection< 1 || selection > 4)
	{
		cout << "Invalid Choice!!\n";
		cin >> selection;
	}
	return selection;
}

void ChangeRecord(fstream &file)
{
	fstream inventory ("Inventory.dat", ios::out | ios::binary);
	InventoryItem record;
	long recNum;

	// Get the record number of the desired record.
	cout << "Which record 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));

	// Display the record contents
	cout << "Description: ";
	cout << record.desc << endl;
	cout << "Quantity: ";
	cout << record.qty << endl;
	cout << "Wholesale Price: ";
	cout << record.wholesale << endl;
	cout << "Retail Price: ";
	cout << record.retail << endl;

	// Get the new record data.
	cout << "Enter the new data:\n";
	cout << "Description: ";
	cin.ignore();
	cin.getline(record.desc, 31);
	cout << "Quantity: ";
	cin >> record.qty;
	cout << "Wholesale Price: ";
	cin >> record.wholesale;
	cout << "Retail Price: ";
	cin >> record.retail;

	// 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));

	// Close the file.
	inventory.close();
}

void DisplayRecord(fstream &file)
{
	fstream inventory ("Inventory.dat", ios::out | ios::binary);
	InventoryItem record;
	long recNum;

	// Get the record number of the desired record.
	cout << "Which record would you like to open? ";
	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 << "Description: ";
	cout << record.desc << endl;
	cout << "Quantity: ";
	cout << record.qty << endl;
	cout << "Wholesale Price: ";
	cout << record.wholesale << endl;
	cout << "Retail Price: ";
	cout << record.retail << endl;

	//clear any error state
	if (file.fail())
		file.clear();

	//closing the file
	file.close();
}

void AddRecord(fstream &file)
{
	cout << "Please enter the information for the new data: \n";

	fstream inventory ("Inventory.dat", ios::out | ios::binary);
	InventoryItem record;
	
	//Info of the new data
	cout << "Description: ";
	cin.ignore();
	cin.getline(record.desc, 31);
	cout << "Quantity: ";
	cin >> record.qty;
	cout << "Wholesale Price: ";
	cin >> record.wholesale;
	cout << "Retail Price: ";
	cin >> record.retail;

	inventory.write(reinterpret_cast<char *>(&record),
		            sizeof(record));

	//closing the file
	file.close();
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: random-access files

  #2  
Sep 22nd, 2005
Nice to hear from you again. Looks like you've really improved!

Unfortunately, my schedule is much tighter these days, so I don't have the time to help you out explicitly, but perhaps this may help...

http://cplus.about.com/od/beginnerct.../aa051802a.htm

It's a simple tutorial for file I/O.
I hope it's helpful.
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: random-access files

  #3  
Sep 23rd, 2005
Originally Posted by Drowzee
Nice to hear from you again. Looks like you've really improved!

Unfortunately, my schedule is much tighter these days, so I don't have the time to help you out explicitly, but perhaps this may help...

http://cplus.about.com/od/beginnerct.../aa051802a.htm

It's a simple tutorial for file I/O.
I hope it's helpful.

WOW, you still remember me?!! :surprised I mean, there are tons of people on this website! :surprised . LOL. I guess I did ask a lot of questions.

It's nice of you to follow my progress. I'd hope that I'm better now. But I'm still only in 2nd semester of C++. Still have much to learn.

Anyway, thanks for the link. I just figured out my coding mistakes. But I'll take a look at the site. Attaining more knowledge is always good.

Thanks again
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC