lovemi93

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

运行结果:

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-02-13
  • 2021-05-08
  • 2021-11-14
  • 2021-04-16
猜你喜欢
  • 2021-08-29
  • 2021-10-12
  • 2021-12-10
  • 2021-09-05
  • 2022-12-23
相关资源
相似解决方案