[问题]帮忙看个超简单的C++ 程序
-
- 帖子: 7
- 注册时间: 2006-06-11 16:38
- jiangpeng
- 帖子: 223
- 注册时间: 2006-07-25 9:33
- 联系:
Take what man makes and use it, But do not worship it, For it shall pass. -- Anonymous
Twitter @jiangpeng
Twitter @jiangpeng
- cho
- 帖子: 70
- 注册时间: 2006-08-12 7:46
Re: [问题]帮忙看个超简单的C++ 程序
while ( cin >> word )
此句为永真,进入死循环。
此句为永真,进入死循环。
- ectotherm
- 帖子: 225
- 注册时间: 2006-05-23 13:35
- 联系:
- jiangpeng
- 帖子: 223
- 注册时间: 2006-07-25 9:33
- 联系:
大家怎么都不看我给地连接呢?问题已经解释地很清楚了
istream& istream::operator >>(...)返回值是istream&,不然怎么cin >> foo >> bar呢?
istream& istream::operator >>(...)返回值是istream&,不然怎么cin >> foo >> bar呢?
Take what man makes and use it, But do not worship it, For it shall pass. -- Anonymous
Twitter @jiangpeng
Twitter @jiangpeng
- ectotherm
- 帖子: 225
- 注册时间: 2006-05-23 13:35
- 联系:
How do I write this very simple program?
代码: 全选
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<double> 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.size(); ++i) cout << v[i] << '\n';
return 0; // success return
}
代码: 全选
while(cin>>d) v.push_back(d); // read elements
当然在 standard c++ library 的实现中是The program ends reading input when it sees "end of file". If you run the program from the keybord on a Unix machine "end of file" is Ctrl-D. If you are on a Windows machine that because of a bug doesn't recognize an end-of-file character, you might prefer this slightly more complicated version of the program that terminates input with the word "end":
代码: 全选
template <typename _CharT, typename _Traits> basic_istream<_CharT, _Traits>& operator >>(...)
代码: 全选
cin >> a >> b;
代码: 全选
int a = 100; cout << a++ << ',' << ++a << ',' << a;
代码: 全选
102,100,102 // gcc
101,102,102 // vc6
-
- 帖子: 8
- 注册时间: 2006-08-16 19:16
Re: [问题]帮忙看个超简单的C++ 程序
我认为cin这是一个函数,当执行cin>>word后函数会返回一个值,你输入了那当然返回1了,while(1)还不死循环那!lqw0205 写了:在我的机器上始终跳不出while循环,why, why, why?代码: 全选
#include <iostream> #include <string> using namespace std; int main() { string word; while ( cin >> word ) { cout << "Word read is: " << word << endl; } cout << "OK: no more words to read: bye!" << endl; return 0; }
cin在linux是以什么判定终结的?
- ectotherm
- 帖子: 225
- 注册时间: 2006-05-23 13:35
- 联系:
这个是我贴的 Stroustrup 的代码,他都有解决的办法了,建议你去他的网站上仔细看看,linux 终端上 Ctrl + D 可以。while(cin>>d) v.push_back(d); // read elements
The program ends reading input when it sees "end of file". If you run the program from the keybord on a Unix machine "end of file" is Ctrl-D. If you are on a Windows machine that because of a bug doesn't recognize an end-of-file character, you might prefer this slightly more complicated version of the program that terminates input with the word "end":
谁说了 cin >> word 一定返回 1 呢?
-
- 帖子: 4
- 注册时间: 2005-07-23 0:16
-
- 帖子: 2
- 注册时间: 2006-04-12 16:05
这段代码有问题
basic_istream::operator>> 正常情况下返回 basic_istream&(不然怎么可以 cin >> a >> b >>c)。遇到eof时会引发一个异常,可以试一下如下代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
try {
while ( cin >> word ) {
cout << "Word read is: " << word << endl;
}
} catch(...) {
cout << "exception" << endl;
}
cout << "OK: no more words to read: bye!" << endl;
return 0;
}
basic_istream::operator>> 正常情况下返回 basic_istream&(不然怎么可以 cin >> a >> b >>c)。遇到eof时会引发一个异常,可以试一下如下代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
try {
while ( cin >> word ) {
cout << "Word read is: " << word << endl;
}
} catch(...) {
cout << "exception" << endl;
}
cout << "OK: no more words to read: bye!" << endl;
return 0;
}