C++ Structure Array Flight terminal program

Hello All,
I am a student new to C++ and I am being asked to write a lab displaying airline flight schedules. The problem is I dont know how to start and I am having trouble being confident enough to assign constant variables and create structures as this is my first programming class.
(Any help would be greatly appreciated.)
Here is the prompt:


Assignment:
Develop a program that will be used to monitor the flight information at an airport.
The information to be maintained includes:
? the carrier (Southwest, United Airlines, Delta...),
? flight number,
? whether it is an arriving or departing flight,
? published arrival or departure time – this is the time promoted when booking flights
(use a 24-hour format; i.e., 2115 is 15 minutes after the 21st hour, or 9:15 p.m.),
? actual arrival/departure time - the time updated as the flight gets closer
? arrival or departure location (city).
Your program should allow the user to:
1. Add flight information,
2. Delete flight information
3. Display all flights
4. Display all arrival OR all departing flight information
5. Update the actual time of a flight
6. Quit
Program Design Notes:
? Create named constants to be used globally (ie. array sizes)
? Create a structure to hold flight information for one flight
o Carrier name will not exceed 20 characters
o Flight number will be an integer value not to exceed three digits,
o Arrival will be a char (‘A’- meaning arrival, ‘D’- meaning departure),
o Arrival or Destination City will not exceed 20 characters
o Published time and Actual time will be integers
? Divide the tasks into functions. See the structure chart on page 3.
You may use this design or one of your own.

Hello Collegestudent01,

Welcome to the forum. Some helpful information to get you started:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.pickinpatchfarm.com/articles/jEywvCM9/
http://www.pickinpatchfarm.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Look at your instructions this way:
Develop a program that will be used to monitor the flight information at an airport.

The information to be maintained includes:

1 the carrier (Southwest, United Airlines, Delta...),
2 flight number,
3 whether it is an arriving or departing flight,
4 published arrival or departure time – this is the time promoted when booking flights
  (use a 24-hour format; i.e., 2115 is 15 minutes after the 21st hour, or 9:15 p.m.),
5 actual arrival/departure time - the time updated as the flight gets closer
6 arrival or departure location (city).

Items 1 - 6 give you a very good place to start in defining a class or struct to hold the information. I find if you break up the instructions into smaller pieces and work on small parts the whole will come together. Start with what will hold the data followed by the input to put values into those pieces.

Put something together and post it and we can go from there.

I do not believe you are up to classes yet so a simple struct will work.
This may help: http://www.pickinpatchfarm.com/doc/tutorial/structures/

Hope that helps,

Andy
Thank You for the Welcome ,

I am having trouble like I said with the setup right now. I am trying to understand how to decalre the structure flight and then call the specific part of the structure to be read out as a display.I am just now sure the best way to do this I have this so far maybe somebody can explain how to from here ( but remembering i am just in beginning C++)
Please and Thank You!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include<fstream>
using namespace std;


int pub_Time,act_Time;
char dest_City[21];

const int records=100;

struct Flight
{
    char carrier[20];
    int flight_num[3];
    char A[21];
    char D[21];

}   record;


int main()


Flight carrier
        {
          CandyBar * pointer = carrier_name[3];
            cout << "Please Enter a Carrier ";
            getline(cin.pointer[0].carrier);
    cout << pointer[0].carrier;

Hello Collegestudent01,

In some ways it is a good start. In others it is not.

I would suggest using std::string in place of the C style character arrays, much easier to use.

As you will see in the later code main is a little off. Lines 24 and 25 are reversed.

Line 26 is using a type called "CandyBar" to create a pointer. This part is OK, but "carrier_name" is not defined anywhere I can see. anything that this like of code does work "pointer" will hold the address of the third element of the "carrier_name" array.

Line 28 "cin" does not have a member function called "pointer" that I know of. I think your idea here is to put the input of "cin" into the struct. The last bit of code coming up will show you how this might be done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include<fstream>

//using namespace std; // <--- Not the best to use.

int pub_Time, act_Time;
std::string dest_City[21];

const int records = 100;

struct Flight
{
	std::string carrier[20];  // <--- is an array of 20 strings. Or remove "[20]" for a single string.
	int flight_num[3];  // <--- This creates an array of 3 ints.
	char A[21];  // <--- Not sure what these are. If not dealing with single characters a std::string would be better.
	char D[21];

}   record;


int main()
{
	Flight Carrier; // <--- Moved down. Should pick nother name so as not to confuse "carrier

	CandyBar * pointer = carrier_name[3];

	cout << "Please Enter a Carrier ";
	getline(cin.pointer[0].carrier);  // <--- "pointer only has one address it is not an array. "cin" is wrong.
	cout << pointer[0].carrier;  // <--- "pointer only has one address it is not an array.

	// This might work better.
	cout << "Please Enter a Carrier ";
	std::getline(std::cin, Delta.carrier);
	std::cout << Carrier.carrier[i] << std::endl;

	//  "i" needs a way o change value when entering information. 


I have not started working on this program yet, but I will start shortly and see what I come up with.

Hope that helps,

Andy
Hello Collegestudent01,

I started working on the program and this is my idea for the struct for now. I usually break a program up into different files, so this is in a header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _FLIGHTINFOH_ 
#define _FLIGHTINFOH_

struct FlightInfo
{
	char s_carrier[20]{};
	int s_flight_num{};
	char s_AD{};
	std::size_t s_aPubADTimeHR{};
	std::size_t s_aPubADTimeMin{};
	std::size_t s_dPubADTimeHR{};
	std::size_t s_dPubADTimeMin{};
	std::size_t s_actADTimeHR{};
	std::size_t s_actADTimeMin{};
	char s_city[20]{};
};

#endif // end !_FLIGHTINFOH_ 


The "std::size_t" is just another name for "unsigned int" that I use. The "s_" prefixing the variable names lets me know it is part of a struct.

There are two sets of "PubADTime..." variables. When booking a flight it would be useful to know the departure time and arrival time of each available flight. When looking at a flight at the destination you may only use the published arrival time.

For now I am thinking along the lines of an array of structs to hold the departure and arrival times, but still thinking about how to access this array based on flight number and the "AD" character in the struct.

Hope that helps for now,

Andy
Thats a good idea to prefix your variables and it makes it easier to see where they are being used. I am still having trouble with the setup and i have only gotten this far
Do you think you could help me get this working as there are two compiling errors i cannot fix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include<fstream>
#include<cstring>
#include <vector>
#include <string>
using namespace std;

const int REC_MAX=100;
const int MAX=20;
const int flightNum=3;


int main()
{

 struct Flightinfo
{
   string flight_Data;
    char carrier[MAX];
    int flightNum[3];
    char dCity[MAX];
    char A[MAX];
    char D[MAX];
    int pub_Time;
    int act_Time;
}flightData;
    int i;

void getData(Flightinfo flightData[]);
int search( const Flightinfo flightData[], const char target[]);
void display( const GameType games[]);
{
    cout << "\nFlight Records: " << endl;

    for( int i=0; i<REC_MAX; i++ )
        printf( "%-23s%-20s%4d  $%6.2f\n", Flightinfo[i].carrier,
                                           Flightinfo[i].flightNum,
                                           Flightinfo[i].dCity,
                                           Flightinfo[i].A,
                                           Flightinfo[i].D,
                                           Flightinfo[i].pubTime,
                                           Flightinfo[i].actTime);
}
    cout << endl;
}



/*

    int i;

 vector<Flightinfo> records;

 struct Flightinfo records[100];
 string flights.txt;
cout<< " Enter your Flight Reservation information";
cin>>flights.txt;

ifstream inputfile(flights.txt);
  inputfile.open ("flights.txt");
  inputfile << "Writing this to a file.\n";
  inputfile.close();
  return 0;



*/






/*USED FOR CHANGING FLIGHT DATA EXAMPLE

void getData( GameType games[] )
{
    for( int i=0; i < SIZE; i++ )
    {
        // prompt and read each member
        cout << "Enter the game name: ";
        cin.getline( games[i].name, NAME_SIZE );

        cout << "Enter genre: ";
        cin.getline( games[i].genre, NAME_SIZE );

        cout << "Enter year of release: ";
        cin  >> games[i].year;

        cout << "Enter cost: ";
        cin  >> games[i].cost;

        cin.ignore();           // advances 1 character past the last return key
                                // to prepare for the next read (cstring)
        cout << endl;
    }

*/


Lines 29-30: Don't nest function prototypes inside main. Move these to line 11.

Line 31-43: You can't nest a function definition inside main.

Line 31: What is a GameType? Don't you mean Flightinfo?

Line 36: Not best style to mix C and C++ I/O. You should be using cout rather than printf.

Lines 22-23: Not clear what A and D are.

Line 18: Not clear what flight_Data is used for.

Lines 19,22,23: Why are you using C strings?

Line 20: Why is flightnum an array?

Line 35: This loop assumes you always have 100 flights.




Last edited on
HelloCollegestudent01,

AbstractionAnon has made some good points. Pay attention to them.

Starting at the top.

Delete line 3. You do not need to use C strings.

Line 6. Best to learn not to use this.

Line 8. I do not see any real use for this right now.

Line 9. Could be useful.

Line 10. Since you defined "flightnum" as an array which element do you want to set to 3?

Line 11. Is where lines 16 - 30 should go.

For the struct:

After thinking about it you should use std::string in place of the C style character arrays. You can check the length after the string has something in it and then limit it to 20 characters if needed. We can cover that later.

Line 20. Is a three element array. The [3] does not limit the number to three digits. You will need to check the size of the number, i.e., verify that it is three digits, when the number is entered.

Lines 22 and 23. This should be a single "char" to hold the letters "A" or "D" nothing more. Neither should be an array.

Lines 24 and 25. This will work, but if you decide to add a colon later it becomes much more involved. That is why I broke this down to hours and minutes in separate variables.

Line 26. Giving a name to "Flightinfo" is fine if you use it correctly. This just means that you do not have to define "flightData" in main.

Lines 31 to 43. Should be put after main not in main.

What is after main right now I will have to work on later.

For the "display" function:

As AbstractionAnon said Line 36: Not best style to mix C and C++ I/O. You should be using cout rather than printf. That said the "printf" will not work the way you have it. You are trying to print 4 variables, but you are listing 7 variables to print. And the format specifiers you are using do not match the variables that you are using. The "std::cout" would be a better choice.

The whole "Display" function look like you copied it, but forgot to change it to what you actually need.

After you get what you have straightened up I would work on the "getData" function and change to match your program and get it working.

Hope that helps,

Andy
Registered users can post here. Sign in or register to post.
  • 452471514 2018-02-18
  • 5299351513 2018-02-18
  • 3036511512 2018-02-18
  • 4224301511 2018-02-18
  • 6975481510 2018-02-18
  • 8259761509 2018-02-18
  • 7007851508 2018-02-18
  • 7796301507 2018-02-18
  • 567811506 2018-02-18
  • 2107511505 2018-02-18
  • 8923811504 2018-02-17
  • 6039601503 2018-02-17
  • 8788931502 2018-02-17
  • 4539021501 2018-02-17
  • 661851500 2018-02-17
  • 9152461499 2018-02-17
  • 4757661498 2018-02-17
  • 981131497 2018-02-17
  • 4385321496 2018-02-17
  • 5046771495 2018-02-17