C++入门经典-例6.8-gets_s与puts的应用
1:使用标准输入函数cin和格式化输入函数scanf时都存在这样一个问题:当输入空格时,程序不会接受空格符之后的内容内容。
输入函数gets_s与输出函数puts都只以结束符'\0'作为输入\输出结束的标志。
代码如下:
// 6.8.cpp : 定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include<iostream>
using namespace std;
#include<string>
void main()
{
char str1[30],str2[30],str3[30],temp[30];
cout<<"请使用scanf和cin输入Hello World!!"<< endl;
scanf("%s",str1);//输入的时候输入了空格,表示结束了,于是输入了Hello
cin>>str2;//默认为Word
cout<<"str1:";
printf("%s\n",str1);
cout<<"str2:";
cout<<str2<<endl;
cout<<"输入流中残留了cin留下的空格符',使用gets接收它:"<<endl;
gets_s(temp);//可以直接输入hello Word
cout<<"temp:"<<temp<<endl;
cout<<"请使用gets输入Hello World!!:"<<endl;
gets_s(str3);
cout<<"str3:";
puts(str3);
}
View Code
运行结果: