开篇~- -| 回首页 | 2005年索引 | - -C++学习笔记1

Learing C++- -

Tagc++,                                          

有关C++中while(cin >> s) 怎样让输入终止?
遇到的第一个问题,所以查询的资料多些,彻底明白就好:)

(1)How do I write this very simple program?

Often, especially at the start of semesters, I get a lot of questions about how to write very simple programs. Typically, the problem to be solved is to read in a few numbers, do something with them, and write out an answer. Here is a sample program that does that:
	#include
#include
#include
using namespace std;

int main()
{
vector v;

double d;
while(cin>>d) v.push_back(d); // read elements
if (!cin.eof()) { // check if input failed
cerr << "format error\n";
return 1; // error return
}

cout << "read " << v.size() << " elements\n";

reverse(v.begin(),v.end());
cout << "elements in reverse order:\n";
for (int i = 0; i<< v[i] << '\n';

return 0; // success return
}
Here are a few observations about this program: For more examples of how to use the standard library to do simple things simply, see the "Tour of the Standard Library" Chapter of TC++PL3 (available for download).
(2)Read from cin in a while loop
The standard C/C++ style for reading is to put the read operation in a while loop condition. If the read proceeds correctly, then the value is true. If there is an end-of-file (EOF) meaning that the end of the data has been reached, or if there is an error in reading, then the value returned is false and execution continues at the end of the loop.
Example -- Adding numbers in the input

int sum = 0;
int x;

while (cin >> x) {
sum = sum + x;
}

cout << sum;

Testing the value from an input operation
Using cin in a while loop is a very common style of programming.

* Produces a value. The input operation with cin not only reads values into variables, but it also produces a value. That's because >> is an operator that produces a value. This value can be tested in loops and ifs.
* true. The value of cin >> x is true if a value was read into x.
* false. The value of cin >> x is false if it was unable to read. There are several possible causes for a reading failure.
o EOF. When there is no more data, the EOF (End-Of-File) condition occurs. Every stream of input has an end so this is a normal event. It happens when reading a disk file and the end is reached. It's also possible to signal an EOF from the console by entering a special key combination, which depends on which operating system and C++ library you are using. Generally, you can use Control-Z followed by Enter to send an EOF from the keyboard. See End-Of-File (EOF).
o Bad data. The read may fail if the data isn't formatted correctly. For example, when trying to read a floating-point temperature and the user types "zero", the read will fail.

Use this pattern when reading. Another style, which is not generally used is Anti-idiom - Using cin in three places, instead of one.

- 作者: ehui928 访问统计: 2005年10月29日, 星期六 21:43 加入博采 打印

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=3359895

博客手拉手

[2005-10-24 17:29:56.0]    《C 标准程序库》驾到

[2005-10-26 20:31:29.0]    C 程序设计之四书五经

[2005-10-27 02:55:04.0]    c primer 3rd下载

[2005-10-23 21:40:26.0]    今天正式开始学习C语言了

[2005-10-27 12:29:42.0]    C 资源之不完全导引

回复

评论内容: