1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Any know how to solve this problem( using input and map container)

Discussion in 'C++' started by ITgirl, Jun 15, 2008.

  1. ITgirl

    ITgirl New Member

    Joined:
    Jun 15, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone,

    My assignment partner and i have been working on this for a while and we have hit a wall and we have been stuck for several hours. A couple of questions, please. 1) In the Airports class I am trying to get input data from a text file and put it inside the variables of an airport object that I'm going to create. 2) Then I'm going to try and store my Airport objects inside a map container with a key being the Airport code. This is what it looks like so far.

    This is my header file:

    Code:
       #ifndef AIRPORTS_
    #define AIRPORTS_
    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    #include <iterator>
    #include <map>
    #include"Flights.h"
    using namespace std;
    
    class Airports{
    
    
     std::istream& operator (std::istream&, Airport::Airport&);
     
       };
     
     #endif /*AIRPORTS_*/
     
    this is the CPP file:

    Code:
      
    #include <iostream>
    #include <string>
    #include "Airports.h"
          
         
          
        istream& Airports:: operator (istream& ins, Airport::Airports:: Airport& airport ){
      
      // Notice how this operator does not need to be 'friend' to Airport
      string s;
      int h, m;
    
      // Get Flight Code
      ins >> s;
      airport.setCode( s );
    
      // Get scheduled arrival time
      ins >> h;
      if (ins.get() != ':')
        {
        ins.setstate( ios::badbit );
        return ins;
        }
      ins >> m;
      airport.setMinimumConnect( (double)(h % 24) + (m / 60.0) );
    
      // Get the description for humans
      ins >> ws;
      getline( ins, s );
      airport.setDescription( s );
    
      return ins;
      } 
       
          
       // This is where the list of airline flight data is stored using airport code (to access airport information) 
    map<Airport::Airport> airport_flights;
    map<Airport,string> mymap;
      map<Airport,string>::iterator it;
    
    // How many data do we need to read?
    int num_flights;
    cin >> num_flights;
    cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
    
    // Read exactly that many
    copy_n(
      std::istream_iterator <Airport::Airport> ( cin ),
      num_flights,
      std::back_insert_iterator <Airport::Airport> ( airport_flights )
      );
    
    
    the above code doesn't compile.
    these are the classes that the Airports class is using. the code below does compile and works:

    Code:
        #include "Flights.h"
    
    
    		  //_____________Start Implementation of Flight class________________\\
    
    
    		  /*Start House-keeping functions  for Flight Class
    		   * Default constructor
    		   *
    		   */
    		   Flight:: Flight (){
    		   //debug statement
    		   cout<< "we are in the construtor with Default arguments\n";
    
    		   setOrigin("");
    		   setDestination("");
    		   setDepartureTime(0);
    		   setDuration(0) ;
    
    
    		 }
    
    		  /* Constructor for flight object
    		   *
    		   *
    		   */
    		   Flight::Flight(const string &orig ,const string &destin ,const string &dep, const string &dur ){
    			//debug statement
    		   cout<< "we are in the construtor with arguments\n";
    		 
    		   setOrigin(orig);
    		   setDestination(destin);
    		   setDepartureTime(dep);
    		   setDuration(dur);
    
    
    
    		 }
    
    		 /* copy constructor for flight object
    		   *
    		   *
    		   *
    
    		   FlightsTL:: Flight:: Flight(const Flight& rhs){
    
    
    
    		   } */
    
    		   /**destructor for flight object
    		   *
    		   *
    		   */
    		  Flight::  ~Flight(){
    
    		 }
    		   //end House-keeping functions \\
    
    
    	   //_____________Start other functions____________________________\\
    
    		   /* Equals operation
    		   *
    		   */
    		  Flight&  Flight::operator=(const Flight& rhs){
    
    			return *this;
    		 }
    
    		   /* get origin
    		   *
    		   */
    		 string  Flight::getOrigin() const{
    
    			  return org;
    		   }
    
    		   /* get destination
    		   *
    		   */
    		 string  Flight::getDestination()const{
    
    			  return dest;
    
    		   }
    
    		   /* get departure time
    		   *
    		   */
    		   string  Flight:: getDepartureTime()const{
    
    				return depTime;
    
    		   }
    
    		   /* get duration
    		   *
    		   */
    		   string  Flight:: getDuration ()const{
    
    			   return dura;
    
    		   }
    
    		   /* set origin
    		   *
    		   */
    		   void  Flight:: setOrigin(string t ){
    
    				  org = t;
    		   }
    
    		   /* set destination
    		   *
    		   */
    		   void  Flight:: setDestination(string s){
    
    				  dest =s;
    
    		   }
    
    		   /* set departure time
    		   *
    		   */
    		   void  Flight:: setDepartureTime(string c){
    
    			  depTime = c;
    			}
    
    		   /* get duration
    		   *
    		   */
    		   void  Flight:: setDuration(string d){
    
    				dura = d;
    		   }
    
    		   /* set all
    		   *
    		   */
    
    		   void  Flight:: setAll(string a,string b,string c,string d){
    
    				   setOrigin(a);
    				   setDestination(b);
    				   setDepartureTime(c);
    				   setDuration(d) ;
    		   }
    
    		   /** ToString to display flight information
    		   *
    		   */
    		   string  Flight:: toString()const{
    				  string s;
    				  //double depT = getDepartureTime();
    				 // double durA =  getDuration();
    				   // convert double to string
    				 // char deptStr[50];
    				 // sprintf(deptStr,"%g",depT);
    				  // convert to string
    				 // char durStr[50];
    				//  sprintf(durStr,"%g",durA);
    				  // print all Flight information
    				  s=getOrigin() +"\t"+ getDestination() + "\t"+  getDepartureTime() + "\t"+ getDuration() +"\n" ;
    					return s;
    			   }
    
    
    	//__________________end of Flight implementation_____________\\
    
    
    
    
    
    
    	//__________________Start of of FlightsTL implementation_____________\\
            //Start House-keeping functions
    		  /*Default constructor
    		  *
    		  */
    
    	   Airport:: Airport (){
    		setCode("");
    		setMinimumConnect(0);
    		setDescription("");
    
    
    		 }
    
    		  /* Constructor for Airport object
    		   *
    		   *
    		   */
    		 Airport:: Airport(const string& code, const double& minCon, const string& description){
    			 setCode(code);
    			  setMinimumConnect(minCon);
    			  setDescription(description);
    
    		  }
    
    		  /* copy constructor for Airport object
    		   *
    		   *
    		   */
    
    		  // Airport(const Airport& rhs);
    
    		   /**destructor for Airport object
    		   *
    		   *
    		   */
    		  Airport:: ~Airport(){
    
    
    
    		   }
    
    		   //end House-keeping functions\\
    
     //_________________Start of other Airport functions___________________________\\
    
    		   /* Equals operation
    		   *
    		   */
    		Airport&  Airport::  operator=(const Airport& rhs){
    
    					  return *this;
    
    		   }
    
    		   /* get minimum connection
    		   *
    		   */
    		  double Airport:: getMinimumConnectionTime()const{
    
    							return minConnect;
    
    		   }
    
    		   /* get Code
    		   *
    		   */
    		   string Airport:: getCode()const{
    				  return code;
    
    
    		   }
    
    		   /* get description
    		   *
    		   */
    		   string Airport:: getDescription()const{
    
    
    				  return description;
    
    
    		   }
    		   
    		   /*get number of Flights 
    		   *
    		   */
    		   int Airport:: getFlightsNum(){
                   
                   return FlightsNum;
                   
                   }
    
    
    		   /* set minimum connection time
    		   *
    		   */
    		   void Airport:: setMinimumConnect (double a){
    
    					 minConnect =a;
    
    		   }
    
    		   /* set code
    		   *
    		   */
    		   void Airport:: setCode (string b){
    						code = b;
    
    
    		   }
    
    		   /* set description
    		   *
    		   */
    		   void Airport:: setDescription (string c){
    
    					description = c;
    
    		   }
    
    
    
    		   /* set all
    		   *
    		   */
    
    		   void Airport:: setAll (string a,double b,string c){
    
    				   setMinimumConnect(b) ;
    				   setCode(a);
    				   setDescription(c);
    
    
    		   }
    
    
    		 void Airport::addFlight(const Flight& Flight){
    
    			 Flights.push_back(Flight);
    			 FlightsNum++;
    
    		 }
    
    
    		 /*add Flight
    		  *
    		  */
    		 void Airport:: addNewFlight(const string& origin , const string& destination,const string& depTime, const string& duration){
    
    			  Flights.push_back(Flight(origin,destination,depTime,duration));
    			  FlightsNum++;
    
    		   }
    
    			/** ToString to display all flights and thier information
    		   *
    		   */
    		 string  Airport:: toString(){
    					  vector<Flight>::iterator find;
    					  string str;
    
    					  if (getFlightsNum() == 0) {
    							return "Nothing to show no Flights" ;
    					 }
    
    				for(find = Flights.begin(); find != Flights.end(); find++){
    					  str+=(*find).toString();
    
    				   }
    			
    
    				  string s;
    				  double mCon = getMinimumConnectionTime();
    
    				   // convert double to string
    				  char mConStr[50];
    				  sprintf(mConStr,"%g",mCon);
    
    				  // print all Airport information
    				  s=getCode() +"\t"+ mConStr + "\t"+ getDescription() + "\n" ;
    				 return str + s;
    
    		   }
    
    
    
    	  //__________________end of FlightsTL Implementation______________\\
    
    
    
    
    	  //I'm thinking of adding remove(), and clear() and add via text
    
    
    Thanks for your help, everyone

    Sandy
     

Share This Page