麦咭智能机器人怎么样:关于string类型的问题,求救于高人~

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 04:55:04
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
string word;
while(cin>>word)
cout<<"word read is: "<<word<<"\n";
cout<<"ok no more words to read,bye"<<endl;
return 0;
}

书上说这段程序如果输入I am chinese会输出:
word read is: I
word read is: am
word read is: chinese
word read is: ok no more words to read,bye
可是我怎么执行也不能输出word read is: ok no more words to read,bye,而且程序还结束不了 这是怎么回事啊~?望高人指教
我也是觉得该有个结束条件,可书上就这么写的,我想了半天,也不知道结束条件是什么,word[0]代表什么意思啊~?

不能这样写的,因为while循环在不停地执行cin>>word等你输入字符串,永远不会听,你应该设定一个终止条件,或者限制循环的次数。比如这样写:
string word;
while(cin>>word){
if(word[0] == '-') break;
cout<<"word read is: "<<word<<"\n";
}
cout<<"ok no more words to read,bye"<<endl;
return 0;

while退出的条件是string值为0还是为空忘记了,不过肯定是这两个中的一个,你死活不输入这个正确退出条件怎么退出,老大
不退出循环,连最后后面那句输出语句豆没执行到,怎么会输出word read is: ok no more words to read,bye
不知道是你自己程序没写完整,还是你那个书上有问题,还是我自己平时没注意还有这种写法...
main函数,我第一次看到是int类型的,而且还有返回值0哦