Программа работает нормально и выдает намеченный вывод при запуске с использованием IDE Codeblocks (использует компилятор g ++). Но при запуске на терминале linux с использованием компилятора g ++ операторы cout ведут себя необычно и не выводят половину операторов. Я вставил только функцию печати всего кода.
void print_result(int dist[], int previous[], int source, int destination)
{
int temp = destination, n=1;
answer[0] = destination;
cout<<"\n The shortest distance from the "<<names[source]<<" to "<<names[destination]<<" is: "<<dist[destination]<<endl;
cout<<"The path from the "<<names[source]<<" to "<<names[destination]<<" is:"<<endl;
while(temp != source) // the previous of the node is recorded until the source is reached
{
answer[n] = previous[temp];//the previous of all the nodes from the destination to the source is recorded
temp = previous[temp];
n = n+1;
}
for(int i=n-1; i>=0; i--) // since the previous loop was run n-1 times, it is clear that there are n-1 vertices between the source and the destination
{
cout<<" "<<nodes[answer[i]]<<" ("<<names[answer[i]]<<")";//the path is printed from source to destination
if(i!=0) // this condition is just to make sure that the arrow is not printed for the last element
{
cout<<" -->";
}
}
}
Вывод, который я получаю в IDE кодовых блоков,
Enter the source node 44
Enter the destination node 82
The shortest distance from the Mathews Arena to East village is: 811
The path from the Mathews Arena to East village is:
44 ( Mathews Arena) --> 45 ( Gainsborough Parking Garage) --> 47 ( Cullinane hall) --> 82 ( East village)
Но вывод, который я получаю в Linuxтерминал:
Enter the source node 44
Enter the destination node 82
is: 811t villagetance from the Mathews Arena
is: East village Mathews Arena
Не могу понять, почему мои операторы cout не работают.