In C + + keyboard input a string __c++

Source: Internet
Author: User

Small white today in writing a C + + program, want to keyboard input character array, stupid use for (i=0;i<20;i++) cin>>a[i], but when debugging found that if the keyboard input Xiao Bai Hao Shuai, the program can only get Xiao , can not obtain the complete "Xiao Bai Hao Shuai", after searching a circle on the net,

The conclusion is: >> will filter out invisible characters (spaces, tab, enter)

Little White then began to think of the way to identify the string input for invisible characters, and conclude as follows:

method One:

Cin.get (character array name, number of characters N, terminating character)

For example:

#include <iostream>
using namespace Std;
Main ()
{
Char str1[20];
Cin.get (str1,20);
cout<<str1<<endl;
}

Input: Xiao Bai Hao Shuai
Output: Xiao Bai Hao Shuai

Input: iamstudingc++anditissointeresting (enter 34 characters)
Output: Iamstudingc++andi (receives 19 characters + 1 "")

Note: 34 characters are entered at the second input, although we will get the first 19 to STR1, but this also causes the array to cross over, and if the next statement is a CIN input stream, it will cause the program to hang

Method Two:

Cin.getline (character array (or character pointer), number of characters, terminating flag character)

For example:

#include <iostream>
using namespace Std;
Main ()
{
Char str1[20];
Cin.getline (str1,5);
cout<<str1<<endl;
}

Input: Xiaobai
Output: Xiao

Accept 5 characters into str1, where the last one is ' I ', so see only 4 characters output;

If you change 5 to 20:
Input: Xiao Bai Hao Shuai
Output: Xiao Bai Hao Shuai

Input: Wo ai Steve Jobs
Output: Wo ai Steve Jobs

Attention:

In the example, the first input, 7 characters, although we will get the first 4 to STR1, but this also caused the array of bounds, if the next statement is a CIN input stream, it will cause the program to hang out

When the third argument is omitted, the system defaults to ' the '
If the example Cin.getline () is changed to Cin.getline (str1,5, ' a '), when the input JLKJKLJKL output jklj, input wo ai steve jobsl, output wo ai

When used in a multidimensional array, you can also use the Cin.getline (str1[i],20) Usage:

Method Three:

Gets (character array or character pointer)

For example:

#include <iostream>
using namespace Std;
Main ()
{
Char str1[20];
Gets (STR1);
cout<<str1<<endl;
}

Input: Xiao Bai Hao Shuai
Output: Xiao Bai Hao Shuai

Input: Wo ai Steve Jobs
Output: Wo ai Steve Jobs

Summarize:

1. Like gets and Cin.getlint () and Getline (), no bounds are checked for the buffer of the string, and if the string crosses the line when entered, the program may crash

2. function fgets () capable of cross-border checking of string input, but small white is debugging on its own Visual C + + 2008, and the program is still crashing, not knowing if the version of Visual C + + is lower, for verification.

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.