Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- java
- happen to
- keep -ing
- continue to
- 제5인격
- by any chance
- sort함수
- gameQA
- end up ing
- by until
- 명세기반테스트
- ISTQB
- relif
- 형변환
- if조건절
- 코로나19
- html
- I'm glad
- metascore
- 게임QA
- C++
- Realtime Rendering
- 명절 표현
- counldn't have
- it's a good thing
- might have p.p
- 변수
- for ing
- UE4
- know
Archives
- Today
- Total
Records rather than Memories
[C++] cout, cin, endl 본문
C++에서 출력을 하기 위해서 어떻게 해야할까?
std: cout (iostream 라이브러리)를 사용하면
C언어에 비해 훨씬 간결하게 출력할 수 있다.
std::cout
1
2
3
4
5
6
7
8
9
10
11
|
#include <iostream>
int main()
{
std::cout << "Quest";
return 0;
}
|
cs |
다음과 같이 std::cout << 옆에 출력하고자 하는 텍스트를 넣으면 출력이 된다.
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <iostream>
int main()
{
int x = 10;
std::cout << "Quest"; << 10;
return 0;
}
|
cs |
또한 출력 연산자(<<)를 추가하면 둘 이상 계속해서 출력이 가능하다.
std::endl
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <iostream>
int main()
{
std::cout << "This is ";
std::cout << "Quest"; << 10;
return 0;
}
//This is Quest
|
cs |
다음과 같이 std::cout만 사용하게 되면 줄 바꿈 없이 이어져 출력이 이어진다.
따라서 여러 줄을 출력하고 싶다면 std::endl를 사용하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <iostream>
int main()
{
std::cout << "This is " << std::endl;
std::cout << "Quest"; << 10;
return 0;
}
|
cs |
다음과 같이 코드를 짜면 줄바꿈이 이루어져 텍스트가 출력된다.
std::cin
std::cin은 사용자로부터 데이터를 입력받아 변수에 할당이 가능하게 한다.
아래 예제를 실행하면 "퀘스트를 수락하시겠습니까? (Yes : 1 No : 2)"라는 텍스트가 출력되고 사용자의 입력을 기다리게 된다. 1을 입력했다면 "당신은 1 번을 선택하셨습니다." 가 출력된다. 이 안에는 텍스트와 입력한 수가 출력된 것이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
int main()
{
std::cout << "퀘스트를 수락하시겠습니까? (Yes : 1 No : 2)" << std::endl;
int x;
std::cin >> x;
std::cout << "당신은"; << x << "번을 선택하셨습니다" << std::endl;
return 0;
}
|
cs |
- std::cout 와 std::cin은 명령문 왼쪽에 위치한다.
- 출력 연산자 : <<
- 입력 연산자 : >>
'Software > C' 카테고리의 다른 글
[C++] 키워드(keyword)와 식별자(naming identifiers) (0) | 2019.12.29 |
---|---|
[C++] 변수의 초기화와 할당 (0) | 2019.12.28 |
네트워크 플로우(Network flow (0) | 2019.12.22 |
위상 정렬(Topology Sort) (0) | 2019.12.13 |
플로이드 와샬(Floyd Warshal) 알고리즘 (0) | 2019.12.11 |