Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

chris01

Efficiency 2

This topic is 5762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for the previous post - here it is again (scap the last one): Could somebody take a look at this code and tell me whether I''m using the most efficient methods. Also whenever I enter the EditRecord function and change an address that begins with a number, it truncates the first digit ie "120 Crap Close" becomes "20 Crap Close" ?? Thanks in advance.
  
#include "stdafx.h"

#define MAX_RECORDS 11

struct RECORD
{
	int id;
	char name[30];
	char address[30];
}details1[MAX_RECORDS], details2[MAX_RECORDS];

const char *filename = "details.txt";

void InitialiseDetails(RECORD *p, RECORD dets[MAX_RECORDS]);
void PrintDetails(RECORD *p, RECORD dets[MAX_RECORDS], int ans);
void EditRecord(RECORD *p, RECORD dets[MAX_RECORDS], int ans);
void SaveFile(RECORD *p);
void LoadFile(RECORD *p);

int main(int argc, char* argv[])
{
	RECORD *p1=details1;
	RECORD *p2=details2;
	int answer=-1;
	int x=0;

	while (x != 6)
	{
		cout << "\nMAIN MENU.\n\n";
		cout << "[1].Load A File From Disk.\n";
		cout << "[2].Print Records.\n";
		cout << "[3].Save A File To Disk.\n";
		cout << "[4].Initialise Details.\n";
		cout << "[5].Edit Record.\n\n";
		cout << "[6].QUIT.\n\n";
		cout << "Option: ";
		cin >> x;
		switch (x)
		{
		case 1:
			LoadFile(p1);
			break;
		case 2:
			cout << "\nPlease Enter Record To Print (99 all): ";
			cin >> answer;
			cin.clear();
			PrintDetails(p1,details1,answer);
			break;
		case 3:
			SaveFile(p1);
			break;
		case 4:
			InitialiseDetails(p1,details1);
			break;
		case 5:
			cout << "\nPlease Enter Record To Edit: ";
			cin >> answer;
			cin.clear();
			EditRecord(p1,details1,answer);
		case 6:
			break;
		default:
			cout << "You Have Input An Invalid Option";
		}
	}
	return 0;
}

void InitialiseDetails(RECORD *p, RECORD dets[MAX_RECORDS])
{
	for (int i=1; i<MAX_RECORDS; i++)
	{
		p=&dets[i];
		p->id=i;
		strcpy(p->name,"");
		strcpy(p->address,"");
	}
}

void PrintDetails(RECORD *p, RECORD dets[MAX_RECORDS], int ans)
{
	cout << "\n";
	if (ans > 10)
	{
		for (int j=1; j<MAX_RECORDS; j++)
		{
			p=&dets[j];
			cout << "ID: " << p->id << "\n";
			cout << "NAME: " << p->name << "\n";
			cout << "ADDRESS: " << p->address << "\n\n";
		}
	}
	else
	{
		p=&dets[ans];
		cout << "ID: " << p->id << "\n";
		cout << "NAME: " << p->name << "\n";
		cout << "ADDRESS: " << p->address << "\n\n";
	}
}

void EditRecord(RECORD *p, RECORD dets[MAX_RECORDS], int ans)
{
	p=&dets[ans];	
	cout << "\nNAME: ";
	cin.ignore();
	cin.getline(p->name,30);
	cout << "ADDRESS: ";
	cin.ignore();
	cin.getline(p->address,30);
}

void SaveFile(RECORD *p)
{
	FILE *pSaveFile;
	pSaveFile=fopen(filename,"wb");
	fwrite(p,sizeof(RECORD),MAX_RECORDS,pSaveFile);
	fclose(pSaveFile);
}

void LoadFile(RECORD *p)
{
	FILE *pLoadFile;
	pLoadFile=fopen(filename,"rb");
	fread(p,sizeof(RECORD),MAX_RECORDS,pLoadFile);
	fclose(pLoadFile);
}
  

Share this post


Link to post
Share on other sites
Advertisement
there are posts in the other one that you haven''t replied to... kind of rude i think... but then that''s just me.

discussion forum (note the word discussion)

Share this post


Link to post
Share on other sites

This topic is 5762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!