본문 바로가기
C++ 언어

C++에서 3자리 숫자 입력 받기

by treeCoder 2021. 8. 13.

C++에서 3자리 숫자 입력 받는 방법이다.

#include <iostream>

using namespace std;

int main(int argc, char **argv) { 

            string st;

	cout << "3자리 숫자를 입력하세요." << endl;
	getline(cin, st);

	if (st.length() != 3) {
		cout << "3자리 숫자를 입력해야 합니다.\n프로그램 종료" << endl;
		return 0;
	}

	for (int i = 0; i < st.length() ; i++) {
		if (st[i] < '0' || st[i] > '9') {
			cout << "0 ~ 9 사이의 숫자를 입력해야 합니다.\n프로그램 종료" << endl;
			return 0;
		}
	}

	cout << "입력하신 숫자는 " << st << " 입니다." << endl;
}

댓글