3 Replies - 3634 Views - Last Post: 23 November 2006 - 10:53 AM Rate Topic: -----

#1 silverswim1979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-November 06

Calling function(with array argument) from a different .cpp file

Posted 22 November 2006 - 11:15 PM

For a project, I have to add code to the following program (main) which will call a function named "createArray" (which hypothetically reads from a text file of ~10000 flights, creates an array, and returns the total number of flights). The other function (createArray) has to be located in another file called create_array.cpp. My question is: how can I write this code so that it calls the createArray function(which has an array argument) from another file.
Also, so that when this file is compiled, it will display my desired output.. Any advice is appreciated!
#include <iostream>
#include <fstream>
#include "../include/flight.hpp"
#include "../include/create_array.hpp"

using namespace std;

// this function prints the flight information for a given flight
void print_flight(flight[], int);

// this program creates an array of flights and then prints out the information for the first, second, and final flights in the array.

int main(){

const int MAX_FLIGHTS = 10000;
flight flights[MAX_FLIGHTS];
const string SENTINEL = "#";
string input = " ";
int flightCounter = 0;
int flightNum;
string orig;
string dest;
float price;
int seats;
int index;



cout << "\n\nFLIGHTS LIST:\n\n";
print_flight(flights, 0);
print_flight(flights, 1);
print_flight(flights, flightCounter-1);


return 0;
}

// this function prints the flight information for a given flight
void print_flight(flight flights[],int this_flight)
{
cout << "Flight Number: " << flights[this_flight].get_flAirline();
cout << " " << flights[this_flight].get_flNum() << endl;
cout << "Departure Airport: " << flights[this_flight].get_flOrig() << endl;
cout << "Destination Airport: " << flights[this_flight].get_flDest() << endl;
cout << "Price: " << flights[this_flight].get_flPrice() << endl;
cout << "Available Seats: " << flights[this_flight].get_flSeatCount() << endl << endl;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Calling function(with array argument) from a different .cpp file

#2 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Calling function(with array argument) from a different .cpp file

Posted 22 November 2006 - 11:29 PM

As long as you include the file that contains the function you can call it. Is it part of the flight class?

Do you have the createArray code to post here?

Im guessing its something like:
int createArray(flight &addr);

And you would call it like:
int val=createArray(flight);

From what you have posted in assuming this function fills your flight array. Is it part of the flight class?

More specific info would help answer. ;)
Was This Post Helpful? 0
  • +
  • -

#3 silverswim1979  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-November 06

Re: Calling function(with array argument) from a different .cpp file

Posted 22 November 2006 - 11:41 PM

This is the create_array.cpp code that the reservation_system.cpp code is referring to. The function within (int createArray(flight flights[], const int MAX_FLIGHTS = 10000) needs to be called by reservation_system.cpp (main) so that the information can be used, and displayed to the user. I'm not sure how to call this function from int main() so that the number of flights is returned.


#include <iostream>
#include <fstream>
#include "../include/flight.hpp"
using namespace std;


// The below function takes an array that is provided in the calling function and populates it with flight info read in from a file.

//This function should receive an array of flights and the array's maximum possible number of elements as arguments
int createArray(flight flights[], const int MAX_FLIGHTS = 10000)
{

  const string SENTINEL = "#";
  string input = " ";
  int flightCounter = 0;
  int flightNum;
  string orig;
  string dest;
  float price;
  int seats;
  int index;


  ifstream flightData;
  flightData.open("../data/flights.txt");
  if (!flightData)
	{
	  cout << "Could not find file containing flights information. This file is expected to be in ../data with relationship to the build directory and is expected to be named 'flights.txt'.\n";
	  exit(0);
	}

  // read first string from input file and test it to see if it is a
  //  '#' sign, which is used as the sentinel for end of file

  flightData >> input;

  // if there is input, grab it and stick it in a flight object
  // loop until sentinel is reached

  if ((input == SENTINEL) || (input == " ")) 
	{
	  cout << "The flights file has no flights so am aborting this run.\n";
	  exit(0);
	}
	  
  while (input != SENTINEL && flightCounter < 10000)
	{
	  // since sentinel hasn't been reached,
	  // read in rest of flight info
	  flightData >> flightNum >> orig >> dest >> price >> seats;

	  //set data in the current flight array element
	  flights[flightCounter].set_flData(input,flightNum,orig,dest,price,seats);

	  // grab the beginning of the next line and increment the counter
	  flightData >> input;
	  flightCounter ++;

	}

}

Was This Post Helpful? 0
  • +
  • -

#4 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: Calling function(with array argument) from a different .cpp file

Posted 23 November 2006 - 10:53 AM

So you are asking how to link that file?

#include "file.cpp" should work, though i dont think you are supposed to (good form) do it that way.

Hopefully someone will drop by and explain a better method. :-\
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1