字符串的输入问题(如果输入的字符串带有空格)
2015-08-04 14:35
423 查看
#include<iostream> #include<string> using namespace std; int main() { char a[126]; cout<<"1 please input the str"<<endl; cin>>a; int n1=strlen(a); cout<<"n1:"<<n1<<endl; cout<<"str[]:"<<a<<endl; system("pause"); return 0; }
#include<iostream> #include<string> using namespace std; int main() { string str; cout<<"1 please input the str"<<endl; cin>>str; int n2=str.length(); cout<<"n2:"<<n2<<endl; cout<<"string str:"<<str<<endl; system("pause"); return 0; }
#include<iostream> #include<string> using namespace std; int main() { char str[126]; cout<<"3 please input the str"<<endl; scanf("%s",str); int n2=strlen(str); cout<<"n2:"<<n2<<endl; cout<<"string str:"<<str<<endl; system("pause"); return 0; }
#include<iostream> #include<string> using namespace std; int main() { char str[126]; cout<<"3 please input the str"<<endl; gets(str); int n2=strlen(str); cout<<"n2:"<<n2<<endl; cout<<"string str:"<<str<<endl; system("pause"); return 0; }
综上,要想输入带' '空格的字符串,需要使用gets()函数才行, cin scanf 都不可以,会受到空格的影响。
相关文章推荐
- Linux下php安装Redis扩展
- 接收发送给服务器的Post请求
- (leetcode)Implement Stack using Queues
- HDU 1008 Elevator
- EnvMan
- JAVA字符串格式化-String.format()的使用
- Binary Tree Level Order Traversal
- 和SimpleCursorAdapter有关的小问题
- tcpDump 抓包保存
- Objective-C学习- 便利初始化函数和便利构造器
- 推荐4个Android引导页控件
- iOS开发--UISwitch用法
- Windows批处理命令入门
- MySQL Study之--MySQL存储过程循环
- Algorithms—199.Binary Tree Right Side View
- ACM之北大——1000_A+B Problem
- android网络通信之WIFI教程实例汇总
- c++静态成员
- 推荐3个Android筛选器控件
- kafka contrib包之hadoop-consumer分析