Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 09-21-2005, 07:53 PM   #1 (permalink)
Registered User
 
Join Date: Jul 2005
Posts: 16
OS: XP


random-access files

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?

Code:
/*
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();
}
karen_CSE is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 06:54 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85