//编译环境:linux GCC
# g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
# vi cin_get.cpp //创建cin_get.cpp文件,并开始编辑。
// 此程序说明,cin.get(array_name,array_size)只读取控制台上输入的那一行数据,而“Enter”键被保留在队列中!
// 紧挨着的下一条cin.get( name2,size2),会直接读取队列中的“Enter”,导致读取数据失败!
// 所以,两个cin.get(name,size)之间要增加一条cin.get()来吸收"Enter".
#include<iostream>
#include<string>
#include <iostream>
using namespace std;
int main()
{
char n1[25];
char n2[25];
cout<<"请输入两行不超过25字符的字符串,用Enter分行!"<<endl;
cin.get(n1, 25);
cin.get(); //读取上条输入语句之后的换行符(Enter)
cin.get(n2, 25);
cout << "there is a famous saying goes:\n" << n1;
cout << n2;
cin.get(); //读取上条输入语句之后的换行符(Enter)
cin.get(); //等待键入换行符,退出控制台!
}
保存并退出vi。
#g++ -o cin_get.out cin_get.cpp //编译
#./cin_get.out //执行
请输入两行不超过25字符的字符串,用Enter分行!
Tom and Jerry,
They are friends!
there is a famous saying goes:
Tom and Jerry,They are friends!
----------------------------------------------------------------------------------------------------
# vi cin_getline.cpp //创建cin_getline.cpp 并开始编辑
#include<iostream>
using namespace std;
int main()
{
char n1[20],n2[20];
cout<<"cin.getline(array_name,array_size),会读取控制台输入的一整行数据,以及换行符Enter,";
cout<<"但是,随后换行符会被自动替换成空字符'\0'"<<endl;
cout<<"所以,内存队列中,不存在任何值了,不会影响下一条cin.getline的输入读取。"<<endl;
cout<<" 现在开始检验,请输入两行数据,以Enter换行:"<<endl;
cin.getline(n1,20);
cin.getline(n2,20);
cout<<"there is a famous saying goes:"<<n1<<n2<<endl;
return 0;
}
保存并退出。
#g++ -o cin_getline.out cin_getline.cpp //编译
# ./cin_getline.out //执行
cin.getline(array_name,array_size),会读取控制台输入的一整行数据,以及换行符Enter,但是,随后换行符会被自动替换成空字符'
所以,内存队列中,不存在任何值了,不会影响下一条cin.getline的输入读取。
现在开始检验,请输入两行数据,以Enter换行:
Tom and Jerry,
They are friends!
there is a famous saying goes:Tom and Jerry,They are friends!