c++第二天_理解解释器和编译器的不同 C++第三天_标准名称空间
Feb 20

进行到现在进度还是比较慢,主要是白天基本没有时间来完成学习。只有晚上有一点时间,挤时间的感觉是很不舒服。怪不得以前父亲老是说我以后就知道时间的宝贵了,现在真是深有体会。

话不多说了,代码如下:

下载: cout.txt
  1. /*
  2.   Name: 使用cout
  3.   Copyright: Hivan Doo
  4.   Author: Hivan Doo
  5.   Date: 20-02-08 10:41
  6.   Description: 程序清单2.2 使用cout,学习cout的使用。
  7. */
  8. // listing 2.2 using std::cout
  9. #include <iostream>         
  10. //#号为预处理器标记,将iostream文件加入到源代码中
  11.  
  12. using namespace std;
  13. int main()
  14. {
  15.     cout << "Hello there.\n";
  16. //cout最简单的用法,打印字符串,\n是特殊的格式符,告诉cout另起一行.
  17.     cout << "Here is 5: " << 5 << "\n";
  18. //三个值被传递给cout,由插入运算符分开.冒号后的空格也是字符串的组成部分.
  19.     cout << "The manipulator std::endl "; 
  20. //显示提示消息,然后程序使用控制符std::endl,endl作用是另起一行
  21. //endl也是由标准库提供的,因此前边加上std::
  22. //endl表示end line,是end-ell而不是end-one
  23. //endl比"\n"更好,因为适应当前使用的操作系统,而在某些OS或平台上,"\n"可能不是完成的换行符
  24.     cout << "Writes a new line to the screen.";
  25.     cout << std::endl;
  26.     cout << "Here is a very big number:\t" << 70000;
  27. //使用一个新的格式化字符"\t",它插入一个制表符.
  28. //此行表明,不仅可以显示整型数据,也可以显示长整型数据.
  29.     cout << std::endl;
  30.     cout << "Here is the sum of 8 and 5:\t";
  31. //与以下一行表明cout可以进行简单加法
  32.     cout << 8+5 << std::endl;   
  33. //将8+5的值传递给cout,而输出是13
  34.     cout << "Here's a fraction:\t\t";
  35.     cout << (float)5/8 <<std::endl; 
  36. //将5/8的值插入到cout中
  37.     cout << "And a very very big number:\t";
  38. //到此行为止因为"\t"作用其输出对齐
  39.     cout << (double) 7000 * 7000 << std::endl;
  40. //(double)告诉cout这是一个浮点数
  41.     cout << "Don't forget to replace Hivan Doo ";   
  42. //此行显示我的名字
  43.     cout << "With your name...\n";
  44.     cout << "Hivan Doo is a c++ programmer!\n"; 
  45. //确认我是一个真正的C++程序员,这肯定是真的,因为PC是这么说的
  46.     char response; 
  47.     cin >> response; 
  48. //由字符控制程序继续
  49.     return 0;
  50. }

执行效果如下:
使用cout

到这里为止都很顺利,可以以下一个很平常的代码却遇到了无法编译的情况。

下载: job1.txt
  1. #include <iostream>
  2. int main()
  3. {
  4.     int x = 5;
  5.     int y = 7;
  6.   std::cout << endl;
  7.   std::cout << x+y << " " << x*y;
  8.   std::cout << end;
  9.   return 0;
  10. }

错误出在第六行,错误提示如下:
编译器: Default compiler
执行 g++.exe…
g++.exe “F:\我的编程学习\C++\第一章 绪论\job1.cpp” -o “F:\我的编程学习\C++\第一章 绪论\job1.exe” -I”C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include” -I”C:\Dev-Cpp\include\c++\3.4.2\backward” -I”C:\Dev-Cpp\include\c++\3.4.2\mingw32″ -I”C:\Dev-Cpp\include\c++\3.4.2″ -I”C:\Dev-Cpp\include” -L”C:\Dev-Cpp\lib”
F:\我的编程学习\C++\第一章 绪论\job1.cpp: In function `int main()’:
F:\我的编程学习\C++\第一章 绪论\job1.cpp:6: error: `endl’ undeclared (first use this function)
F:\我的编程学习\C++\第一章 绪论\job1.cpp:6: error: (Each undeclared identifier is reported only once for each function it appears in.)

F:\我的编程学习\C++\第一章 绪论\job1.cpp:8: error: `end’ undeclared (first use this function)

执行结束

不知道是为什么,看来得上论坛去找高手们问问了。

PS:CSDN上给出了一个正确的代码:

下载: right.txt
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int x = 5;
  6.     int y = 7;
  7.     cout << endl;
  8.     cout << x+y << " " << x*y;
  9.     cout << endl;
  10.     return 0;
  11. }

其实我还是不明白,为什么添加一个关键字using就能正确编译了?

written by doo \\ tags: , ,

One Response to “c++第二天_使用cout”

  1. leo Says:

    std::endl

Leave a Reply