请教一下如何结束while(cin>>str)
比如:int main()
{
int i=0;
string str;
while(cin>>str)
{
i++;
}
if(i>0)
{
cin>>i;
}
cout<<i<<endl;
return 0;
}
请问如何结束这个输入字符串的while循环而执行下面的语句呢?我在网上查有人说 ctrl+z,ctrl+d。 我都试了,这两个会直接跳出程序而不是跳出循环。
程序代码:
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main(void)
{
int i=0;
string str;
while(cin>>str)
{
i++;
}
if(i>0)
{
cin>>i;
cout << "执行过这里" << endl;
}
cout<<i<<endl;
return 0;
}
