Click here to Skip to main content
16,004,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: command prompt using c++ Pin
Hamid_RT30-Jul-06 3:22
Hamid_RT30-Jul-06 3:22 
GeneralRe: command prompt using c++ Pin
davidbr430-Jul-06 9:24
davidbr430-Jul-06 9:24 
Questioncopy executable during run Pin
davidbr429-Jul-06 12:15
davidbr429-Jul-06 12:15 
AnswerRe: copy executable during run [modified] Pin
Trollslayer29-Jul-06 12:50
mentorTrollslayer29-Jul-06 12:50 
GeneralRe: copy executable during run Pin
davidbr430-Jul-06 9:27
davidbr430-Jul-06 9:27 
GeneralRe: copy executable during run Pin
ThatsAlok30-Jul-06 22:04
ThatsAlok30-Jul-06 22:04 
GeneralRe: copy executable during run Pin
davidbr431-Jul-06 8:18
davidbr431-Jul-06 8:18 
QuestionNot Visual C++... new coder using Dev C++ Pin
newcoder333329-Jul-06 11:27
newcoder333329-Jul-06 11:27 
Fist Part
I am trying to work on a simple database project which needs to determine if a record is null. I am wanting to do this by using the function isNull() in the classes Personal and Student to determine that a record is null. Then overwrite the record to be deleted by a null record. A null record can be defined as having nonnumeric characters (a tombstone) in the first position of the SSN member.

Second Part
Then I would like to define the function remove() in the Database which should locate the position of a record to be deleted and overwrite it with a null record. Then after a session is finshed. I would like to invoke a database destructor which copies the nonnull records to a new data file, deletes the old data file, and renames the new data file with the name of the old data file.

Here is what I have so far... Any help with code, advice, or tips would be greatly appreciated...

************************ personal.h *********************
public:
Personal();
Personal(char*,char*,char*,int,long);
void writeToFile(fstream&) const;
void readFromFile(fstream&);
void readKey();
int size() const {
return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
}
bool operator==(const Personal& pr) const {
return strcmp(pr.SSN,SSN) == 0;
}
protected:
const int nameLen, cityLen;
char SSN[10], *name, *city;
int year;
long salary;
ostream& writeLegibly(ostream&);
friend ostream& operator<<(ostream& out, Personal& pr) {
return pr.writeLegibly(out);
}
istream& readFromConsole(istream&);
friend istream& operator>>(istream& in, Personal& pr) {
return pr.readFromConsole(in);
}
};

#endif

****************** personal.cpp *******************

#include <iostream>
#include "personal.h"

Personal::Personal() : nameLen(10), cityLen(10) {
name = new char[nameLen+1];
city = new char[cityLen+1];
}

Personal::Personal(char *ssn, char *n, char *c, int y, long s) :
nameLen(10), cityLen(10) {
name = new char[nameLen+1];
city = new char[cityLen+1];
strcpy(SSN,ssn);
strcpy(name,n);
strcpy(city,c);
year = y;
salary = s;
}

void Personal::writeToFile(fstream& out) const {
out.write(SSN,9);
out.write(name,nameLen);
out.write(city,cityLen);
out.write(reinterpret_cast<const char*="">(&year),sizeof(int));
out.write(reinterpret_cast<const char*="">(&salary),sizeof(int));
}

void Personal::readFromFile(fstream& in) {
in.read(SSN,9);
in.read(name,nameLen);
in.read(city,cityLen);
in.read(reinterpret_cast<char*>(&year),sizeof(int));
in.read(reinterpret_cast<char*>(&salary),sizeof(int));
}

void Personal::readKey() {
char s[80];
cout << "Enter SSN: ";
cin.getline(s,80);
strncpy(SSN,s,9);
}

ostream& Personal::writeLegibly(ostream& out) {
SSN[9] = name[nameLen] = city[cityLen] = '\0';
out << "SSN = " << SSN << ", name = " << name
<< ", city = " << city << ", year = " << year
<< ", salary = " << salary;
return out;
}

istream& Personal::readFromConsole(istream& in) {
char s[80];
cout << "SSN: ";
in.getline(s,80);
strncpy(SSN,s,9);
cout<<'['<<ssn<<']';
cout="" <<="" "name:="" ";
="" in.getline(s,80);
="" strncpy(name,s,namelen);
="" cout<<'['<<name<<']';
="" "city:="" strncpy(city,s,citylen);
="" cout<<'['<<city<<']';
="" "birthyear:="" in="">> year;
cout<<'['<<year<<']';
cout="" <<="" "salary:="" ";
="" in="">> salary;
cout<<'['<<salary<<']';
cin.ignore();
="" return="" in;
}
****************************="" student.h="" *********************

#ifndef="" student
#define="" student

#include="" "personal.h"

class="" student="" :="" public="" personal="" {
public:
="" student();
="" student(char*,char*,char*,int,long,char*);
="" void="" writetofile(fstream&)="" const;
="" readfromfile(fstream&);
="" int="" size()="" const="" {
="" personal::size()="" +="" majorlen;
="" }
protected:
="" char="" *major;
="" ostream&="" writelegibly(ostream&);
="" friend="" operator<<(ostream&="" out,="" student&="" sr)="" sr.writelegibly(out);
="" }
="" istream&="" readfromconsole(istream&);
="" operator="">>(istream& in, Student& sr) {
return sr.readFromConsole(in);
}
};

#endif
******************************** student.cpp ********************

#include <iostream>
#include "student.h"

Student::Student() : majorLen(10) {
Personal();
major = new char[majorLen+1];
}

Student::Student(char *ssn, char *n, char *c, int y, long s, char *m) :
majorLen(11) {
Personal(ssn,n,c,y,s);
major = new char[majorLen+1];
strcpy(major,m);
}

void Student::writeToFile(fstream& out) const {
Personal::writeToFile(out);
out.write(major,majorLen);
}

void Student::readFromFile(fstream& in) {
Personal::readFromFile(in);
in.read(major,majorLen);
}

ostream& Student::writeLegibly(ostream& out) {
Personal::writeLegibly(out);
major[majorLen] = '\0';
out << ", major = " << major;
return out;
}

istream& Student::readFromConsole(istream& in) {
Personal::readFromConsole(in);
char s[80];
cout << "Major: ";
in.getline(s,80);
strncpy(major,s,9);
return in;
}
***************************** database.h ************************
#ifndef DATABASE
#define DATABASE

template<class t="">
class Database {
public:
Database();
void run();
private:
fstream database;
char fName[20];
ostream& print(ostream&);
void add(T&);
bool find(const T&);
void modify(const T&);
friend ostream& operator<<(ostream& out, Database& db) {
return db.print(out);
}
};

#endif
*********************** database.cpp *************************
#include <iostream>
#include "personal.h"
#include "student.h"
#include "database.h"

template<class t="">
Database<t>::Database() {
}

template<class t="">
void Database<t>::add(T& d) {
database.open(fName,ios::in|ios::out|ios::binary);
database.seekp(0,ios::end);
d.writeToFile(database);
database.close();
}

template<class t="">
void Database<t>::modify(const T& d) {
T tmp;
database.open(fName,ios::in|ios::out|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
cin >> tmp; // overloaded >>
database.seekp(-d.size(),ios::cur);
tmp.writeToFile(database);
database.close();
return;
}
}
database.close();
cout << "The record to be modified is not in the database\n";
}

template<class t="">
bool Database<t>::find(const T& d) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
database.close();
return true;
}
}
database.close();
return false;
}

template<class t="">
ostream& Database<t>::print(ostream& out) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (true) {
tmp.readFromFile(database);
if (database.eof())
break;
out << tmp << endl; // overloaded <<
}
database.close();
return out;
}

template<class t="">
void Database<t>::run() {
cout << "File name: ";
cin >> fName;
cin.ignore(); // skip '\n';
database.open(fName,ios::in);
if (database.fail())
database.open(fName,ios::out);
database.close();
char option[5];
T rec;
cout << "1. Add 2. Find 3. Modify a record; 4. Exit\n";
cout << "Enter an option: ";
while (cin.getline(option,5)) {
cout<<'['<<option<<']';
if="" (*option="=" '1')="" {
="" cin="">> rec; // overloaded >>
add(rec);
}
else if (*option == '2') {
rec.readKey();
cout << "The record is ";
if (find(rec) == false)
cout << "not ";
cout << "in the database\n";
}
else if (*option == '3') {
rec.readKey();
modify(rec);
}
else if (*option != '4')
cout << "Wrong option\n";
else return;
cout << *this; // overloaded <<
cout << "Enter an option: ";
}
}

int main() {
Database<personal>().run();
// Database<student>().run();
return 0;
}
AnswerRe: Not Visual C++... new coder using Dev C++ Pin
Christian Graus29-Jul-06 13:59
protectorChristian Graus29-Jul-06 13:59 
QuestionDrawing graphics on dialog box--which way is best? Pin
ctroyp29-Jul-06 11:01
ctroyp29-Jul-06 11:01 
AnswerRe: Drawing graphics on dialog box--which way is best? Pin
Justin Tay29-Jul-06 11:08
Justin Tay29-Jul-06 11:08 
GeneralRe: Drawing graphics on dialog box--which way is best? Pin
ctroyp29-Jul-06 11:22
ctroyp29-Jul-06 11:22 
GeneralRe: Drawing graphics on dialog box--which way is best? Pin
Justin Tay29-Jul-06 11:54
Justin Tay29-Jul-06 11:54 
GeneralRe: Drawing graphics on dialog box--which way is best? Pin
ctroyp29-Jul-06 11:59
ctroyp29-Jul-06 11:59 
GeneralRe: Drawing graphics on dialog box--which way is best? Pin
Trollslayer29-Jul-06 12:51
mentorTrollslayer29-Jul-06 12:51 
QuestionHelp with Windows forms Pin
samaruf29-Jul-06 10:30
samaruf29-Jul-06 10:30 
AnswerRe: Help with Windows forms Pin
Justin Tay29-Jul-06 10:46
Justin Tay29-Jul-06 10:46 
Questionoffice look not supported in VC .NET 2005 Pin
giddy_guitarist29-Jul-06 8:19
giddy_guitarist29-Jul-06 8:19 
AnswerRe: office look not supported in VC .NET 2005 Pin
Justin Tay29-Jul-06 10:32
Justin Tay29-Jul-06 10:32 
GeneralRe: office look not supported in VC .NET 2005 Pin
giddy_guitarist29-Jul-06 21:29
giddy_guitarist29-Jul-06 21:29 
GeneralRe: office look not supported in VC .NET 2005 Pin
Justin Tay29-Jul-06 23:15
Justin Tay29-Jul-06 23:15 
QuestionLowerCase(char stringtoConvert) Pin
jon-8029-Jul-06 6:35
professionaljon-8029-Jul-06 6:35 
AnswerRe: LowerCase(char stringtoConvert) Pin
Maxwell Chen29-Jul-06 7:27
Maxwell Chen29-Jul-06 7:27 
Questionalloc memory, debug and release Pin
includeh1029-Jul-06 4:48
includeh1029-Jul-06 4:48 
AnswerRe: alloc memory, debug and release Pin
Maxwell Chen29-Jul-06 7:34
Maxwell Chen29-Jul-06 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.