编译 & 执行 C++ 程序

编译 & 执行 C++ 程序
接下来让我们看看如何把源代码保存在一个文件中,以及如何编译并运行它。下面是简单的步骤:

打开一个文本编辑器,添加上述代码。
保存文件为 hello.cpp。
打开命令提示符,进入到保存文件所在的目录。
键入 'g++ hello.cpp ',输入回车,编译代码。如果代码中没有错误,命令提示符会跳到下一行,并生成 a.out 可执行文件。
现在,键入 ' a.out' 来运行程序。
您可以看到屏幕上显示 ' Hello World '。

 

 1 #include <iostream>
 2 #include <iomanip> 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6     double 
 7      a=123.345,
 8      b=3.14153,
 9      c=-3214.67;
10      
11      cout <<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
12      cout <<setw(10) <<a<<endl;
13      cout <<setw(10)<<b<<endl;
14      cout <<setw(10)<<c<<endl;
15     return 0;
16 }

 

posted @ 2018-08-01 13:54  borter  阅读(279)  评论(0编辑  收藏  举报