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;
}}}
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;
}}}