cin的问题??

aspiregao 2002-01-12 10:05:46
近日在读侯捷的Essential C++,
对第一章的习题有些疑问?
请问如下代码怎样结束输入:

#include <vector>
#include <string>
using namespace std;

void ex1_xtra1()
{
vector< string* > sp_vec;
string st;

cout << "Please enter a series of strings\n";
while ( cin >> st ) //死循环??????
sp_vec.push_back( new string( st ));

vector<string*>::iterator
iter = sp_vec.begin(),
it_end = sp_vec.end();

cout << endl;
cout << "This is what you entered, no?\n";

for ( int items = 1; iter != it_end; ++items, ++iter ) {
if ( !( items % 8 ))
// print a newline every 8th item
cout << '\n';

// *iter returns a string*; (*(*iter)) yields string
cout << (*( *iter )) << ' ';
delete *iter;
}
cout << endl;
}

int main() {
ex1_xtra1();
return 0; // quiets vc++
}

另外,对
int i
cin<<i
若输入字符型数据,会导致该段代码下的其它cin无效,该如何解决?
...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspiregao 2002-01-15
  • 打赏
  • 举报
回复
thank you!
aspiregao 2002-01-15
  • 打赏
  • 举报
回复
thank you!
hz129 2002-01-15
  • 打赏
  • 举报
回复
ctrl+d
kinghawk 2002-01-15
  • 打赏
  • 举报
回复
cin<<i是什么意思?
cream2001 2002-01-15
  • 打赏
  • 举报
回复
Ctrl+z来结束
bugbugbug 2002-01-15
  • 打赏
  • 举报
回复
while ( cin >> st )
{
if(st=="end")break;
sp_vec.push_back( new string( st ));
}
梅雪香 2002-01-15
  • 打赏
  • 举报
回复
谁有时间看呐
aspiregao 2002-01-14
  • 打赏
  • 举报
回复
怎么没人理我??
老大,帮帮忙吧?

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧