使用g++的c++输出中的额外细节

Extra details in output of c++ using g++

本文关键字:细节 输出 g++ c++ 使用      更新时间:2023-10-16

我用c++编写了一个程序,没有编译错误,运行良好,但在输出结束时,它总是在同一行显示我的设备名称和目录。我试过使用endl,但仍然没有改变(我正在使用g++)程序代码如下

#include<iostream>
using namespace std;
int  max(int *,int); 
int main()
{int n,a[10],b;
 cout<<"Please enter the no. of integers you wish to enter ";
 cin>>n;
 for(int i=0;i<n;i++)
 {cout<<endl<<"please enter the "<<i+1<<" no. ";
  cin>>a[i];
 }
 b=max(a,n);
 cout<<endl<<"The greatest no. is "<<a[b]<<" and its index position is "<<b<<endl;
 return 0;
}
int max(int *ptr,int n)
{int i,max,k=0;
 max=ptr[0];
 for(i=0;i<n;i++)
 {if(max<ptr[i])
  {max=ptr[i];
   k=i;
  }
 }
 return k;
}

和输出请输入号码。希望输入4的整数

请输入1号。2

请输入2号。4

请输入3号。6

请输入4号。1

最大的no。为6,索引位置为2*himanshu@hunt:~/Documents/Himanshu/c++ $*

从您给出的所有信息来看,似乎没有在输出结束时打印新行,这意味着您在添加std::endl时忘记重新编译,或者您已经放错了位置。

编辑:

我只是按你发布的方式运行了你的代码。endl工作正常,命令行位于代码输出的下面。你显然是运行一个旧的二进制文件,从代码没有endl还。仔细查看g++调用中生成的文件的名称(在-o之后的名称),以及它的位置。您可以尝试将您正在运行的可执行文件重命名为其他名称,并运行g++以查看是否生成了具有您期望的名称的新文件。

就像前面提到的,确保您的代码中有using namespace std#include <iostream>