Question???

This is a discussion on Question??? within the C++ Programming forums, part of the General Programming Boards category; I have a question. The instructions given from the professor states to do the following: Declare an array of five ...

  1. #1
    Unregistered
    Guest

    Question???

    I have a question. The instructions given from the professor states to do the following:
    Declare an array of five elements of type FLIGHT. Write a function that initializes the array(using structures).

    My original Structure:

    struct FLIGHT
    {
    long flightnum;
    long Date;
    char departcode
    ete......
    };

    Declare an array of five elemens of type FLIGHT:
    I did this in main,

    FLIGHT numof[5];
    getnum(numof[5]);//calling funtion

    Write a function that initializes the array.
    assuming I have written the funtion prototype and I have called the function in main, I wrote the following funtion per these intructions:

    int getnum(int flightinfo)
    {
    FLIGHT numof[5]={1111}{1112}{1113}{1114}

    Am I doing this correctly? Wouldn't the initialization only need the first element of the structure?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    int getnum(int flightinfo)
    {
    FLIGHT numof[5]={1111}{1112}{1113}{1114}

    You can't define data in a routine you call and be able to use it from the calling method unless you put it on the heap with a new;


    i'd change the getnum name to

    InitializeFlight( FLIGHT * pFlight, int nQty )
    Use InitializeFlight to iterative initialize the array based on the quantity of FLIGHT objects.

    and call it with

    InitializeFlight( numof , ( sizeof( numof ) / sizeof( numof[0]) ) )

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    FLIGHT numof[5]={1111}{1112}{1113}{1114}
    I believe this would initialize a 5 dimensional array. What you're looking for is:
    FLIGHT numof[5] = {11111, 11112, 11113, 11114}
    You're also leaving one array element undefined. You put 5 elements in a five element array. They are subscripted from 0 to 4, but there are 5 elements in the array.
    Or, I'm going buggy from looking at too much code.
    Last edited by Brown Drake; 10-10-2001 at 02:26 PM.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    struct FLIGHT 
    { 
      long flightnum; 
      long Date; 
      char departcode  
    }; 
    
    InitializeFlight(struct FLIGHT, int);
    
    int main()
    {
      struct FLIGHT numof[5];
      InitializeFlight( numof , ( sizeof( numof ) / sizeof( numof[0]) ) )
      ...
       return 0;
    }
    
    InitializeFlight(struct FLIGHT fl, int size)
    {
       for(int i=0;i < size; i++)
      {
         fl[i].flightnum = 1111;
         fl[i].Date = 2222;
         fl[i].departcode = 'x';
       }
    }
    This is more or less what you want but you are likely wanting to initalize the structure with data from a datafile. Anyways this is the format. I used some code from the other suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21