帮忙看下代码
下面的程序运行时输入字符串,怎么不会结束啊,只会执行这一段
while ( cin >> st)
{
sp_vec.push_back( new string( st ));
}
- C/C++ code
#include <vector>#include <string>#include <iostream>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; }
[解决办法]
你google一下cin的用法
[解决办法]
要输入结束符CTRL+Z
[解决办法]
输入结束以后,按回车。然后按一下Ctrl+z,然后回车就好了...
[解决办法]
你的程序在while(cin>>st){}一直循环读入了,没有跳出循环
在你的输入后加上一个ctrl+z就行,给cin一个表示结束的标识...