c++第一天_简单的hello world!

我学C++可不是一时兴起。虽然我知道这和我的职业也许有些出入,可是这真是为了我的职业所需。 :mrgreen:

在maya中,有mel的脚本编写,而这部分的脚本是和C++几乎雷同的。所以,要学mel的脚本编写,C++是我必须要攻克的一关。
对于编程来说,我以前真是一点没有接触过,第一次接触C++是两个小时前。找编译器就花了我一个半小时,真想不明白,VC2005和DEV-c都属于C++编译器,可是两者的体积却相差那么多,大概VC还有别的功能吧。但是我用不上。。

计划有了C++基础以后立马攻克PHP+数据库。哈哈。。。。想的到很美,估计要一年之后去了。

学编程的人看了我这些日志不要嘲笑我,我完全是自学,没有人教我。写这些日志也只是为了督促和掌握进度。

第一天学习进度,时间不长,当然进步也没有。仅仅是学会了历史悠久的”hello world!”

下载: 12.cpp
  1. #include <iostream>
  2. int main()
  3. {
  4.     std::cout < <"hello world!\n";   //用于输出hello world! 
  5.     return 0;
  6. }

这是一段正规的c++代码,但是有些编译器使用的main函数原型,所以以上代码第一行就会出错,会出现:connot find file iostream,

如果遇到上述情况,需要从新编写代码,在第三行前边加上一行,程序变成:

下载: 12.cpp
  1. #include <iostream>
  2. int main();                                //most compilers don't need this line
  3. int main()
  4. {
  5.     std::cout < <"hello world!\n";   //用于输出hello world!
  6.     return 0;
  7. }

而在编译之后,执行.exe的时候会出现一闪而过的情况。开始我以为是自己的程序有问题,上网google才知道并不是如此。而是程序执行没有停止,直接结束关闭了。
为了避免上述情况,看到自己写的代码输出hello world,可以有以下一种办法。

1、利用windows自带的MS-DOS执行盘符/文件
我想不用多说大家都知道了。
2、修改代码如下:

下载: 12.cpp
  1. #include <iostream>
  2. int main()
  3. {
  4.     std::cout < <"hello world!\n";   //用于输出hello world!
  5.     char response;
  6.     std::cin >> response;           //用于暂停程序。
  7.     return 0;
  8. }</iostream>

3、在您想要暂停的地方加上 system(”pause”); 就可以使 C/C++ 程序暂停。不过,这个办法奏效的前提是系统中必须存在 pause 这个命令。此外,还需要包含标准头文件 stdlib.h(对于 C)或者 cstdlib(对于 C++)。例如:

下载: 12.cpp
  1. #include <iostream>
  2.         int main()
  3.         {
  4.             std::cout < <"hello world!\n";
  5.             system("pause");
  6.             printf("here too.\n");
  7.             system("pause");
  8.             return 0;
  9.         }

4、这种方法稍微有点复杂,但它通用于任何系统,只要这个系统拥有符合标准的 C/C++ 编译器。在您想要暂停的地方加上 getchar();(对于 C 和 C++)或者 cin.get();(仅适用于 C++)就可以使程序暂停,然后按回车程序就会继续执行。不过,您会发现,这种办法却不一定奏效。如果您够细心,会发现只有当 getchar();/cin.get(); 前面有接收输入的语句的时候,该办法才会失效。如果之前没有接收任何输入,该办法是 100% 奏效的!这是因为,如果前面接收了输入,输入流中可能会有残留数据,getchar();/cin.get(); 就会直接读取输入流中的残留数据,而不会等待我们按回车。解决该问题的办法是,先清空输入流,再用 getchar();/cin.get();。清空输入流的办法如下:

1). /* 适用于 C 和 C++。需要包含 stdio.h(对于 C)或者 cstdio(对于 C++)*/
while ( (c = getchar()) != ‘\n’ && c != EOF ) ; /* 对于 C 和 C++ */

2). cin.clear(); // 仅适用于 C++,而且还需要包含标准头文件 limits
cin.ignore( numeric_limits::max(), ‘\n’ );

例如:

下载: 12.cpp
  1. /*--------------------------------------------------------------
  2.          | 作者: Antigloss @ http://stdcpp.cn @ 蚂蚁的 C/C++ 标准编程
  3.          |
  4.          | 功能: 演示如何清空输入流及使用 getchar();/cin.get(); 暂停
  5.           -------------------------------------------------------------*/
  6.  
  7.         #include <iostream>
  8.         #include <limits>
  9.         #include <cstdio>
  10.  
  11.         using namespace std;
  12.  
  13.         int main()
  14.         {
  15.             int i_test, c;
  16.  
  17.             printf("Please enter an integer: ");
  18.             scanf("%d", &i_test);
  19.             printf("You just entered %d.\nPress enter to continue...", i_test);
  20.             while ( (c = getchar()) != '\n' && c != EOF ) ;  // 清空输入流
  21.             clearerr(stdin); // 清除流的错误标记
  22.             getchar();  // 等待用户输入回车
  23.  
  24.             cout < < "Please enter an integer: ";
  25.             cin >> i_test;
  26.             cout < < "You just entered " << i_test << ".\nPress enter to continue...";
  27.             cin.clear();  // 清除流的错误标记
  28.             cin.ignore( numeric_limits<streamsize>::max(), '\n' );  // 清空输入流
  29.             cin.get();  // 等待用户输入回车
  30.  
  31.             return 0;
  32.         }

PS:第一种方法自己想的,第二种方法书上写的,第三种和第四种方法都是网上查到的,第四种方法我没有测试,不知道可行不。但是前三种是绝对可行的。。。

About the Author

doo

独立,内向,叛逆而勤于思索... 深知自己一直是一个无法融入社会的异类,自从有了孩子以后才感觉人有的时候真是身不由己.最大的理想是成为优秀的CGer.

2 Responses to “ c++第一天_简单的hello world! ”

  1. thanks a lot

  2. :mrgreen:

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>