Usage of c++:cin, Cin.getline (), Getline ()

Source: Internet
Author: User

Usage of c++:cin, Cin.getline (), Getline ()

Main content:

1. CIN Usage

2, Cin.getline () usage

3, getline () usage

3. Issues of attention

First, cin>>

Usage 1: Enter a number or character

#include <iostream>
using namespace Std;
Main ()
{
int A, B;
cin>>a>>b;
cout<<a+b<<endl;
}

Usage 2: Receive a string, the "Space", "tab", "Enter" the end

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

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLJKL JKLJKL//In the event of a space end
Output: JKLJKL

Second, Cin.getline ()

Usage: Receives a string that can receive a space and output

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

Input: JKLJKLJKL
Output: JKLJ

receive 5 characters into M, where the last one is ' s ', so only see 4 characters output ;

If you change 5 to 20:
Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLF FJLSJF FJSDKLF
Output: JKLF fjlsjf FJSDKLF

Extended:
1, Cin.getline () actually has three parameters,cin.getline (the variable that receives the string, the number of characters received, the ending character)
2, when the third parameter is omitted, the system defaults to '
3, if the example of Cin.getline () is changed to Cin.getline (m,5, ' a '), when the input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK

Third, Getline ()

Usage: Receives a string, can receive the space and output, need to include "#include <string>"

#include <iostream>
#include <string>
using namespace Std;
Main ()
{
String str;
Getline (CIN,STR);
cout<<str<<endl;
}

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKL JFKSLDFJ JKLSJFL
Output: JKL jfksldfj JKLSJFL

Iv. issues of attention

1,Cin.getline () belongs to the IStream stream, and Getline () belongs to the string stream, which is a different two function

2. When using Cin>>,getline () at the same time, it is important to note that after the cin>> input stream is complete, getline ()

Str= "\ n";
Getline (CIN,STR);

The carriage return as the input stream cin to clear the cache, and if not, the input hint of getline () will not appear on the console and skip directly because the program defaults to the previous variable as the input stream.

Look at the following procedure:

/*-------------Basic input/output-------------*/
#include <iostream>
#include <string>
#include <sstream>
using namespace Std;

int main () {
int age;
Standard input (CIN)
cout<< "Please enter a integer value as your age:";
cin>>age;
cout<< "Your ager is:" <<age<< ". \ n";
CIN and string
String mystr;
cout<< "What ' s your name?" "<<endl;
mystr= "\ n";
getline (CIN,MYSTR);
Getline (CIN,MYSTR);
cout<< "Hello," <<mystr<< ". \ n";
char sex;
cout<< "Please enter a F or M as your sex:";
cin>>sex;
cout<< "Your sex is:" <<sex<<endl;
cout<< "What ' s your favorite team?" ";
mystr= "\ n";
getline (CIN,MYSTR);
Getline (CIN,MYSTR);
cout<< "I like" <<mystr<< ". \ n";

System ("pause");
return 0;
}

The result of the operation is:

If you do not add a red code, the result is:

In order to solve the unwanted characters left in the queue, we can solve them in two ways.
The first one is to eat this character, call once Cin.get ()
The second one is to have this character, called Once Cin.ignore ();

IStream & Istream::get (char *, int, char = ' \ n ');
IStream & Istream::getline (char *, int, char = ' \ n ');
Function: Extracts the specified number of strings from the text and adds a null character to the end of the string array.
Difference: Get () does not extract the terminating character from the stream, and the terminating character is still in the input stream. Getline () extracts the terminating character from the stream, but the terminating character is discarded.

Usage of c++:cin, Cin.getline (), Getline ()

Related Article

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.