PLEASE HELP WITH ARRAYS AND CSTRING

Hello, I'm having a lot of trouble with this program. I need to extract things from lines in file. for example one of the lines would be:
2013-05-07-1430, DTW, 150SW, FL330, Delta123
with this I would need to convert the first section to a date and time like "May 7, 2013" "2:30pm" then I would have to convert DTW to "Detroit Metro Airport", then convert 150SW to "150 miles southwest", then FL330 to "33,000 feet" and Delta123 to "Delta 123". I'm having problems extracting the portions of this file. I'm used to grabbing information within the commas but not within letters and numbers. I could use some help on how to do that. thank you


Write a C++ program to process air traffic control data stored in a comma-delimited file. The data relates to incidents of severe turbulence and you wish to create a plain text summary report for the file.

You will not know how many lines of data will be in the file. The format for the file will be:


{date},{airportcode},{distance from city},{flightlevel},{flightID}

An example data line for the file could be:


2013-05-07-1430,DTW,150SW,FL330,Delta123

The format for the date/time will always be exactly 15 characters (yyyy-mm-dd-hhmm).

CORRECTION:
The airport code will always be precisely three characters. The distance string will of course will vary,but the number will always be a one or two-digit integer followed by a two-character compass direction (N=north, NE-northeast, E=east, and so on). The altitude is presented as a coded number requiring the digits after the “FL” to be extracted. Add two zeroes to this value (along with a comma) to convert the flight altitude to feet.. Finally, the aircraft identifier will vary in the number of characters but will always be the last field in the line.

The provided file flights.txt will vary in the number of flight data lines. Be sure to use an end-of-file loop to drive the processing of your program. Each line of the input file includes one flight, time, and location. For each line of input, write an output message that can be printed:

Given the example above, the with output then:


Flight: Delta 123
Date: May 7, 2013
Time: 2:30 pm
Location: 150 miles southwest of Detroit Metropolitan Airport
Altitude: 33,000 feet

Your output should then be a list of these summaries, one for each observation. One additional data file will be required to allow you to convert the airport code to the airport name. Data file miairports.txt includes many actual airport identifiers and names. Your program will need to read this file into an array so that it can be searched as you process each line of the primary input file.


The program is presented in modular form using functions. You should continue this design with functions for the new features you are adding.



Here is the code description

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

// This program processes air traffic control data stored in a comma-delimited file
// The data relates to incidents of severe turbulence 
// and displays a plain text summary report for the file.
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
// prototypes
void GetList(ifstream& InFile, int date[], int airportcode[], int distance[], int level[], int ID[], int& totalRecs);
int strIndex(char inStr[], int start, char searchTarget);
void substring(char inStr[], int start, int end, char outStr[]);

const int Max = 1000; // Max number of data lines


int main()
{
	// Array of list elements 
	char inputLine[60];
	int totalRecs = 0;
	int date[Max];
	char dateString[16];
	int airportcode[Max];
	int distance[Max];
	int level[Max];
	char levelstring[6];
	char templevelstring[6];
	int ID[Max];
	int flight, date2, time, pmam, miles, direction, alt, pos, index, oldpos, level, airport;
	ifstream InFile;
	InFile.open("flights.txt");
	// Read file and count balues in array
	GetList(InFile, date, airportcode, distance, level, ID, totalRecs);

	while (!InFile.eof())
	{
		// locate date substring
		pos = strIndex(inputLine, 0, ',');
		substring(inputLine, 0, pos - 1, dateString);
		date2 = atoi(dateString);
		// locate airport code and convert

		// locate distance from city and extract "SW" then conver

		// locate flight level and extract "FL"
		oldpos = pos;
		pos = strIndex(inputLine, oldpos + 1, ',');
		substring(inputLine, oldpos + 1, pos - 1, levelstring);
		oldpos = pos;
		pos = strIndex(levelstring, 0, ',');
		substring(levelstring, 0, pos-1, templevelstring);
		strcpy(levelstring, templevelstring);
		level = atof(levelstring);
		// locate flight ID and convert 


	}



	// Output message
	cout << "Flight: " << flight << endl;
	cout << "Date: " << date2 << endl;
	cout << "Time: " << time << " " << pmam << endl;
	cout << "Location: " << miles <<" miles " << direction << " of " << airport << endl;
	cout << "Altitude: " << alt << " feet" << endl;

	system("pause");
	return 0;
}

void GetList(ifstream& InFile, int date[], int airportcode[], int distance[], int level[], int ID[], int& totalRecs)
{

	int i = 0;

	// Priming read 

	InFile >> date[i] >> airportcode[i] >> distance[i] >> level[i] >> ID[i];

	while (!InFile.eof())
	{
		i++; // Add one to pointer
		// continuation reads
		InFile >> date[i] >> airportcode[i] >> distance[i] >> level[i] >> ID[i];
	}

	totalRecs = i;

}
void substring(char inStr[], int start, int end, char outStr[])
{
	strcpy(outStr, "");             // Empty output string 

	int pos = 0;                    // To mark curr position of substring 
	for (int i = start; i <= end; i++)
	{
		outStr[pos] = inStr[i];      // Move character to output string 
		pos++;                       // Manually advance position marker 
	}

	outStr[pos] = '\0';             // Add null character to complete string 
}
int strIndex(char inStr[], int start, char searchTarget)
{
	int pos = start;       // Set position marker to start index 
	int returnPos = -1;    // Character position marker, assume not found 
	bool found = false;    // Assume not found at start 

	while (inStr[pos] != '\0' && !found)
	{
		if (inStr[pos] == searchTarget)   // If character found, 
		{
			returnPos = pos;               // Mark position 
			found = true;                  // Ready for exit from loop 
		}
		else
			pos++;                         // Otherwise, move to next character 

	}
	return returnPos;                    // Return value 
}
// This function puts the flight ID into a different format
void FlightID()
{

}
// This function converts the date into a different form and displays it
void DateChg()
{

}
// This fucktion takes the data from the date and converts it to time
// Displays it
void Time()
{

}
// This function takes the distance and airport code
// Changes it to the airport name and distance in miles
// Displays it in the proper format
void Location()
{

}
// This function takes the fightlevel and converts it to feet
// Then displays it
void Altitude()
{

}
?
And? You're having trouble, but what do you need help with can we do to help you?

-Albatross
Last edited on
I don't understand how to grab parts of the lines. For example I need take the "distance from city" which is "150SW" then I need to take the 150 part and output that it is 150 miles away. then I need to take the "SW" part and say South West. So that I can have an output of "150 miles south west". However, I don't understand how to do this. I also need to change "Delta123" to "Delta 123"
Another thing I need to do is change "2013-05-07-1420" then I need to take "2013-05-07" and change it to May 7, 2013. then I need to take "1420" and change it to 2:30pm. I just don't understand how to split up these chunks of the line
**?
I don't understand how to grab parts of the lines.


See if you can figure out a way with these functions:

http://en.cppreference.com/w/cpp/string/basic_string/getline
http://en.cppreference.com/w/c/string/byte/isdigit
http://en.cppreference.com/w/cpp/string/basic_string/substr http://en.cppreference.com/w/cpp/string/basic_string/stol

If not, post and I'll tell you how.

EDIT: Somehow I missed the fact that you were specifically talking about cstrings.

In your code, you already have the strIndex and substring functions. Using those and getline, you should be able to split the string into the parts that you want.

http://en.cppreference.com/w/cpp/io/basic_istream/getline
Last edited on
These links helped but I still cant get it. Could you please show me?
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
int main()
{
    const int MAXLENGTH = 200; //I doubt any line can be over 200 chars
    char buffer[MAXLENGTH + 1]; //+1 for null char
    ifstream InFile("flights.txt");
    
    while (inFile.getline(buffer, MAXLENGTH))
    {
        char dateTime[16]; //all are same format (2013-05-07-1430) so same length (15) + null char (1)
        char airportCode[4]; //all airport codes are 3 chars, right?
        char distanceCity[8]; //earth's circumference is 24901 miles, direction is at most 2 chars
        
        int start = 0;
        int end = strIndex(buffer, start, ',');
        substring(buffer, start, end - 1, dateTime);
        start = end + 1;
        end = strIndex(buffer, start, ',');
        substring(buffer, start, end - 1, airportCode);
        start = end + 1;
        end = strIndex(buffer, start, ',');
        substring(buffer, start, end - 1, distanceCity);
        //etc.
        DateChg(dateTime);
        Location(distanceCity, airportCode);
    }
    inFile.close();
    
    return 0;
}

// This function converts the date into a different form and displays it
void DateChg(char dateTime[])
{
    char year[5];
    char month[3];
    char day[3];
    substring(dateTime, 0, 3, year);
    substring(dateTime, 5, 6, month);
    substring(dateTime, 8, 9, day);
    int m = atoi(month);
    switch (m)
    {
        case 1: cout << "January "; break;
        case 2: cout << "February "; break;
        //etc.
    }
    cout << day << ", " << year << endl;
}

// This function takes the distance and airport code
// Changes it to the airport name and distance in miles
// Displays it in the proper format
void Location(char distanceCity[], char airportCode[])
{
    char distance[6];
    char direction[3];
    int start = 0, end;
    for (end = start; isdigit(distanceCity[end]); ++end);
    substring(distanceCity, start, end - 1, distance);
    start = end;
    end = strIndex(distanceCity, start, '\0');
    substring(distanceCity, start, end - 1, direction);
    cout << distance << " miles ";
    if (strcmp(direction, "SW") == 0)
        cout << "southwest of ";
    else if (strcmp(direction, "SE") == 0)
        cout << "southeast of ";
    else //etc.
    if (strcmp(airportCode, "DTW") == 0)
        cout << "Detroit Metropolitan Airport";
    else //etc.
}
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#define _CRT_SECURE_NO_WARNINGS
#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cstring>
#include <cmath>
using namespace std;
typedef char AirportType[4];
typedef char AirportNameType[50];
typedef char LocationType[31];
void substring(char inStr[], int start, int end, char outStr[]);
int strIndex(char inStr[], int start, char searchTarget);
void getData(AirportType airportint[], AirportNameType airportname[], LocationType location[], int& numVals);
void DateChg(char dateTime[]);
void Location(char distanceCity[]);
void airCodeChg(char airportCode[]);
void flightchg(char flightID[]);
void flightlev(char flightlevel[]);
const int MAX_ARRAY = 1200;
char buffer[MAX_ARRAY + 1]; // +1 for null char
int main()
{
	char inputLine[60];
	char datetime[16];
	char airportCode[4];
	char distCity[8];
	char flightlevel[6];
	char flightID[10];
	int numAirports;

	AirportType airportint[MAX_ARRAY];
	AirportNameType airportname[MAX_ARRAY];
	LocationType location[MAX_ARRAY];

	ifstream inFile("flights.txt");
	if (inFile.fail())         // Test for file existence 
	{
		cout << "Problem opening file";
		exit(-1);
	}
	getData(airportint, airportname, location, numAirports);

	inFile.getline(inputLine, 61);
	while (inFile.getline(buffer, MAX_ARRAY))
	{
		int start = 0;
		int end = strIndex(buffer, start, ',');
		substring(buffer, start, end - 1, datetime);
		start = end + 1;
		end = strIndex(buffer, start, ',');
		substring(buffer, start, end - 1, airportCode);
		start = end + 1;
		end = strIndex(buffer, start, ',');
		substring(buffer, start, end - 1, distCity);
		start = end + 1;
		end = strIndex(buffer, start, ',');
		substring(buffer, start, end - 1, flightlevel);
		start = end + 1;
		end = strIndex(buffer, start, ',');
		substring(buffer, start, end - 1, flightID);

		flightchg(flightID);
		DateChg(datetime);
		Location(distCity);
		airCodeChg(airportCode);
		flightlev(flightlevel);

		inFile.getline(inputLine, 61); // continuation read
	}
	system("pause");
	return 0;
}
void substring(char inStr[], int start, int end, char outStr[])
{
	strcpy(outStr, "");             // Empty output string 

	int pos = 0;                    // To mark curr position of substring 
	for (int i = start; i <= end; i++)
	{
		outStr[pos] = inStr[i];      // Move character to output string 
		pos++;                       // Manually advance position marker 
	}

	outStr[pos] = '\0';             // Add null character to complete string 
}
int strIndex(char inStr[], int start, char searchTarget)
{
	int pos = start;       // Set position marker to start index 
	int returnPos = -1;    // Character position marker, assume not found 
	bool found = false;    // Assume not found at start 
	while (inStr[pos] != '\0' && !found)
	{
		if (inStr[pos] == searchTarget)   // If character found, 
		{
			returnPos = pos;               // Mark position 
			found = true;                  // Ready for exit from loop 
		}
		else
			pos++;                         
	}
	return returnPos;                   
}
void getData(AirportType airportint[], AirportNameType airportname[], LocationType location[], int& numVals)
{
	{
		ifstream fileIn;                // Input file identifier                
		fileIn.open("miairports.txt");   // Open file 

		if (fileIn.fail())             // Test for file existence 
		{
			cout << "Problem opening file";
			exit(-1);
		}
		int i = 0;
		fileIn >> airportint[i] >> airportname[i] >> location[i];
		while (!fileIn.eof() && i < MAX_ARRAY)
		{
			i++;
			fileIn >> airportint[i] >> airportname[i] >> location[i];
		}
		numVals = i;
		fileIn.close();                // Close file 
	}
}
void DateChg(char dateTime[])
{
	char year[5];
	char month[3];
	char day[3];
	char hours[2];
	char minutes[2];
	substring(dateTime, 0, 3, year);
	substring(dateTime, 5, 6, month);
	substring(dateTime, 8, 9, day);
	substring(dateTime, 11, 12, hours);
	substring(dateTime, 13, 14, minutes);
	int m = atoi(month);
	switch (m)
	{
	case 1: cout << "January "; break;
	case 2: cout << "February "; break;
	case 3: cout << "March "; break;
	case 4: cout << "April "; break;
	case 5: cout << "May "; break;
	case 6: cout << "June "; break;
	case 7: cout << "July "; break;
	case 8: cout << "August "; break;
	case 9: cout << "September "; break;
	case 10: cout << "October "; break;
	case 11: cout << "November "; break;
	case 12: cout << "December "; break;
	}
	cout << "Date: " << day << ", " << year << endl;
	if (strcmp(hours, "00") == 00)
		cout << "Time: " << "12:" << minutes << "AM" << endl;
	else if (strcmp(hours, "01") == 00)
		cout << "Time: " << "1:" << minutes << "AM" << endl;
	else if (strcmp(hours, "02") == 00)
		cout << "Time: " << "2:" << minutes << "AM" << endl;
	else if (strcmp(hours, "03") == 00)
		cout << "Time: " << "3:" << minutes << "AM" << endl;
	else if (strcmp(hours, "04") == 00)
		cout << "Time: " << "4:" << minutes << "AM" << endl;
	else if (strcmp(hours, "05") == 00)
		cout << "Time: " << "5:" << minutes << "AM" << endl;
	else if (strcmp(hours, "06") == 00)
		cout << "Time: " << "6:" << minutes << "AM" << endl;
	else if (strcmp(hours, "07") == 00)
		cout << "Time: " << "7:" << minutes << "AM" << endl;
	else if (strcmp(hours, "08") == 00)
		cout << "Time: " << "8:" << minutes << "AM" << endl;
	else if (strcmp(hours, "09") == 00)
		cout << "Time: " << "9:" << minutes << "AM" << endl;
	else if (strcmp(hours, "10") == 00)
		cout << "Time: " << "10:" << minutes << "AM" << endl;
	else if (strcmp(hours, "11") == 00)
		cout << "Time: " << "11:" << minutes << "AM" << endl;
	else if (strcmp(hours, "12") == 00)
		cout << "Time: " << "12:" << minutes << "PM" << endl;
	else if (strcmp(hours, "13") == 00)
		cout << "Time: " << "1:" << minutes << "PM" << endl;
	else if (strcmp(hours, "14") == 00)
		cout << "Time: " << "2:" << minutes << "PM" << endl;
	else if (strcmp(hours, "15") == 00)
		cout << "Time: " << "3:" << minutes << "PM" << endl;
	else if (strcmp(hours, "16") == 00)
		cout << "Time: " << "4:" << minutes << "PM" << endl;
	else if (strcmp(hours, "17") == 00)
		cout << "Time: " << "5:" << minutes << "PM" << endl;
	else if (strcmp(hours, "18") == 00)
		cout << "Time: " << "6:" << minutes << "PM" << endl;
	else if (strcmp(hours, "19") == 00)
		cout << "Time: " << "7:" << minutes << "PM" << endl;
	else if (strcmp(hours, "20") == 00)
		cout << "Time: " << "8:" << minutes << "PM" << endl;
	else if (strcmp(hours, "21") == 00)
		cout << "Time: " << "9:" << minutes << "PM" << endl;
	else if (strcmp(hours, "22") == 00)
		cout << "Time: " << "10:" << minutes << "PM" << endl;
	else if (strcmp(hours, "23") == 00)
		cout << "Time: " << "11:" << minutes << "PM" << endl;
}
void Location(char distanceCity[])
{
	char distance[6];
	char direction[3];
	int start = 0, end;
	for (end = start; isdigit(distanceCity[end]); ++end);
	substring(distanceCity, start, end - 1, distance);
	start = end;
	end = strIndex(distanceCity, start, '\0');
	substring(distanceCity, start, end - 1, direction);
	cout << "Location: " << distance << " miles ";
	if (strcmp(direction, "SW") == 0)
		cout << "southwest of ";
	else if (strcmp(direction, "SE") == 0)
		cout << "southeast of ";
	else if (strcmp(direction, "NE") == 0)
		cout << "north east of";
	else if (strcmp(direction, "NW") == 0)
		cout << "north west of";
	else if (strcmp(direction, "N") == 0)
		cout << "north of ";
	else if (strcmp(direction, "S") == 0)
		cout << "south of ";
	else if (strcmp(direction, "E") == 0)
		cout << "east of ";
	else if (strcmp(direction, "W") == 0)
		cout << "west of ";
}
void flightchg(char flightID[])
{
	char word[4];
	char num[4];
	substring(flightID, 0, 4, word);
	substring(flightID, 5, 7, num);
	cout << word << " " << num << endl;
}
void flightlev(char flightlevel[])
{
	char level[6];
	char flight[3];
	int start = 0, end;
	substring(flightlevel, 0, 2, level);
	substring(flightlevel, 3, 4, flight);
	cout << "Altitude: " << level << "00 feet" << endl;
}
thank you that was very helpful. I edited my code now but parts are still not working. I now have to have it all displayed. here is the text file that will be running.
2013-05-07-1430,DTW,150SW,FL330,Delta123
2013-05-13-2245,JXN,50N,FL310,Southwest97
2013-06-01-0020,MBS,5S,FL250,United1120
2013-05-09-0550,TVC,20NW,FL200,Spirit55
2013-06-02-1200,MKG,40W,FL220,USAir456
2013-05-22-1715,MCD,15NE,FL290,American99
2013-05-30-0645,BAX,40SE,FL350,Alaska12

some of the parts are different lengths and I think that might be the problem, however I'm not sure how to fix it.

I also have to run another text file that takes the airports initials, finds it in a txt file, and writes it out and displays. (example "DTW" to Detroit Metropolitan Airport)

here is a small part of that text file.

APN Alpena County Rgnl Airport Alpena
ARB Ann Arbor Muni Airport Ann Arbor
SJX Beaver Island Airport Beaver Island
FNT Bishop Intl Airport Flint
6Y1 Bois Blanc Island Airport Bois Blanc Island
N98 Boyne City Muni Airport Boyne City
BFA Boyne Mountain Airport Boyne Falls
OEB Branch County Memorial Airport Coldwater
45G Brighton Airport Brighton
LAN Capital Region Intl Airport Lansing
CVX Charlevoix Muni Airport Charlevoix
SLH Cheboygan County Airport Cheboygan
TVC Cherry Capital Airport Traverse City
CIU Chippewa County Intl Airport Sault Ste Marie
DET Coleman A. Young Muni Airport Detroit
TTF Custer Airport Monroe
ESC Delta County Airport Escanaba
DTW Detroit Metropolitan Airport Detroit
DRM Drummond Island Airport Drummond Island

I would appreciate any help. thank you
this is the hint I was given for reading through the airport information and finding the full name, but I'm not sure where to start.

you will not be able to do this with the airport
file:

fileIn >> airportint[i] >> airportname[i] >> location[i];


Remember that the ³>>² operator seeks input ³tokens² separated with
spaces. There are numerous spaces in the city and airport names and you
will get out of sync very fast. I suggest instead using:

fileIn.getline(inputCstring, 100);

into a c-string. Then, you can substring out the two pieces you need for
this part.
On line 29, you incorrectly assume that flightID is a maximum of 9 characters long.

What is line 44 and 69 doing there? Reading from the file is already taken care of in line 45. Remove them.

On line 60, you are searching for ','. But flightID is the end of the line, there is no ',' after it. That means on line 61 you should use
substring(buffer, start, strlen(buffer) - 1, flightID);

On lines 131 and 132, hours and minutes should be arrays of size 3, not 2.

On line 154, "Date: " should be output before you output the month in the switch starting on line 139.

On line 212, I told you to search for '\0'. Turns out I was wrong, because I didn't notice that your strIndex() function returns -1 when it sees that. So on 213, replace end with strlen(distanceCity).

In flightchg(), you are assuming that the airline is always 3 characters and the number is always 3 digits. That's wrong.

In flightlev(), you are using level to store "FL" so why is it an array of size 6 instead of 3? You are using flight to store the number for altitude, and you assume that it's only 2 digits, which is wrong.

Get rid of line 244, because you don't use it at all.

On line 245, you are reading in 3 characters for level, which is wrong.

On line 246, you are reading in only 2 digits for the number, and assuming that the number starts at index position 3, which is wrong.

On line 247, you are outputting the "FL", not the number, which is wrong.

I will not comment on getData() or airCodeChg(). If you can understand everything else that's going on in your program, you should be able to write those functions correctly.
Topic archived. No new replies allowed.