I am having a couple issues with the following code. Any help with understanding what I am doing wrong is worth its weight in gold rather than handing me the answer. My problem with the following code is I can Add a Flight to the database but it seems to infinite loop as soon as I hit the sentinel. Does anyone see something there that is causing this? And, if there is any input on how to improve this code its appreciated as well.

#include <stdlib.h>
#include <iostream.h>
#include <iomanip.h>
#include <assert.h>
#include <iterator.h>
#include <list.h>

template <typename NodeValueType>



ostream & operator<<(ostream & out, /*const*/ list <NodeValueType> & l)

{
if (l.empty())
   out << "list is empty" << endl;
else
   for (typename list<NodeValueType>::iterator j=l.begin(); j!=l.end(); j++)
         //"typename" declares what follows it to be interpreted as a type.
      out << *j << "  ";
out <<  "end of list" << endl;
return out;  //return the "left" operand so can chain "<<" operations together.
}

struct FlightType{
       int FlightNum;
       int FlightCap;
       };

ostream& operator<< (ostream& outstream, FlightType const &flight)
{
         outstream<<"Flight Number is:"<< flight.FlightNum<<".";
         outstream<<"Flight Capacity is:"<< flight.FlightCap<<endl;
return outstream;
}


int main(void)
{
    list<FlightType> flightList;
    int option;
    do
    {
        cout<<"Please choose your menu options/n"<<endl;
        cout<<"Choose 1 to add flight/n"<<endl;
        cout<<"Choose 2 to print flight/n"<<endl;
        cout<<"Choose 3 to delete flight/n"<<endl;
        cout<<"Choose 4 to exit/n"<<endl;
        cin>>option;
        
        switch(option)
        {
                      case 1:
                           for(;;)
                           {
                                   int FlightNum, FlightCap;
                                   cout << "Enter Flight Number";   cin >> FlightNum;
                                   cout << "Enter Flight Capacity"; cin >> FlightCap;
                                  
                                   if(FlightNum==-1000)break;
                                   FlightType flight;
                                   flight.FlightNum = FlightNum;
                                   flight.FlightCap = FlightCap;
                                   cout << "You just entered: "  <<flight<< endl;
                                   flightList.push_front(flight);
                           
                           }       
                      
                      case 2:
                           cout<<"/n The flights you just entered are:/n";
                           while(flightList.begin()!= flightList.end())
                           {
                           cout<<"The printed flight num is"<<*flightList.begin();
                           }
                           cout << "push Enter to quit\n"; cin.get(); cin.get();  
                           
                      case 3:
                           for(;;)
                           {
                           int FlightNum;
                           cin>>FlightNum;
                           if(FlightNum==-1000)break;
                           flightList.remove();
                           cout << "Removed Flight from Database";
                           }
                      
                      case 4:
                           
                           {
                           return 0;
                           }
        }
                                            
                      }while(option!=4);
                      
        }

Recommended Answers

All 10 Replies

Please edit your post to put your code in [code][/code] tags.

Your problem is that break; has a special meaning inside a switch. The general way a switch is written is with

switch (n) {
 case 1:
    // do stuff
    break;
 case 2:
    // do other stuff
    break;
 // etc
}

You need to put a break after every case, or else execution will just roll right into the next case. C++ is a weird language.

Also, your break statement that you currently have therefore only breaks from the switch, not from the while loop.

Thanks for the reply and it worked for case 1 but when I go to case 2 it loops. Also in case 3, my compiler Dev C gives me an undefined function error for flightList.remove() which is part of STL. Is there a header that Im missing for this to work?

case 2:
                           cout<<"/n The flights you just entered are:/n";
                           while(flightList.begin()!= flightList.end())
                           {
                           cout<<"The printed flight num is"<<*flightList.begin();
                           cout << "push Enter to quit\n"; cin.get(); cin.get();  
                           }
                           break; 
 case 3:
                           for(;;)
                           {
                           int FlightNum;
                           cin>>FlightNum;
                           if(FlightNum==-1000)break;
                           flightList.remove(); <----------/*
                           cout << "Removed Flight from Database";
                           }
                           break;

.remove doesn't take 0 parameters, it needs to know what it has to remove. So in your case you need to tell it which flight you want to remove. You can do this in two ways:

create a temp variable of type 'flight' and give it the number and cap of the flight you want to remove, then pass it to the remove() function.

Search trough your list until you encounter the flight with the inputted number and remove it

.remove doesn't take 0 parameters, it needs to know what it has to remove. So in your case you need to tell it which flight you want to remove. You can do this in two ways:

create a temp variable of type 'flight' and give it the number and cap of the flight you want to remove, then pass it to the remove() function.

Search trough your list until you encounter the flight with the inputted number and remove it

Okay , this is what I did as you've instructed. It makes sense but the compiler is giving me an error "no match for operator ==" . I am not sure what that means .

case 3:
                            for(;;)
                            {
                            int FlightNum, FlightCap;
                            cout<<"Please enter the Flight Number to be deleter"<<endl;
                            cin>>FlightNum;
                            cout<<"Please enter the Flight Cap"<<endl;
                            cin>>FlightCap;
                            if(FlightNum==-1000)break;
                            FlightType temp;
                            temp.FlightNum = FlightNum;
                            temp.FlightCap = FlightCap;
                            cout<<"Flight:"<<temp.FlightNum<<endl;
                            cout<<"With Seating Cap of:"<<temp.FlightCap<<"Have been removed"<<endl;
                            flightList.remove(temp);
                            }
                            break;

What line is the error on? It isn't in this code for as far as I can see

g++ tells me it's in the remove line.... the error actually happens in member function `void std::list<_Tp, _Alloc>::Remove`, with a no match for `operator==`
I'm probably way off, but I'm guessing std::list is trying to iterate over the list of FlightTypes, and since FlightType doesn't overload ==.....

Member Avatar for jencas

I'm probably way off, but I'm guessing std::list is trying to iterate over the list of FlightTypes, and since FlightType doesn't overload ==.....

You are right.

Oh, I'm sorry, I didn't see the for loop when considering the behavior of that break statement.

Hi, I can't figure out how to overload the operator FlightType with ==. Can you please show me how to accomplish this.
Thanks,


g++ tells me it's in the remove line.... the error actually happens in member function `void std::list<_Tp, _Alloc>::Remove`, with a no match for `operator==`
I'm probably way off, but I'm guessing std::list is trying to iterate over the list of FlightTypes, and since FlightType doesn't overload ==.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.