看书中不懂的!
#include <iostream>
#include <string>
int main()
{
string word;
while(cin>>word)
cout >>"word read is :">> word>>'\n';
cout>>"ok:no more words to read:bye!\n";
return 0;
}
这段是C++ primer中的,我就在C-FREE中编译了,但是不通过,有2个错误,我可和书上一样的。
求帮助!
问题点数:5、回复次数:16Top
1 楼yhbttfile(小兵)回复于 2005-08-02 23:01:17 得分 2
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
while(cin>>word)
cout <<"word read is :"<< word<<'\n';
cout<<"ok:no more words to read:bye!\n";
return 0;
}
Top
2 楼LoveYouJustOneDay(哈哈)回复于 2005-08-02 23:02:39 得分 0
#include <iostream>
#include <string>
using namespace std;
int main()
{
Top
3 楼JackyRao(一儿)回复于 2005-08-02 23:03:39 得分 0
统一楼上Top
4 楼renmo80()回复于 2005-08-02 23:12:35 得分 0
谢谢啊,我再试试Top
5 楼renmo80()回复于 2005-08-02 23:23:11 得分 0
刚试了,结果和书上不一样。奇怪
书上是说输入riverrun,past eve and adam's
书上的结果是word read is:riverrun,
word read is:past
word read is:eve
word read is:and
word read is:adam's
word read is:ok!no more words to read:bye!
而我的结果是word read is:riverrun,past
word read is:eve
word read is:and
word read is:adam's
这又是什么问题??
Top
6 楼apollon_hhy(一天五十页!)回复于 2005-08-03 02:23:41 得分 1
呵呵 ~
riverrun, past 逗号后面 要输入一个空格!Top
7 楼apollon_hhy(一天五十页!)回复于 2005-08-03 02:24:13 得分 0
输入riverrun, past eve and adam's 就行了~Top
8 楼renmo80()回复于 2005-08-03 02:41:31 得分 0
哦,我再试试Top
9 楼renmo80()回复于 2005-08-03 02:43:46 得分 0
我试过了是少了个空格,但是少了一句啊:word read is:ok!no more words to read:bye!
这又是怎么回事,不断求解中。Top
10 楼xrayshow(Shekinah)回复于 2005-08-03 03:56:57 得分 1
我也是新手,我只提出我的看法,还请各位高手指教:
我想问题存在 while(cin>>word) 这句中。。。
while(cin>>word) cout <<"word read is :"<< word<<'\n'; 是一句
而 cout>>"ok:no more words to read:bye!\n"; 是独立的一句,与 while 并无关系, while 循环不结束,cout<<"ok:no more words to read:bye!\n"; 还会执行吗?
Top
11 楼renmo80()回复于 2005-08-03 06:09:18 得分 0
同意楼上的看法,那怎么样使程序结束呢?Top
12 楼zhuying1983(竹影)回复于 2005-08-03 09:30:01 得分 0
cin >> word(word为string类型),则在标准输入中是以空格间隔的Top
13 楼Foric(人间怅惆客)回复于 2005-08-03 12:41:30 得分 0
UP
Top
14 楼afx_swallow(足球和啤酒)回复于 2005-08-03 13:36:57 得分 1
循环内加一个判断可以跳出
while(cin>>word)
{
cout <<"word read is :"<< word<<'\n';
if(word=="end") //输入end跳出
{
cout<<"ok:no more words to read:bye!\n";
return 0;
}
}Top
15 楼renmo80()回复于 2005-08-03 16:09:24 得分 0
楼上说的有道理,但是能不能找一种更好的办法?比如在输入的一行中最后是“.”或“\n”做为结束符。再试试,好了,就讨论到这,谢谢大家!!!Top
16 楼xrayshow(Shekinah)回复于 2005-08-04 01:09:20 得分 0
昨天凌晨我也想了这道题目的结束方法, 但我也没什么简洁的方法,当时就是用的
afx_swallow(足球和啤酒) 的那种。我只是新手,不知库里面有没有好的方法,你找找吧Top




