Author:
STDIN and STDOUT⚓︎
Keywords: Standard I/O
Situation⚓︎
Below is the typical question format:
Question type
STDIN⚓︎
The situation of STDIN can be separated into below types:
Load element⚓︎
Load element
C++
......
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char a;
int b;
string c;
cin >> a >> b >> c;
cout << a << " " << b << " " << c;
//input: x 1 7g qt
//output: x 1 7g
return 0;
}
Warning! Space , Enter , and Tab
>>
will filter(auto jump) all the input unseen characters.- If input type is
string
orchar[]
, the character loading will stop when meet the unseen characters.
C++
......
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
//WAY1: use "int cin.get()"
int cha,chb,chc;
cha = cin.get();
chb = cin.get();
chc = cin.get();
cout << cha << " " << chb << " " << chc << endl;
//input: a b c
//output: 97(a) 32(space) 98(b)
//WAY2: use "istream& cin.get(char& var)"
char a[4] = {};
cin.get(a, 4);
//input: a b c
//output: a_b_ ("_" means space)
return 0;
}
Warning!
- Even though
get()
returnsint
, the return value representsASCll
value.- Successful read: return the of the
ASCll
value character. When an end-of-file character is encountered, returnEOF
or-1
.
- Successful read: return the of the
- The
get()
function reads characters from the buffer WITHOUT ignoring the separator.
Load single line⚓︎
Load single line
Text Only
```cpp
......
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char a[10] = {};
char b[10] = {};
string line;
cin.getline(a,3); //read line until: length (including 'enter') == 3
cin.getline(b,5,'\n'); //indicate end symbol: '\n'
getline(cin,line); //read the whole line into string including space.
cout << a << "," << b << "," << line;
//input:ak
// ksks
// cmas;ckmaslc
//output: ak,ksks,cmas;ckmaslc
return 0;
}
```