您的位置:首页 > 其它

字符串输入连接比较还有关于cin的问题

2016-06-09 20:25 387 查看
连接比较调用类就可以了,但是一开始把几个东西写一起总是不过,觉得很神奇......后来发现时cin输出流的问题,因为残留了个回车,所以清了就好了。

#include<iostream>
#include<string>
using namespace std;
int main(){
string word;
string line;
string s1, s2,zong;
zong = "";
//一个词一个词读
cout << "逐个读" << endl;
while (cin >> word)
{
cout << word << endl;
}
cin.clear(); //把输入流的换行符清掉!!!!!一定要有,否则下面的不执行!
system("PAUSE");
// 一行一行读
cout << "按行读" << endl;
while (getline(cin, line))
{
cout << line << endl;
}
cin.clear(); //把输入流的换行符清掉!!!!!一定要有,否则下面的不执行!
system("PAUSE");
//输入两个字符串比大小
cout << "比大小" << endl;
cin >> s1 >> s2;
if (s1 == s2)
{
cout << "equal" << endl;
}
else if (s1 >= s2)
cout << s1 << endl;
else
cout << s2 << endl;
cin.clear(); //把输入流的换行符清掉!!!!!一定要有,否则下面的不执行!
system("PAUSE");
cout << "连接字符串" << endl;
while (cin >> word){
zong = zong + word;
}
cout << zong << endl;
system("PAUSE");
return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: