Spent most of tonight on this program, please check it out!!!

J

John

// this program stores video tapes by name in a
// linked list
// it stores the video tape's name, year, and time

#include<iostream>
#include<string>
using namespace std;

int totalnodes= 0; // the nodes in this case are tapes


template<class X>
class Lnode{
public:
X value;
Lnode * next;
};

template<class Y>
class linkedlist{
Lnode<Y> *head;
public:
linkedlist();
~linkedlist();
void addNode(Y &); // insert a video
void deleteNode(Y &);
void updateNode(Y &);
void displayNode()const;
void searchNode(const Y &);
};

class VideoTape{

string name;
double time;
int year;
double price;
public:
VideoTape();
VideoTape(string);
~VideoTape();

friend istream& operator >>(istream &,VideoTape &);
friend ostream& operator <<(ostream &,const VideoTape &);

friend bool operator < (const VideoTape &, const VideoTape &);
friend bool operator > (const VideoTape &, const VideoTape &);
friend bool operator != (const VideoTape &, const VideoTape &);
friend bool operator == (const VideoTape &, const VideoTape &);

void changetime(double);
void changeyear(int);
void changeprice(double);
};


template<class Y>
linkedlist<Y>::linkedlist(){
head = NULL;
}

template<class Y>
linkedlist<Y>::~linkedlist(){
Lnode<Y> *nodePtr;
nodePtr = head;

while(nodePtr != NULL)
{
head = nodePtr->next;
delete nodePtr;
nodePtr = head;
}
}

template<class Y>
void linkedlist<Y>::addNode(Y &n){
Lnode<Y> * newnode, * nodePtr, * previousNode;


newnode = new Lnode<Y>;
newnode->value = n;

// Empty linked list

if(!head)
{
head = newnode;
newnode->next = NULL;
totalnodes++;

}
// not empty list
else
{
nodePtr = head;

// If head's value is greater and not equal to n
if(nodePtr->value > n && nodePtr->value != n){
previousNode = head;
head = newnode;
newnode->next = previousNode;
totalnodes++;
}

else{
// cycle through numbers while he pointer's value is less than n, and,
also
// while the pointer's value does not equal n

while(nodePtr != NULL && nodePtr->value < n && nodePtr->value !
= n)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// the while loop ends
// if the number is duplicate and the loop did not end at end of list
if(nodePtr != NULL)
if(nodePtr->value == n){
cout << "Tape name already exist" << endl;
cout << "Did not add tape" << endl;
return;
}

// if not then insert the newnode
previousNode->next = newnode;
newnode->next = nodePtr;
totalnodes++;
}
}
}


// Delete a node from the linked list
template<class Y>
void linkedlist<Y>::deleteNode(Y &n)
{
Lnode<Y> *nodePtr, *previousnode;

//empty linked list
if(!head)
{
cout << "empty list" << endl;
return;
}

//delete the first node
if(head->value == n){
nodePtr = head->next;
delete head;
head = nodePtr;
totalnodes--;
}
else
{
nodePtr = head;

while(nodePtr != NULL && nodePtr->value != n){
previousnode = nodePtr;
nodePtr = nodePtr->next;
}
// if there is no number in the list that matches
// then the nodePtr points to NULL

if(nodePtr == NULL)
return;

previousnode->next = nodePtr->next;
delete nodePtr;
totalnodes--;
}
}

template<class Y>
void linkedlist<Y>::updateNode(Y &n){
Lnode<Y> * nodePtr;
char choice;

double tmptime;
int tmpyear;
double tmpprice;

nodePtr = head;

if(!head){
cout << "The list is empty" << endl;
}
else{
while(nodePtr != NULL && nodePtr->value != n){
nodePtr = nodePtr->next;
}

if(nodePtr->value == n){
cout << nodePtr->value << endl;
cout << "Update the running time, year realeased, or
price" << endl;
cout << "a. Running time" << endl;
cout << "b. Year released" << endl;
cout << "c. Price" << endl;
cin.clear();
cin >> choice;
while(toupper(choice) != 'A' && toupper(choice) != 'B' &&
toupper(choice) != 'C'){
cout << "Invalid choice" << endl;
cout << "Please try again" << endl;
cout << "Update the running time, year realeased, or
price" << endl;
cout << "a. Running time" << endl;
cout << "b. Year released" << endl;
cout << "c. Price" << endl;
cin.clear();
cin >> choice;
}
switch(choice){
case 'A':
cout << "Enter the new running time" << endl;
cin >> tmptime;
nodePtr->value.changetime(tmptime);
break;
case 'B':
cout << "Enter the new Year released" << endl;
cin >> tmpyear;
nodePtr->value.changeyear(tmpyear);
break;
case 'C':
cout << "Enter the new Price" << endl;
cin >> tmpprice;
nodePtr->value.changeprice(tmpprice);
break;
}
}
else if(nodePtr == NULL){
cout << "The video tape does not exist in the list" <<
endl;
}
}
}



template<class Y>
void linkedlist<Y>::displayNode()const {

Lnode<Y> * nodePtr;

nodePtr = head;

// Empty list

if(!head){
cout << "The list is empty" << endl;
return;
}
else{

cout << "Total of video tapes:" << totalnodes << endl;
cout << "Name\t\tRunning Time\tYear Released\tPrice" << endl;

while(nodePtr != NULL){
cout << nodePtr->value;
nodePtr = nodePtr->next;
}
cout << endl;
}
}

template<class Y>
void linkedlist<Y>::searchNode(const Y &n){ // copyright by john cho
Lnode<Y> *nodePtr;

nodePtr = head;

// Empty list

if(!head){
cout << "The list is empty" << endl;
return;
}
else{
while(nodePtr != NULL && nodePtr->value != n){
nodePtr = nodePtr->next;
}

if(nodePtr->value == n)
cout << nodePtr->value;
else{
cout << "There is no such video tape in the list" << endl;
}
}
}




VideoTape::VideoTape(){
time = 0;
year = 0;
price = 0;
}

VideoTape::VideoTape(string n){
name = n;
time = 0;
year = 0;
price = 0;
}

VideoTape::~VideoTape(){
}


istream& operator >>(istream &in, VideoTape &vtape){
cout << "Enter Video Tape name" << endl;
in >> vtape.name;
cout << "Enter Running time" << endl;
in >> vtape.time;
cout << "Enter release year" << endl;
in >> vtape.year;
cout << "Enter price" << endl;
in >> vtape.price;

return in;
}



ostream& operator <<(ostream &out, const VideoTape &vtape2){
out << vtape2.name;
out << "\t\t";
out << vtape2.time;
out << "\t\t";
out << vtape2.year;
out << "\t\t";
out << vtape2.price;
out << endl;

return out;
}


bool operator < (const VideoTape &a, const VideoTape &b){
return a.name < b.name;
}

bool operator > (const VideoTape &a, const VideoTape &b){
return a.name > b.name;
}

bool operator != (const VideoTape &a, const VideoTape &b){
return a.name != b.name;
}

bool operator == (const VideoTape &a, const VideoTape &b){
return a.name == b.name;
}


void VideoTape::changetime(double t){
time = t;
}

void VideoTape::changeyear(int y){
year = y;
}

void VideoTape::changeprice(double p){
price = p;
}

int main(){

linkedlist<VideoTape> thelist;

VideoTape avideo;
char choice;
string tapename;


do{
cout << "Videotape SYSTEM" << endl;
cout << "a. Add" << endl;
cout << "b. Delete" << endl;
cout << "c. Update" << endl;
cout << "d. Display" << endl;
cout << "e. Search" << endl;
cout << "x. Exit" << endl;
cin >> choice;

while(toupper(choice) != 'A' && toupper(choice) != 'B' &&
toupper(choice) != 'C'
&& toupper(choice) != 'D' && toupper(choice) != 'E' &&
toupper(choice) != 'X'){
cout << "Invalid entry, Please try again" << endl << endl;
cout << "Videotape SYSTEM" << endl;
cout << "a. Add" << endl;
cout << "b. Delete" << endl;
cout << "c. Update" << endl;
cout << "d. Display" << endl;
cout << "e. Search" << endl;
cout << "x. Exit" << endl;
cin.clear(); // clear multiple letters in the cin stream
cin >> choice;
cout << endl;
}

if(toupper(choice) == 'A'){
cout << "Enter a video tape" << endl;
cin >> avideo;
thelist.addNode(avideo);
}
else if(toupper(choice) == 'B'){
cout << "Enter a video tape name" << endl;
cin.clear(); // clear the istream
cin >> tapename;
thelist.deleteNode(VideoTape(tapename));
}
else if(toupper(choice) == 'C'){
cout << "Enter a video tape name" << endl;
cin.clear(); // clear the istream
cin >> tapename;
thelist.updateNode(VideoTape(tapename));
}
else if(toupper(choice) == 'D')
thelist.displayNode();
else if(toupper(choice) == 'E'){
cout << "Enter a video tape name" << endl;
cin.clear();
cin >> tapename;
thelist.searchNode(VideoTape(tapename));
}

}while(toupper(choice) != 'X');

return 0;

}
 
L

Leor Zolman

You must have some sense of what aspects of the program are weak or not; if
you want a critique for something this large, you might consider giving us
some idea of what aspects, if any, you'd like comment on.

First, please make sure your long lines have not wrapped to the point of
rendering the code invalid. Limiting your source file line length to 72
colums is always a good idea. Your code wraps in the middle of the !=
operator, in a string, and in a few comments (I think it was).

(I wrote a little utility to find lines beyond a certain length, and used
it all the time when producing code for publication in my old CUJ articles.
If you'd like it, email me.)

After fixing those problems, it compiled. I ran it, and for the first
video, I put in a time of "1:20". This did not sit well with your program.

Later I went to "display" a video, and it seems the only way to get out of
the "display" option is to make some kind of edit change to the video. This
must be a new use of the word "display" I'm not familiar with ;-)

That's about as far as I went with it.
HTH,
-leor
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top