Deleting records

D

Daz01

How do I delete records? Ive written a program that can add new records
and I can display what I've entered, but how do I select certain
records to delete?

Code below

#include <cstdlib>
#include <iostream>

using namespace std;

struct Boat
{
char BoatOwn [50];
char BoatName[50] ;
int TypeBoat;
int LengthBoat;
int DepthBoat;
Boat* next;
};

int main(int argc, char *argv[])
{
int menuOption; //stores the users entered menu choice

int LengthBoatTemp; //stores the boat length in metres
int DepthBoatTemp; //stores the boat depth in metres

int duration; //stores the duration in months
int rate;
char confirm; //Yes or No confirmation
int spaceUsed=0; //Space in marina

//stockStruct pointers don't point anywhere at moment
Boat* head = NULL;
Boat* current = NULL;
Boat* last = NULL;
Boat* currentTemp = NULL; //This is used to display records

while (menuOption!=4)
{

//The 4 option choices
cout << "\t\t\t\t1. Add New Booking." << endl;
cout << "\t\t\t\t2. Delete a Record." << endl;
cout << "\t\t\t\t3. Display all Booking Records." <<
endl;
cout << "\t\t\t\t4. Exit Program." << endl;
cout << "" << endl;
cout << "" << endl;
cout<<"\n";
cout<<"\t\t\t\tPlease select an option: ";
cin>>menuOption;

if (menuOption==1)
{
system ("CLS");
cout<<"\n\n\t\tBoat length: ";
cin>>LengthBoatTemp;
//If the boat is too big an error message appears
if (LengthBoatTemp>15)
{
cout<<"\t\tYour boat is too long for the marina!" <<
endl;
system("PAUSE");
system("CLS");
}
else
{
//checks to see if there is enough space in marina
currentTemp=head;
spaceUsed=0;

while (NULL!=currentTemp)
{
spaceUsed=spaceUsed+currentTemp->LengthBoat;
currentTemp=currentTemp->next;
}

if (150<(LengthBoatTemp+spaceUsed))
{
cout<<"\t\tThe Marina is full, sorry booking must
be cancelled" << endl;
system("PAUSE");
system("CLS");
}
else
{
cout<<"\t\tBoat Depth: "<<endl;
cin >> DepthBoatTemp;
if (DepthBoatTemp>5)
{
cout<<"The boat exceeds the maximum depth, we
can't complete the booking\n";
system("PAUSE");
system("CLS");
}

else
{
cout<<"\t\tEnter the length of your stay
(months): ";
cin>>duration;
rate=(10*LengthBoatTemp)*duration;
cout << "\n";
cout <<"\t\tTotal cost: " << rate << endl;
cout <<"\n";
cout <<"\t\t\tProceed with the booking? (y/n)
";
cin >> confirm;
system ("CLS");
if (confirm=='y'||confirm=='Y')

{
last=current;
current = new Boat;


cout << "\t\tEnter Owners Name: ";
cin>>current->BoatOwn;

cout<<"\t\tEnter Boat Name: ";
cin>>current->BoatName;

cout<<"\t\tWhat type of Boat?:" << endl;
cout << "\t\t\t1. Motor " << endl;
cout << "\t\t\t2. Sailing " << endl;
cout << "\t\t\t3. Narrow\n\n\t\t\t";
cout << "Choice ";
cin >> current->TypeBoat;
current->LengthBoat=LengthBoatTemp;
current->DepthBoat=DepthBoatTemp;
current->next=NULL;

if (NULL==head)
{
head=current; //make current pointer the
head
}

if (NULL!=last)
{
last->next = current; //make last point
at the new record
}
cout << "\n";
cout<<"\t\tYour booking has been
entered\n";
system("PAUSE");
system("CLS");
}
else
{
cout<<"Your booking couldn't be
completed\n"<<endl;

} //end of y or N
} //end of else boat depth
}//end of else marina length
} //end of else boat length
} //end of option1

else if (menuOption==2)
{

}
else if (menuOption==3)
{

currentTemp=head;
spaceUsed=0;

while (NULL!=currentTemp)
{
cout << "Owner of Boat : " <<
currentTemp->BoatOwn << endl;
cout << "Name of Boat : " <<
currentTemp->BoatName << endl;
cout << "Type of boat: " << currentTemp->TypeBoat
<< endl;
cout << "Length of Boat: " <<
currentTemp->LengthBoat <<" metres" << endl;
cout << "Depth of the Boat : " <<
currentTemp->DepthBoat << " metres" << endl;
cout << "\n";
spaceUsed=spaceUsed+currentTemp->LengthBoat;
currentTemp=currentTemp->next;
}

cout<<"Available Space left in the marina: " <<
(150-spaceUsed)<<" metres\n\n";
system("PAUSE");
system("CLS");
/* char buffer[180];
char lineName[20];
long shotPoint, x, y;


// Open the file
FILE *fp;

if ( (fp = fopen("marina.csv", "r")) == NULL )
{
cout << "Unable to open file program aborting"<<endl;
system("PAUSE");
return(-1);
}

while (!feof(fp))
{
fgets( buffer, 179, fp);
cout << "buffer = " << buffer << "\n";
// sscanf( buffer, "%16s%5ld%8ld%8ld", lineName, &shotPoint, &x, &y
);
// cout << "Owner = " << lineName << "\n";
// cout << "Boat Name = " << shotPoint << "\n";
// cout << "Length = " << x << "\n";
// cout << "Depth = " << y << "\n\n";
}

fclose(fp); */

}//end of option 3

else if (menuOption==4)
{
cout << "Exiting " << endl;
system("PAUSE");
return EXIT_SUCCESS;
}}}
 
D

dasjotre

Daz01 wrote:
< sniped irrelevant code >

#include <list>

struct Boat
{
bool operator == (Boat const & b) const;
};

std::list<Boat> boats;

Boat b;

// to add to the list
boats.push_back(b);

// to remove from the list
boats.remove(b);

appart from iostreams I can't see anything other than C code.
this is a C++ group

Also, post minimal code that ilustrates your problem.
 
L

Lionel B

How do I delete records? Ive written a program that can add new records
and I can display what I've entered, but how do I select certain
records to delete?

Code below

#include <cstdlib>
#include <iostream>

using namespace std;

struct Boat
{
char BoatOwn [50];
char BoatName[50] ;
int TypeBoat;
int LengthBoat;
int DepthBoat;
Boat* next;
};

[snip more code]

You appear to be attempting to design and implement your own (singly)
linked list. I suspect you are thus re-inventing a (probably rather
wobbly) wheel ;) I would strongly suggest that you don't attempt this - it
is difficult and very error-prone to design even a reasonably simple list
class - but rather use the standard C++ list, which implements all the
functionality you require (adding, deleting and referencing "records").

You'll find the standard list class template in the header file <list>. The
standard C++ container class templates take a bit of getting used to but it
is *very* worthwhile to do so - any time put into learning to use them
will be offset against the considerable effort of debugging your own
efforts. And it also makes your code far more maintainable and
comprehensible to others.

Good luck,
 
S

Salt_Peter

Daz01 said:
How do I delete records? Ive written a program that can add new records
and I can display what I've entered, but how do I select certain
records to delete?

Code below

Learn how to use a std::list, your program will take 10 minutes to
write instead of a month.

#include <list>

Record {
...
};

int main()
{
std::list< Record > records;
}

If you really, really want to reinvent the wheel, design a simple
node-based templated list. The node struct should be an embedded
struct. That way you can later pass any user-type as T. When you design
a container of any kind, make it work with a plain primitive integer
first. That alone still presents a challenge since managing pointers is
not guesswork and absolutely critical.

template< typename T >
class MyList
{
struct node_t
{
T t;
node_t(const T& t, node_t* p)
: p_prev(p), p_next(0) { }
node_t* p_prev;
node_t* p_next;
} *p_head, *p_tail;

MyList() : p_head(0), p_tail(0) { }
... and so on
};

Note, all members are initialized via constructors. Without exceptions.
If you break that rule, you'll fail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top