[问题]帮忙看个超简单的C++ 程序

软件和网站开发以及相关技术探讨
conquersky
帖子: 7
注册时间: 2006-06-11 16:38

#16

帖子 conquersky » 2006-08-12 21:19

死循环啊。。。。。。。
头像
jiangpeng
帖子: 223
注册时间: 2006-07-25 9:33
联系:

#17

帖子 jiangpeng » 2006-08-13 1:10

Take what man makes and use it, But do not worship it, For it shall pass. -- Anonymous

Twitter @jiangpeng
头像
cho
帖子: 70
注册时间: 2006-08-12 7:46

Re: [问题]帮忙看个超简单的C++ 程序

#18

帖子 cho » 2006-08-13 10:05

while ( cin >> word )

此句为永真,进入死循环。
头像
ectotherm
帖子: 225
注册时间: 2006-05-23 13:35
联系:

#19

帖子 ectotherm » 2006-08-14 15:15

这个是 Stroustrup 个人网站上的一个代码(好像是类似的),while (cin >> word) 并没有问题,而是这个语句在 win32s console 有问题,上面有讲怎么搞定,去看看。

cin >> word 实际上是调用 template <typename Tp> Tp& istream::operator >>(Tp& var); 当然有可能返回 0 只是这个 0 该怎么返回的问题。

ps: 这样的代码除了有学术研究的价值(有吗?)外,在实际中碰到算你运气好。
头像
jiangpeng
帖子: 223
注册时间: 2006-07-25 9:33
联系:

#20

帖子 jiangpeng » 2006-08-14 20:09

大家怎么都不看我给地连接呢?问题已经解释地很清楚了
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
头像
ectotherm
帖子: 225
注册时间: 2006-05-23 13:35
联系:

#21

帖子 ectotherm » 2006-08-14 20:50

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
	}
Stroustrup 的代码,关于

代码: 全选

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":
当然在 standard c++ library 的实现中是

代码: 全选

template <typename _CharT, typename _Traits> basic_istream<_CharT, _Traits>& operator >>(...)
但在实际的代码中类似

代码: 全选

cin >> a >> b;
我不太清楚有什么意义,因为 a b 的先后你并不能确定,iso 98 并没有规定该哪个先,你可以试试

代码: 全选

int a = 100; cout << a++ << ',' << ++a << ',' << a; 
看看,结果很出乎意料的

代码: 全选

102,100,102 // gcc
101,102,102 // vc6 
而且各种 C++ 的实现未必都相同。
Triangle
帖子: 8
注册时间: 2006-08-16 19:16

Re: [问题]帮忙看个超简单的C++ 程序

#22

帖子 Triangle » 2006-08-18 16:56

lqw0205 写了:

代码: 全选

#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;
}
在我的机器上始终跳不出while循环,why, why, why?
cin在linux是以什么判定终结的?
我认为cin这是一个函数,当执行cin>>word后函数会返回一个值,你输入了那当然返回1了,while(1)还不死循环那!
头像
ectotherm
帖子: 225
注册时间: 2006-05-23 13:35
联系:

#23

帖子 ectotherm » 2006-08-18 17:41

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":
这个是我贴的 Stroustrup 的代码,他都有解决的办法了,建议你去他的网站上仔细看看,linux 终端上 Ctrl + D 可以。

谁说了 cin >> word 一定返回 1 呢?
dyson
帖子: 4
注册时间: 2005-07-23 0:16

#24

帖子 dyson » 2006-08-20 13:27

我觉的就从文件重定向输入,这段代码应该没问题的吧。
当到文件末尾标记 EOF 的时候, cin 操作返回一个失败(不过没测试过返回什么值),while 结束。
但是在控制台下输入的时候, 因为结束程序的方式在不同的平台下面是不同的。编译器对于
cin 操作的实现可能也不同。
以上是我的一些想法。
dark_moon
帖子: 2
注册时间: 2006-04-12 16:05

#25

帖子 dark_moon » 2006-08-21 16:46

这段代码有问题
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;
}
Rainarrow
帖子: 16
注册时间: 2006-03-06 2:04

#26

帖子 Rainarrow » 2006-10-19 3:02

是这样的
Windows下,Ctrl+Z就是模拟EOF
POSIX下要在一行开始输入Ctrl+D才是EOF
有可能是这个原因
回复