Jump to content
  • Advertisement

Archived

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

chris01

Efficiency

This topic is 5933 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

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; 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; jid << "\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
Just making things right in the fourm world

  

#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 {
p=&dets;
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 {
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
Sorry


  

#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
I recommend you rewrite your program in terms of classes and the STL. For example, you might write (crude implementation):


    
class record
{
public:
void setName(const string& name)
{
name_ = name;
}
string getName() const
{
return name_;
}
void setAddress(const string& address)
{
address_ = address;
}
string getAddress() const
{
return address_;
}
private:
string name_;
string address_;
};

ostream& operator<<(ostream& os, const record& rec)
{
return os << rec.getName() << " " << rec.getAddress();
}

istream& operator>>(istream& is, record& rec)
{
string line;
getline(is, line);

const int pos = distance(line.begin(), find(line.begin(), line.end(), ' '));

if(pos != 0)
{
string name = line.substr(0, pos);
string address = line.substr(pos +1);

rec.setName(name);
rec.setAddress(address);
}

return is;
}


This provides everything you need for adding records to STL containers and using them in conjunction with the standard streams (although the stream operations need more work). Rather than arrays, you should store your records in vectors, and you could then use various STL algorithms to implement the menu option functionality. For instance, reading a file of records into a vector of records would be a simple call to:

vector details;
ifstream in("details.txt");
copy(istream_iterator(in), istream_iterator(),
back_inserter(details)) ;


Similarly, copying all records to the standard output can be done in a single line using:

copy(details.begin(), details.end(),
ostream_iterator(cout, "\n"));


Again, initialising the vector of records is very simple with a call to vector::clear().

If you write your code in the right way, you will find that you can make use of Standard Library features rather than writing tons of your own supporting code, and you can then concentrate on the actual problem at hand. If you need to learn more about this, then getting hold of a copy of Accelerated C++ is a good idea.

[edited by - SabreMan on September 13, 2002 1:12:12 PM]

Share this post


Link to post
Share on other sites
Thanks for your reply - I''ll read up on your suggestions.

Appologies again for the 2 posts - it''s only the second time I''ve used this forum or any forum for that matter. I wasn''t intentionally trying to be rude.

Share this post


Link to post
Share on other sites
quote:
Original post by chris01
Appologies again for the 2 posts - it''s only the second time I''ve used this forum or any forum for that matter. I wasn''t intentionally trying to be rude.

That''s OK. You probably didn''t realise you can go back and edit your previous posts. See the "Edit" and "Quote" buttons on the top-right of each post? Check ''em out!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!