So this is the last part of a program I've been working on for four weeks now. This question may be a tough one considering the amount of files included in the program.
The program is to read in a file of requests between two cities, read in a file of flights and cities that occur between the cities. It then checks to see if there is a path between the flights and output an itinerary. I have the correct itinerary outputting, but when attempting to output the associated flight number and price according to the city, I am getting odd data. How can I output the correct flight number and price associated with each flight on the itinerary.
I'll post out the output I am currently getting and the section where I am outputting the data. I'm sure I'll need to post more files so the program can be understood.
Don't want the code done for me, just a point in the right direction! I don't want to let this program defeat me!
Output:
Code:
Request is to fly from Atlanta to San-Diego.The flight itinerary is:
Flight # From To Cost
10 Atlanta Chicago $134529069
10 Chicago Miami $134529069
10 Miami Dallas $134529069
10 Dallas San-Francisco $134529069
10 San-Francisco San-Diego $134529069
This function finds a path between cities.
Code:
bool flightMap::IsPath(string originCity, string destinationCity){
StackClass aStack, bStack;
flightStruct flightRec;
string topCity, nextCity;
bool success;
int index = 0;
UnvisitAll();
aStack.Push(originCity, success);
MarkVisited(originCity);
aStack.GetStackTop(topCity, success);
while(!aStack.IsEmpty() && topCity != destinationCity)
{
success = GetNextCity(topCity);
if(!success)
aStack.Pop(success);
else
{
aStack.Push(topCity, success);
MarkVisited(topCity);
}
aStack.GetStackTop(topCity, success);
}
//Returns false if an itinerary is not found
if(aStack.IsEmpty())
{
cout << "Sorry, EastWest airline does not fly from " << originCity << " to " << destinationCity;
return false;
}
while(!aStack.IsEmpty())
{
aStack.Pop(topCity, success);
bStack.Push(topCity, success);
}
bStack.Pop(destinationCity, success);
while(!bStack.IsEmpty())
{
bStack.Pop(destinationCity, success);
flights[index].Retrieve(flightRec, success);
cout << left
<< setw(10) << flightRec.flightNum
<< setw(18) << originCity
<< setw(20) << destinationCity
<< '$' << flightRec.price << endl;
originCity = destinationCity;
}
return !(aStack.IsEmpty());
}
This functions finds the next city, checks if it is visited, and if it is not, uses it for the itinerary.
Code:
bool flightMap::GetNextCity(string& nextCity) //Pass back the first adjacent city that has not been visited. If it has not been visited, return true. Otherwise, return false.{
flightStruct flightRec;
bool success = false;
int index = 0;
int destIndex = 0;
int i = 1;
index = LinearSearch(cities, nextCity, size);
while(i <= flights[index].length())
{
if(flights[index].IsEmpty())
return false;
flights[index].RetrieveByPosition(i, flightRec, success);
destIndex = LinearSearch(cities, flightRec.destination, size);
if(!visited[destIndex])
{
nextCity = flightRec.destination;
return true;
}
i++;
}
return false;
}
The file with the flights and associated price and numbers.
Code:
3746 Los-Angeles Seattle 230
772 San-Francisco Los-Angeles 89
8322 San-Francisco San-Diego 120
829 Dallas San-Francisco 250
103 Dallas Miami 350
1114 Dallas Chicago 320
380 Chicago Albuquerque 400
290 Las-Vegas Nashville 59
920 Nashville Las-Vegas 59
1172 Atlanta Memphis 47
891 Memphis Dallas 140
3940 Chicago Orlando 430
2843 Chicago Miami 500
339 Nashville Atlanta 83
920 Atlanta Orlando 270
722 Orlando Miami 80
233 Washington-DC Orlando 195
429 Tampa Nashville 78
428 Nashville Tampa 79
3345 Washington-DC Boston 440
112 Boston New-York-City 140
211 New-York-City Boston 140
983 Nashville New-York-City 260
888 New-York-City Atlanta 430
345 Boston Chicago 390
290 Chicago Atlanta 175
1203 Nashville Denver 560
1204 Denver Nashville 590
529 New-York-City Denver 785
89 Nashville Miami 490
8291 San-Francisco Dallas 250
1031 Miami Dallas 350
842 Orlando Chicago 430
339 Phoenix San-Francisco 384
480 Orlando Atlanta 270
563 Boston Washington-DC 440
2901 Atlanta Chicago 175
665 Nashville Tampa 320
837 Phoenix San-Jose 283
8371 San-Jose Phoenix 283
883 Philadelphia Nashville 410
8831 Nashville Philadelphia 410
877 Los-Angeles Dallas 240