进行到现在进度还是比较慢,主要是白天基本没有时间来完成学习。只有晚上有一点时间,挤时间的感觉是很不舒服。怪不得以前父亲老是说我以后就知道时间的宝贵了,现在真是深有体会。
话不多说了,代码如下:
- /*
- Name: 使用cout
- Copyright: Hivan Doo
- Author: Hivan Doo
- Date: 20-02-08 10:41
- Description: 程序清单2.2 使用cout,学习cout的使用。
- */
- // listing 2.2 using std::cout
- #include <iostream>
- //#号为预处理器标记,将iostream文件加入到源代码中
- using namespace std;
- int main()
- {
- cout << "Hello there.\n";
- //cout最简单的用法,打印字符串,\n是特殊的格式符,告诉cout另起一行.
- cout << "Here is 5: " << 5 << "\n";
- //三个值被传递给cout,由插入运算符分开.冒号后的空格也是字符串的组成部分.
- cout << "The manipulator std::endl ";
- //显示提示消息,然后程序使用控制符std::endl,endl作用是另起一行
- //endl也是由标准库提供的,因此前边加上std::
- //endl表示end line,是end-ell而不是end-one
- //endl比"\n"更好,因为适应当前使用的操作系统,而在某些OS或平台上,"\n"可能不是完成的换行符
- cout << "Writes a new line to the screen.";
- cout << std::endl;
- cout << "Here is a very big number:\t" << 70000;
- //使用一个新的格式化字符"\t",它插入一个制表符.
- //此行表明,不仅可以显示整型数据,也可以显示长整型数据.
- cout << std::endl;
- cout << "Here is the sum of 8 and 5:\t";
- //与以下一行表明cout可以进行简单加法
- cout << 8+5 << std::endl;
- //将8+5的值传递给cout,而输出是13
- cout << "Here's a fraction:\t\t";
- cout << (float)5/8 <<std::endl;
- //将5/8的值插入到cout中
- cout << "And a very very big number:\t";
- //到此行为止因为"\t"作用其输出对齐
- cout << (double) 7000 * 7000 << std::endl;
- //(double)告诉cout这是一个浮点数
- cout << "Don't forget to replace Hivan Doo ";
- //此行显示我的名字
- cout << "With your name...\n";
- cout << "Hivan Doo is a c++ programmer!\n";
- //确认我是一个真正的C++程序员,这肯定是真的,因为PC是这么说的
- char response;
- cin >> response;
- //由字符控制程序继续
- return 0;
- }
到这里为止都很顺利,可以以下一个很平常的代码却遇到了无法编译的情况。
- #include <iostream>
- int main()
- {
- int x = 5;
- int y = 7;
- std::cout << endl;
- std::cout << x+y << " " << x*y;
- std::cout << end;
- return 0;
- }
错误出在第六行,错误提示如下:
编译器: 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上给出了一个正确的代码:
- #include <iostream>
- using namespace std;
- int main()
- {
- int x = 5;
- int y = 7;
- cout << endl;
- cout << x+y << " " << x*y;
- cout << endl;
- return 0;
- }
其实我还是不明白,为什么添加一个关键字using就能正确编译了?







August 8th, 2008 at 9:57 am
std::endl