[C , C++]

C++ test case 입력 개수 모를 때 입력 받기

말하는펭귄 2021. 1. 21. 13:18
728x90
반응형

 

1. getc(stdin) == '

 

엔터를 입력하면 반복문 탈출

 

#include <iostream>
using namespace std;

int main() {
	int n;

	do {
		cin >> n;
	} while (getc(stdin) == ' ');

	cout << "end";
}

 

 

2. cin, scanf

 

데이터 소스에서 더 이상 읽을 데이터가 없어 EOF(End Of File)를 리턴하면 종료

백준 알고리즘 문제 풀이 등에 사용 가능

사용자의 입력을 직접 받을 때는 사용자가 종료하기 전에는 종료X

 

#include <iostream>
using namespace std;

int main() {
	int n;

	while (cin >> n) {
		cout << n << endl;
	}

	cout << "end";
}

 

 

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
using namespace std;

int main() {
	int n;

	while (scanf("%d", &n) != EOF) {
		printf("%d \n", n);
	}

	printf("end");
}

 

 

 

728x90
반응형