Summary of C/C++ Input and Output

Summary of Input and Output

Article directory

input

Only one set of input data

#include <bits/stdc++.h>
using namespace std;

int main() {
	int a, b;
	scanf("%d%d", &a, &b);
	
	printf("%d %d", a, b);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a << " " << b << endl;
	return 0;
}

There are multiple sets of test data until the end of the input file is read

#include <bits/stdc++.h>
using namespace std;

int main() {
	int a, b;
	while(scanf("%d %d", &a, &b) != EOF)
	{
	    printf("%d %d", a, b);
	    printf("\n");
	}   
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    while(cin >> a >> b)
        cout << a << " " << b << endl;
	return 0;
}

Input an N at the beginning, followed by N sets of data

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    scanf("%d", &n);
	while(n--)
	{
	    int a, b;
	    scanf("%d %d", &a, &b);
	    printf("%d %d", a, b);
	    printf("\n");	    
	}
 
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b, n;
    cin >> n;
    while(n--)
    {
        cin >> a >> b;
        cout << a << " " << b << endl;
    }
        
	\return 0;
}

Input of strings

Receive a string, encounter a space, Tab, Enter end

#include <bits/stdc++.h>
using namespace std;

int main() {
    char a[20];
    cin >> a;
    cout << a << endl;
        
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
	string str;
	cin >> str;
	cout << str << endl;
	return 0;
}

Receive characters or strings using cin.get()

Receiving character

#include <bits/stdc++.h>
using namespace std;

int main() {
    char ch;
    ch = cin.get();
    cout << ch << endl;
        
	return 0;
}

Receive strings and spaces

#include <bits/stdc++.h>
using namespace std;

int main() {
    char a[20];
    cin.get(a,20);
    cout << a << endl;
        
	return 0;
}

Receive a string with cin.getline(), which can receive spaces and output them

#include <bits/stdc++.h>
using namespace std;

int main() {
    char m[20];
    cin.getline(m,20);
    cout << m << endl;
        
	return 0;
}

Receiving multidimensional arrays

#include <bits/stdc++.h>
using namespace std;

int main() {
    char m[3][20];
    for(int i = 0; i < 3; i++)
    {
        cin.getline(m[i],20);
        cout << m[i] << endl;
    }
    
	return 0;
}

Usage of the need to include string header file: getline() receives strings containing spaces

#include <bits/stdc++.h>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    cout << str << endl;
    
	return 0;
}

Use of a string header file: gets() receives a string containing spaces

#include <bits/stdc++.h>
using namespace std;

int main() {
    char m[20];
    gets(m);
    cout << m << endl;

    system("pause");
	return 0;
}

Receiving multidimensional

#include <bits/stdc++.h>
using namespace std;

int main() {
    char m[3][20];
	for(int i = 0; i < 3; i++)
    {
	    gets(m[i]);
	    cout << m[i] << endl;
	}
    system("pause");
	return 0;
}

The use of the string header file needs to be included: getchar() receives a character

#include <bits/stdc++.h>
using namespace std;

int main() {
    char ch;
    ch = getchar();
    cout << ch << endl;

    system("pause");
	return 0;
}

while Solves Multiple Test Data Input

    while(scanf("%d", &n) != EOF);Read Read Characters
    while((c = getchar()) != EOF);

    char line[1024];
    while(gets(line));

Strings with commas, with others, are also possible

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<string> stu;
    string ss;
    cin >> ss;
     
    stringstream sstr(ss);
    string token;
    while(getline(sstr, token, ','))
    {
        stu.push_back(token);
    }    
    
    for(int i = 0 ; i < stu.size(); i++)
        cout << stu[i]<<endl;
    
    return 0;
}

Posted by peeter_talvistu on Sat, 14 Sep 2019 05:00:24 -0700