首页 > 解决方案 > cout的异常行为

问题描述

该程序运行良好并在使用 Codeblocks IDE(使用 g++ 编译器)运行时产生预期的输出。但是当使用 g++ 编译器在 linux 终端上运行时,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 语句不起作用。

标签: c++cout

解决方案


推荐阅读