C++_输入常用函数

来源:互联网 发布:仿百度php源码 编辑:程序博客网 时间:2024/05/20 23:40

C++ 常用输入有:cin、cin.get( )、cin.getline( )

C常用输入:getline( )、gets( )

1、cin:-- 相当于scanf

功能:输入字符串到字符输出,遇空格、Tab、回车结束

代码:

[cpp] view plaincopyprint?
  1. //输入字符串到字符数组   
  2. #include <iostream>   
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     const int len =100;  
  7.     char ch[len];  
  8.     cin>>ch;  
  9.     system("pause");  
  10. }  
//输入字符串到字符数组#include <iostream>using namespace std;void main(){const int len =100;char ch[len];cin>>ch;system("pause");}

输入:ab cd

数组接受的值:ab

功能:输入字符串到 字符串 输出,遇空格、Tab、回车结束

代码:

[cpp] view plaincopyprint?
  1. //输入字符串到字符串中   
  2. #include <iostream>   
  3. #include <string>   
  4. using namespace std;  
  5. void main()  
  6. {  
  7.     string a;  
  8.     cin>>a;  
  9.     system("pause");  
  10. }  
//输入字符串到字符串中#include <iostream>#include <string>using namespace std;void main(){string a;cin>>a;system("pause");}

输入:ab cd

字符串接受的值:ab

2、ch=cin.get();

功能:输入字符,遇空格、Tab、回车结束

代码:

[cpp] view plaincopyprint?
  1. //输入字符   
  2. #include <iostream>   
  3. #include <string>   
  4. using namespace std;  
  5. void main()  
  6. {  
  7.     char ch;  
  8.     ch=cin.get();  
  9.     cout<<ch;  
  10.     system("pause");  
  11. }  
//输入字符#include <iostream>#include <string>using namespace std;void main(){char ch;ch=cin.get();cout<<ch;system("pause");}

输入:abcd

字符接受的值:a

3、cin.getline(ch,len); --  相当于gets( )

功能:输入字符串,遇回车结束

代码:

[cpp] view plaincopyprint?
  1. //输入一行,可以包含空格,遇回车结束   
  2. #include <iostream>   
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     const int len=5;  
  7.     char ch[len];  
  8.     cin.getline(ch,len);//len表示接受的最大字符数   
  9.     cout<<ch;  
  10.     system("pause");  
  11. }  
//输入一行,可以包含空格,遇回车结束#include <iostream>using namespace std;void main(){const int len=5;char ch[len];cin.getline(ch,len);//len表示接受的最大字符数cout<<ch;system("pause");}

输入:12345678

输出:1234(最大长度为5,实际存4个,还有接个结束符\0)

4、cin.getline()与二维数组连用

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <string>   
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     char ch[3][81];  
  7.     for (int i=0;i<3;i++)  
  8.     {  
  9.         cin.getline(ch[i],81);  
  10.     }  
  11.     for (int j=0;j<3;j++)  
  12.     {  
  13.         cout<<ch[j]<<endl;  
  14.     }  
  15.     system("pause");  
  16. }  
#include <iostream>#include <string>using namespace std;void main(){char ch[3][81];for (int i=0;i<3;i++){cin.getline(ch[i],81);}for (int j=0;j<3;j++){cout<<ch[j]<<endl;}system("pause");}

 

注意:

实际应用:接受单个字符,使用cin,接收字符串cin.getline( )

原因:虽然cin.get( )不仅可以接受字符,还可以接受字符串。但是接受完数据后,还会有回车在缓冲区中,下次在接受数据时,会自动把这个回车给下个变量,这就会产生问题,要想使用它,还要在cin.get( )语句后面接受回车。使用cin.get( ).

错误代码:

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <string>   
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     char ch;  
  7.     ch=cin.get();  
  8.     cout<<ch;  
  9.     ch=cin.get();  
  10.     cout<<ch;  
  11.     system("pause");  
  12. }  
#include <iostream>#include <string>using namespace std;void main(){char ch;ch=cin.get();cout<<ch;ch=cin.get();cout<<ch;system("pause");}

正确代码:

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <string>   
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     char ch;  
  7.     ch=cin.get();  
  8.     cin.get();// 接收回车   
  9.     cout<<ch;  
  10.     ch=cin.get();  
  11.     cin.get(); //接收回车   
  12.     cout<<ch;  
  13.     system("pause");  
  14. }  
#include <iostream>#include <string>using namespace std;void main(){char ch;ch=cin.get();cin.get();// 接收回车cout<<ch;ch=cin.get();cin.get(); //接收回车cout<<ch;system("pause");}

 

原创粉丝点击