Introduction to C + + Classic-Example 6.8-gets_s and puts application

Source: Internet
Author: User

1: The problem exists when using the standard input function, CIN, and the format input function scanf: When a space is entered, the program does not accept the content after the whitespace.

Both the input function gets_s and the output function puts only end with the Terminator ' \ n ' as the input/output end flag.

The code is as follows:

//6.8.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd; #include<string>voidMain () {Charstr1[ -],str2[ -],str3[ -],temp[ -]; cout<<"Please enter Hello world!! using scanf and CIN"<<Endl; scanf ("%s", str1);//Enter a space when entered, indicating the end, and then entered the Hellocin>>str2;//default to Wordcout<<"str1:"; printf ("%s\n", STR1); cout<<"str2:"; cout<<str2<<Endl; cout<<"The whitespace left by CIN remains in the input stream, which is used by the get to receive it:"<<Endl; gets_s (temp);//You can enter Hello Word directlycout<<"Temp:"<<temp<<Endl; cout<<"Please enter Hello world!! with gets:"<<Endl;    gets_s (STR3); cout<<"STR3:"; Puts (STR3);}
View Code

Operation Result:

Introduction to C + + Classic-Example 6.8-gets_s and puts application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.