Open Side Menu Go to the Top
Register
SenorKeeed's adventures in C++ SenorKeeed's adventures in C++

04-11-2013 , 01:28 PM
OK, sorry for cluttering up the LC thread, if anyone can help me in my struggles to learn C++ I would be much obliged.

So, the function below is a function of the Airline has as its input a pointer to the Flight class. Airline has two data fields, ptrFlights, an array of pointers to Flight objects, and numberFlights, a running tally keeping track of how many Flight objects have been added to the ptrFlights array.

So my problem is with lines 42-46. All the cout lines are just me trying to keep track of what is going on, the else statement is just the bolded two lines:

cout << numberFlights << endl;
ptrFlights[numberFlights]=newFlight;
cout << numberFlights << endl;
numberFlights=numberFlights+1;
cout << numberFlights << endl;

When I run the code the output of this is something like this. The huge number is always different, then incremented by one

0
1360869592
1360869593

WTF? isn't line 43 me taking the numberFlights element of ptrFlights and assigning it to newFlight? How is it assigning numberFlights some huge value when I do that?


Last edited by SenorKeeed; 04-11-2013 at 01:36 PM.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 03:51 PM
I can't see lines 1-16 on my phone. Looks like your saving newFlight object ptr is clobbering the number of flights variable. Post the statement that allocates the ptr array.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:00 PM
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:13 PM
Can't see anything obvious. Comment out

ptrFlights[0] = newFlight;

Then see what the output is.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:19 PM
Oh I see the problem. You've declared the array in two places. One ad a member of the class and another as an automatic variable in your constructor.
Scope rules got ya. Just delete the declaration in your constructor.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:26 PM
Where else did I declare it besides line 12?
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:29 PM
In your. h file.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:32 PM
Where do you declare numberFlights?

There are ways to paste code in instead of screenshotting it.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:36 PM
Quote:
Originally Posted by jmark
Where do you declare numberFlights?

There are ways to paste code in instead of screenshotting it.
In his .h file
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:37 PM
I see, just like I don't put int in front of numberFlights. But how do I make the array dynamically allocated then? In the h file? What would the syntax look like?
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:39 PM
And what's the best way to post code, sorry for the screenshots
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:42 PM
Just delete Flight ** in your constructor.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 04:45 PM
Paste it in with code tags around it (go advanced and click the pound sign)
SenorKeeed's adventures in C++ Quote
04-11-2013 , 07:12 PM
Keep posting your questions when issues come up.
SenorKeeed's adventures in C++ Quote
04-11-2013 , 07:33 PM
Thank you, I definitely will
SenorKeeed's adventures in C++ Quote
04-11-2013 , 08:11 PM
Use bool for flightIsThere. I know they are kinda the "same" in C++ but bool is more self documenting and less likely to cause subtle issues. The variable clearly represents a boolean value so make it explicit.

Dynamic allocation of the array should be done just once, in the constructor. I'd prefer using the initilizer list to allocate the memory. Then create a function to zero all the pointers. Also should keep track of the array capacity instead of hard coding it.

Better yet, use vector
SenorKeeed's adventures in C++ Quote
04-12-2013 , 04:32 PM
Quote:
Originally Posted by adios
Just delete Flight ** in your constructor.
So just deleting the Flight **

ptrFlights=new Flight*[100];

gives me this error message:

Airline.cpp:12: error: incompatible types in assignment of ‘Flight**’ to ‘Flight* [0u]

Here's my h file and cpp file. I really don't understand all this array of pointers to pointers stuff. I've gone through the book like ten times and there's no examples that seem relevant. It's very frustrating.

Code:
#ifndef AIRLINE_H
#define AIRLINE_H

#include <string>
using namespace std;

class Airline {
 private:
  Flight *ptrFlights[];
  int numberFlights;
  public: 
  Airline();
  void addFlight(Flight *);
  void removeFlight(Flight *);
  bool checkAvailability(string myDeparture, string myDestination);
  int makeRSVP(string  myDeparture, string myDestination, string passengerName);
  bool cancelRSVP(string myDeparture, string myDestination, int passengerSeatNumber);
  int sellTicket(string myDeparture, string myDestination, string passengerName);
  void printAllFlights();
};
#endif
Code:
#include <string>
#include "Seat.h"
#include "Flight.h"
#include <iostream>
#include "Airline.h"

using namespace std;

Airline::Airline()
{
  numberFlights=0;
  ptrFlights=new Flight*[100]; // add an array of 100 pointers pointing to null
     for (int i=0; i<100; i++)
    {
     ptrFlights[i]=0;
    }
}
void Airline::addFlight(Flight* newFlight)
{
 int isFlightThere=0;
 if (numberFlights>0)
   {   
     for (int i=0; i<numberFlights; i++)  //check and see if flight is already in the array
       {
       if (ptrFlights[i]==newFlight)
	 {
	  isFlightThere=1;
	 }
       }
     if (isFlightThere==1) //if it is print a warning
       {
      cout << "Warning: Flight is already there!" << endl;
       }
     else // if it's not assign the newFlight to that element
       {
      ptrFlights[numberFlights]=newFlight;
      numberFlights=numberFlights+1;
       }
}
 else
   {
     cout << numberFlights << endl;
     ptrFlights[0]=newFlight;
     cout << numberFlights << endl; 
     numberFlights=numberFlights+1;
     cout <<  numberFlights << endl;
   }
}    
  
void Airline::removeFlight(Flight * rmFlight)
{
  int test=0;
  int count=0;
  while (test==0)
    {
      if (ptrFlights[count]!=0)
	{
	  count=count+1;
	}
      else
	{test=1;}
    }
      int isFlightThere=0;
      for (int i=0; i<count; i++)
	{
	  if (ptrFlights[i]==rmFlight)
	    {
	      ptrFlights[i]=0;
	      isFlightThere=1;
	    }
	}
      if (isFlightThere==0)
      {
	cout << "Flight you're trying to remove isn't there!!!" << endl;
	  }
}
bool Airline::checkAvailability(string myDeparture, string myDestination)
{
  int test=0;
  int count=0;
  while (test==0)
    {
      if (ptrFlights[count]!=0)
	{
	  count=count+1;
	}
      else
	{
	  test=1;
	}
    }
  int isDepartureThere=0;
  int flightNumber=0;
  for (int i=0; i<count; i++)
    {
      if ((*ptrFlights[i]).getDeparture()==myDeparture)
	{
	  isDepartureThere=1;
	}
    }
  int isDestinationThere=0;
  for (int i=0; i<count; i++)
    {
      if ((*ptrFlights[i]).getDestination()==myDestination)
	{
	  isDepartureThere=1;
	  if ((*ptrFlights[i]).getDeparture()==myDeparture)
	    {
	      flightNumber=i;
	      cout << flightNumber << endl;
	    }
	}
}
  if ((isDepartureThere==0)||(isDestinationThere==0))
    {
      cout << "Flight with given departure and destination does not exist!" << endl;
      return 0;
    }
  else 
    {
      return ((*ptrFlights[flightNumber]).getAvailableSeatsNumber()!=0);
    }
}
SenorKeeed's adventures in C++ Quote
04-12-2013 , 04:44 PM
change

Code:
Flight *ptrFlights[];
to

Code:
Flight* ptrFlights[100];
in the .h file. Then don't new it in the constructor.

Where is the code that uses this class?

Your addFlight looks wrong in the check for dupes. Need to see how it is used to be sure.
SenorKeeed's adventures in C++ Quote
04-12-2013 , 04:51 PM
Quote:
Originally Posted by Chips Ahoy
change

Code:
Flight *ptrFlights[];
to

Code:
Flight* ptrFlights[100];
in the .h file. Then don't new it in the constructor.

Where is the code that uses this class?

Your addFlight looks wrong in the check for dupes. Need to see how it is used to be sure.
if I don't use new in the constructor then it won't it be on the stack instead of the heap? We're supposed to allocate the array dynamically.

I haven't really written the code to test the class yet. I'm just trying to get it to compile.
SenorKeeed's adventures in C++ Quote
04-12-2013 , 05:12 PM
Anytime you are thinking of using a dynamically allocated array, you should probably use std::vector instead. This will make your code much simpler.
SenorKeeed's adventures in C++ Quote
04-12-2013 , 05:14 PM
This is a homework assignment and we were told to use a dynamically allocated array
SenorKeeed's adventures in C++ Quote
04-12-2013 , 05:29 PM
Ah, understood. In that case, keep it in mind for later.
SenorKeeed's adventures in C++ Quote
04-12-2013 , 06:12 PM
Use Flight** ptrFlights in the header file
Then dynamically allocate with new in the constructor.

R remember in C++ the array's NAME is a pointer to the first element. So the name of an array of pointers is a pointer to the first element, that is, a pointer to a pointer.
SenorKeeed's adventures in C++ Quote
04-12-2013 , 06:15 PM
Quote:
Originally Posted by sards
Ah, understood. In that case, keep it in mind for later.
Ya. THe better C++ books use a lot of vector/STL in beginner C++ programs. Because people should get out of the mindset that C++ is C with classes. There are many support classes that should be used instead of fiddling with arrays. Later you can do the more advanced memory management stuff or write custom optimized data structures.

Also in this problem it seems a linked list structure is more appropriate since you probably won't know the number of flights up front. For bonus points implement this using a linked structure
SenorKeeed's adventures in C++ Quote
04-12-2013 , 06:26 PM
So all I need to declare is the pointer for the first element in the array? And when I do ptrFlights=new Flight*[100] it's saying allocate the 99 memory locations next to ptrFlights to the array?

And thanks, that seems to work.
SenorKeeed's adventures in C++ Quote

      
m