본문 바로가기
반응형

분류 전체보기38

5-3. C++ std::cin.fail(), std::cin.clear(), std::cin.ignore() 함수 cin.ignore() cin.ignore함수는 첫번째 parameter 개수만큼 문자를 읽어온 뒤 버리거나, 두번째 parameter(delim)에 해당하는 문자를 만나면 해당 문자까지 읽고 버린다. 둘중 먼저 만족하는 조건을 따르게 된다. int main() { int x; while (true) { cout > x; if (cin.fail()) { std::cin.clear(); std::cin.ignore(32767, '\n'); cout 2024. 2. 7.
5-2. C++ 난수 만들기 난수 만들기 (Random Number Generation) 실행할 때마다 다른 난수 생성하기 #include #include // std::rand(), std::srand() #include // std::time() using namespace std; int main() { std::srand(static_cast(std::time(0))); for (int count = 1; count 2024. 2. 5.
5-1. C++ switch-case, do-while switch-case 특별한 경우에 if문보다 깔끔하고 간결하게 코딩을 할수있다. #include using namespace std; enum class Colors { RED, // 0 GREEN, // 1 BLUE, // 2 }; void printColor(Colors Color) { switch(static_cast(Color)) // cast를 통해 case를 정수로 { case 0 : cout 2024. 2. 3.
4-3. C++ 자료형에게 가명 붙이기 (type aliases), 구조체 (struct) 자료형에게 가명 붙이기 (Type aliases) typedef 뒤에 이름을 붙여주고 싶은 자료형을 써주고 가명을 사용한다. #include #include int main() { using namespace std; typedef vector pairlist_t; // using pairlist_t = vector 로도 사용가능 pairlist_t pairlist1; pairlist_t pairlist2; return 0; } 내 프로그램에서 이 자료형을 표현할때 이 가명을 쓰겠다고 선언하는 것이다. 기본적으로 긴 문장을 짧게 줄이는데 매우 편리하게 사용할 수 있다. 구조체 (struct) 다양한 요소들을 묶어서 마치 하나의 자료형인 것처럼, 하나의 사용자 정의 자료형인 것처럼 사용할 수 있게 해주는 .. 2024. 2. 2.
4-2. C++ 문자열, 열거형, 열거형 클래스 문자열 (string) C++ 언어에서 기본적으로 제공해주는 것은 한 글자이다. 한 글자를 여러개 나열하는 방식으로 문자열을 사용한다. #include #include #include int main() { using namespace std; cout > name; std::getline(std::cin, name); cout > age; // std::cin.ignore(32767, '\n'); std::cin.ignore(std::numeric_limits::max(), '\n'); string abc = "abcdefg"; cout 2024. 2. 2.
4-1. C++ 전역변수, 정적변수, 내부연결, 외부연결 전역 변수(Global Variable) #include using namespace std; int a = 100; int main() { cout 2024. 2. 2.
반응형