C ++: Brush title input

Ⅰ, standardize

Note: The inputs are multiple sets of input, not a group

(1) Input data in advance without entering the number of sets of

while(cin>>a>>b){
    cout<<a+b<<endl;
}

(2) to know in advance the number of data sets

cin>>n;
for(int i=0; i<n; i++){
    int a,b;
    cin>>a>>b;
    cout<<a+b<<endl;
}

Ⅱ, the relevant input syntax

A, 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: receiving a string, the case of "space", "TAB", "Enter" to end

#include <iostream>
using namespace std;
main ()
{
char a[20];
cin>>a;
cout<<a<<endl;
}
//输入:jkljkljkl
//输出:jkljkljkl

//输入:jkljkl jkljkl       //遇空格结束
//输出:jkljkl

二, cin.getline ()

Usage: receiving a string, and may receive output space

#include <iostream>
using namespace std;
main ()
{
char m[20];
cin.getline(m,5);
cout<<m<<endl;
}
//输入:jkljkljkl
//输出:jklj

//接收5个字符到m中,其中最后一个为'\0',所以只看到4个字符输出;

//如果把5改成20:
//输入:jkljkljkl
//输出:jkljkljkl

//输入:jklf fjlsjf fjsdklf
//输出:jklf fjlsjf fjsdklf

Extension:
. 1, cin.getline () actually has three parameters, cin.getline (received string variable, the number of received character, end character)
2, when the third argument is omitted, the system defaults to '\ 0'
3, if the example cin.getline () to cin.getline (m, 5, 'a '); jklj output when the input jlkjkljkl, input jkaljkljkl, output jk

Three, getline ()

Usage: receiving a string, and may receive output space, must include "#include"

#include<iostream>
#include<string>
using namespace std;
main ()
{
string str;
getline(cin,str);
cout<<str<<endl;
}
//输入:jkljkljkl
//输出:jkljkljkl

//输入:jkl jfksldfj jklsjfl
//输出:jkl jfksldfj jklsjfl

Fourth, pay attention to the problem

1, cin.getline () istream stream belongs, and getline () belongs string flow, is a function of two different

2, when used simultaneously cin >>, getline () , to be noted that, after completion of the input stream cin >>, before getline (), need

str="\n";
getline(cin,str);

Way carriage return as an input stream cin to clear the cache, if you do not do so, it will not appear on the console getline () input prompts, and skip, because the program will default before the variable inputs flow.

Look at the following program:
running version:

/*-------------Basic Input/Output-------------*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
 
int main(){
    int age;
    //standard input(cin)
    cout<<"Please enter an 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;
}
/*输出:
Please enter an integer value as your age: 16
Your ager is: 16.
What's your name?
jiaos
Hello,jiaos.
Please enter a F or M as your sex: F
Your sex is: F
What's your favorite team? programming
I like programming.
*/

Non-functioning version:

/*-------------Basic Input/Output-------------*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main() {
	int age;
	//standard input(cin)
	cout << "Please enter an 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;
}
/*输出:
Please enter an integer value as your age: 16
Your ager is: 16.
What's your name?
Hello,.
Please enter a F or M as your sex: F
Your sex is: F
What's your favorite team? I like .
*/

Reference blog:
Reference 1
invasion deleted

Released three original articles · won praise 0 · Views 246

Guess you like

Origin blog.csdn.net/weixin_43991826/article/details/105223193