c ++ read data

1, china

(1) input digital

Encounter a space, carriage return, tab end returns a reference.

#include <iostream> 
using namespace std; 
main () 
{ 
    int a,b; 
    cin>>a>>b; 
    cout<<a+b<<endl; 
}

 

(2) Read a string

In case of "space", "TAB", "Enter" is over

#include <iostream> 
using namespace std; 
main () 
{ 
    char a[20]; 
    cin>>a; 
    cout<<a<<endl; 
}

Input: jkljkljkl 
output: jkljkljkl

 
 

Input: jkljkl jkljkl // end of the event space 
Output: jkljkl

2、cin.get (char *str, int maxnum)

(1) reads a character

#include <the iostream> 
 the using  namespace STD; 
main () 
{ 
    char CH; 
    CH = CIN. GET ();                // or cin.get (CH); 
    COUT CH << << endl; 
} 

Input: jljkljkl 
Output: j

(2) an array of characters

cin.get(char *str, int maxnum+1)

Can read spaces, encountered the end of the line breaks, while \ n remain in the stream , the first is \ n when read again, then it is generally in a cin.get (no arguments) to discard \ n. Such re-use the get function, the first character is the first character of the next line.

#include <the iostream> 
 the using  namespace STD; 
main () 
{ 
    char a [ 20 is ]; 
    . CIN GET (a, 20 is ); // read into a character into the read 20-1 = 19 characters, and added to the end A \ 0 
    COUT A << << endl; 
} 

input: jkl jkl jkl 
output: jkl jkl jkl 

input: abcdeabcdeabcdeabcdeabcde (input 25 characters) 
output: abcdeabcdeabcdeabcd (19 characters receiving +1 ' \ 0 ' )

3, cin.getline (char * str, int maxnum) (string class variable may be used)

Accepts a string, enter the end face, and at the same time discarded line breaks .

Usage and get the same acceptance and output spaces

4、getline

#include <string>

To define a variable of type string

getline(cin,str);

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11298081.html