注:C++ Primer Plus,2019-09-11
1 cstring相关I/O
1-1,cin
使用空白(空格,制表符, 换行符)来确定字符串的结束位置,即cin在获取字符数组输入时只获取一个单词,
错误代码示例:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
const int Arsize = 20;
char name[Arsize];
char food[Arsize];
cout << "Enter your name : \n";
cin >> name;
cout << "Enter your favourite dessert: \n";
cin >> food ;
cout <<"i happen to have " << food << "for you";
return 0;
}
运行结果
这里本应该输入两次,但是第一次的输入中含有空格,出现了bug
正常的结果:
显然cin不能满足需求,比如有时候用户输入 new york…