video linked list creating cerr commands

How do i make my code works so that if i insert a list of video and then try to insert another list with the exact same title, it prompts some cerr message to say i have already entered the same title?

the lookup command has the cerr message that works exactly fine as I want


this is what i have


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

main.cpp 

31   while (getline(cin, command))
 32   {
 33 
 34     if(command=="insert")
 35     {
 36 
 37       getline(cin, title);
 38       getline(cin, url);
 39       getline(cin, comment);
 40       cin >> length;
 41       cin >> rating;
 42       cin.ignore();
 43 
 44       myVideo = new Video(title, url, comment, length,rating);
 45 
 46       list.insert(myVideo);
 47       // list.print();
 48       counter++;
 49 
 50      if(list.lookup(title)->get_title() == myVideo->get_title())
 51       {
 52 
 53         cerr << "Title is already in the list" << endl;
 54       }
 55     }



 83     else if(command == "lookup")
 84     {
 85       getline(cin, title);
 86       Video *myVideo = list.lookup(title);
 87 
 88       if(myVideo!=NULL)
 89       {
 90         myVideo->print();
 91       }
 92       else
 93       {
 94         cerr << "Title" << " " << "<" << title << ">" << " " << "is not in list." <<endl;
 95       }
 96 
 97     }
 98 









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
videolist.cpp


 27 bool Vlist::insert(Video *video)
 28 {
 29   if(m_head==NULL)
 30   {
 31     m_head=new Node(video, NULL);
 32     return true;
 33   }
 34 

 39   if(m_head->m_video->get_title() >= video->get_title())
 40   {
 41     if(m_head->m_video->get_title() == video->get_title())
 42     {
 43 
 44       return false;
 45     }
 46 
 47     m_head=new Node(video, m_head);
 48     return true;
 49   }
 50 
 51   Node *ptr=m_head;
 52   while( (ptr->m_next!=NULL) && ( video->get_title()>ptr->m_video->get_title() ) )
 53     //checking if first title is bigger than last
 54   {
 55     if(( video->get_title() == ptr-> m_video->get_title() ) )
 56     {
 57       //error if same title
 58       return false;
 59     }
 60     ptr=ptr->m_next;
 61   }
 62   ptr->m_next=new Node(video, ptr->m_next);
 63 
 64   return true;
 65 }


 86 Video* Vlist::lookup(string title)
 87 {
 88   Node *ptr = m_head;
 89 
 90   while(ptr!=NULL)
 91   {
 92     if(title == ptr->m_video->get_title())
 93       return ptr->m_video;
 94     ptr = ptr->m_next;
 95   }
 96 
 97   return NULL;
 98 }
 99 


I'm not really sure what the issue actually is, but perhaps you only want to insert into the list after you've done the lookup and noted the video doesn't already exist?
Topic archived. No new replies allowed.