主题:[讨论]多个while(cin>>str)的使用问题
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> svec1,svec2;
string svec;
cout<<"Enter the first string:(ctrl+z to end)"<<endl;
while(cin>>svec)
svec1.push_back(svec);
cin.clear();
cin.ignore();
cout<<"Enter the second string:(ctrl+z to end)"<<endl;
while(cin>>svec)
svec2.push_back(svec);
if(svec1 == svec2)
cout<<"string1 is equal to string2"<<endl;
else
cout<<"string1 is not equal to string2"<<endl;
system("pause");
return 0;
}
为什么需要 换行 键入ctrl+z 回车后,再换行,才能进入下一个while循环的输入
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> svec1,svec2;
string svec;
cout<<"Enter the first string:(ctrl+z to end)"<<endl;
while(cin>>svec)
svec1.push_back(svec);
cin.clear();
cin.ignore();
cout<<"Enter the second string:(ctrl+z to end)"<<endl;
while(cin>>svec)
svec2.push_back(svec);
if(svec1 == svec2)
cout<<"string1 is equal to string2"<<endl;
else
cout<<"string1 is not equal to string2"<<endl;
system("pause");
return 0;
}
为什么需要 换行 键入ctrl+z 回车后,再换行,才能进入下一个while循环的输入