C++ Structure Array Flight terminal program

Pages: 12
Hello Collegestudent01,

If I understand the instructions correctly I believe these two functions cover what is needed.

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
int Menu()
{
	int choice{};

	std::cout << "\n  /**************Airline Flight Menu**************\\ \n";  // <--- The backslash needs to be excaped. That is the two "\\" after the "*"s.
	std::cout << "\n";  // This line is for Easier readability for the user. Can also be written as "\n\n\n". I would suggest 1 or 2, but not 3. The extra space on the first two is not needed.
	std::cout << "1: Add Flight Information\n";
	std::cout << "2: Delete Flight Information\n";
	std::cout << "3: Display All Flights\n";
	std::cout << "4: Display All Arivals OR All Departing Flight Information\n";
	std::cout << "5: Update The Actual Arrival Time of a Flight\n";
	std::cout << "6: QUIT\n";
	std::cout << " Enter A choice from the menu: ";  // <--- Removed the last "\n". Add  space i its place.

													 // Input here then verify that input is correct before returning answer to caller.
	do
	{
		try
		{
			std::cin >> choice;
			if (choice < 1 || choice > 6)
				throw choice;
			else
				return choice;
		}
		catch (int e)
		{
			std::cout << "\n Your choice is: " << e << "\n Invalid entry. Choices are 1 - 6." << std::endl;
			std::cout << " Enter A choice from the menu: ";
			continue;
		}
	} while (choice < 1 || choice > 6);
}

int getData(flightData(&flightInfo)[100], int count)
{
	int count{};
	// create file pointer
	std::string temp;

	std::ifstream inFile;
	// connect to a file
	inFile.open("flight.txt");

	if (!inFile)  // <--- Changed. This is all you need.
	{
		// This is all you need. Nothing fancy like the throw.
		std::cout << "File not found" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	inFile >> flightInfo[i].carrier;

	while (1)  // <---Changed to use "inFile.eof()" at end
	{
		inFile >> flightInfo[i].flightNum;
		//  Something here to check that the flight number is no more than 3 digits.

		inFile >> flightInfo[i].arrivDept;  // <--- This line needs to come first.
		inFile >> std::ws;  // <--- Added. Skips the white space before the city name.
		inFile.get(flightInfo[i].destinationCity, MAX);  // <--- Gets the name + some white spaces.

														 //  Function to remove the trailing white space.
		temp = StripWhiteSpace(flightInfo, i);

		//  Replaces the original input with a new shorter string .
		memcpy(flightInfo[i].destinationCity, temp.c_str(), temp.size() + 1);

		//inFile >> flightInfo[i].Departure;  // <--- Only need the above changed line that has been moved up becuse there is nothing for this line to read.

		inFile >> flightInfo[i].pubTime;
		inFile >> flightInfo[i++].actTime;
		
		try
		{
			if (i > MAXSIZE)
				throw i;
		}
		catch (int e)
		{
			std::cout << "\n Trying to add " << e + 1 << " to the list." << std::endl;
			std::cout << "\n Only the first 100 flights will be used." << std::endl;
			break;
		}
		inFile >> flightInfo[i].carrier;

		if (inFile.eof())
			break;
	}  // End while loop

	   // from AbstractionAnon.
	std::cout << " " << i << " flights loaded" << std::endl;

	return i;
}


For now using the try/catch is the only way I know of to throw and catch an exception. Someone else who has used the concept of throwing execptionnmay have a better way.

Hope that helps,

Andy
Topic archived. No new replies allowed.
Pages: 12