Search

ํ‘œ์ค€ ์ž…์ถœ๋ ฅ

ํƒœ๊ทธ

ํ‘œ์ค€์ž…๋ ฅ ์‹œ ๊ณต๋ฐฑ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ

#include <bits/stdc++.h> using namespace std; int main() { // cin์˜ ๊ณต๋ฐฑ๋ฌธ์ž์—ด ์ด์Šˆ string s1; cout << "input s1 : "; cin >> s1; cout << "s1 is " << s1 << '\n'; // cin ์ž…๋ ฅ ๋ฒ„ํผ ๋น„์šฐ๊ธฐ cin.ignore(); // ๋˜๋Š” ์•„๋ž˜ ๋ฐฉ์‹ /* string bufferflush; getline(cin, bufferflush); */ /** input s1 : hi hello! s1 is hi cin ์„ ๊ณต๋ฐฑ ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌ ๋ชป ํ•จ! */ // getline ์€ ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ ํฌํ•จํ•ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ string s2; cout << "input s2 : "; getline(cin, s2); cout << "s2 is " << s2 << '\n'; /** input s2 : hi hello! s2 is hi hello! */ string s3; cout << "input s3 : "; getline(cin, s3, '!'); // ์ข…๊ฒฐ๋ฌธ์ž๋ฅผ '!'๋กœ ๋ฐ›์•„์„œ ๊ทธ ์ „๊นŒ์ง€๋งŒ ๋ฐ›์Œ cout << "s3 is " << s3 << '\n'; /* input s3 : hi hello! s3 is hi hello */ }
C++
๋ณต์‚ฌ

ํ‘œ์ค€ ์ž…๋ ฅ ์‹œ ์ž…๋ ฅ๋ฒ„ํผ ์ฒ˜๋ฆฌ

#include <bits/stdc++.h> using namespace std; int main() { string str1; string str2; // 1. cin -> cin // ๋‹ค์Œ cin์ด ๋ฒ„ํผ์˜ ๊ณต๋ฐฑ ๋ฐ ๊ฐœํ–‰๋ฌธ์ž ๋ฌด์‹œํ•˜๋ฏ€๋กœ ๋ฒ„ํผ ๋น„์›Œ์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค cin >> str1; cin >> str2; cout << str1 << " " << str2 << "\n"; // 2. cin -> getline // ๋‹ค์Œ getline์€ ์ „ ๋ฒ„ํผ์˜ ๊ณต๋ฐฑ ๋ฐ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฏ€๋กœ ๋ฒ„ํผ ๋น„์›Œ์ค˜์•ผ ํ•œ๋‹ค cin >> str1; cin.ignore(); // ๋ฒ„ํผ์˜ '\n' ๋น„์›Œ์ฃผ๊ธฐ getline(cin, str2); cout << str1 << " " << str2 << "\n"; // 3. getline -> getline ๋˜๋Š” cin // getline์€ ์• ์ดˆ์— '\n'์„ ์ž…๋ ฅ์œผ๋กœ ์•ˆ ๋ฐ›์œผ๋ฏ€๋กœ ๋ฒ„ํผ ๋น„์›Œ์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค. getline(cin, str1); getline(cin, str2); cout << str1 << " " << str2 << "\n"; }
C++
๋ณต์‚ฌ

์ž…์ถœ๋ ฅ ์‹œ๊ฐ„ ์ค„์ด๊ธฐ

โ€ข
ios::sync_with_stdio(0)
: C stream ๊ณผ C++ stream ์˜ ๋™๊ธฐํ™”๋ฅผ ํ•ด์ œ
โ‡’ cin/cout ๊ณผ scanf/printf ์„ž์–ด์„œ ์“ฐ๋ฉด ์•ˆ ๋จ
โ€ข
cin.tie(0)
: cin ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— cout ๋ฒ„ํผ ๋น„์šฐ์ง€ ์•Š๋„๋ก ํ•จ
๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์ „์— cout ๋ฒ„ํผ๋ฅผ ๋น„์›Œ์คŒ์œผ๋กœ์จ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ๋งž์ถฐ์ฃผ๋Š”๋ฐ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ๋Š” ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค.
โ€ข
endl ๋Œ€์‹  \n ์“ฐ๊ธฐ
: endl ์“ฐ์ง€ ๋ง๊ฒƒ