字符串输入连接比较还有关于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;
}
#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;
}
相关文章推荐
- BitSet
- Linux键盘快捷键
- 你不知道的关于计算机大师Dijkstra的事情
- Servlet的原理是什么?
- 经典SQL练习题
- vim
- HTML元素的宽度计算
- [LeetCode129]Sum Root to Leaf Numbers
- [Kali Linux] Kali Linux2.0 安装后的一些基本配置
- 多处理机、多核cpu、多线程cpu的区别
- 什么是真正的贫穷,什么才值得你自卑?!
- OGNL表达式的基本语法和用法
- 浅谈进制转换
- 自定义图片在上边,文字在下边的按钮
- linux svn搭建
- 开源新闻速递:Linux 内核 4.5 结束支持,发布了该系列最后一个版本
- 八类大学毕业生求职时不受欢迎
- MySQL执行sql查询并上传至远程服务器
- 防丢App记录(4)--开机启动service检测sim卡的串码和之前保存的是否相同
- Sparse Autoencoder 编程练习