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;
elsereturn 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.