UE5 강의 13 - C++ 기초 문법 개론 3
오늘 한 일
- C++ 기초 문법 개론
- 배열 확장
- NumberRunWithConsole
#include <iostream>
- NumberRunWithConsole
- 배열 확장
#include <Windows.h>
void main() { // 0 1 2 3 4 5 6 7 index char tiles[] = { ’ ’, ’-’, ’|’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’ }; int maps[][25] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, {6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, {7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, };
int currentColumns[5] = { 0, };
int ranks[5] = { 0, };
bool isGameOver = false;
while (!isGameOver)
{
Sleep(50);
system("cls");
{//이동하기
for (int i = 0; i < 5; i++)
{
int numberRow = i + 1; // 1, 2, 3, 4, 5
int numberColumn = currentColumns[i];
int temp = maps[numberRow][numberColumn]; // map[1][0]
maps[numberRow][numberColumn] = 0; // maps[1][0]=0
maps[numberRow][numberColumn + 1] = temp; // maps[1][1]=temp;
++currentColumns[i];
}
}
{//랜덤한 하나만 한번 더 이동
int randNum = rand() % 5; // 0, 1, 2, 3, 4
int randNumRow = randNum + 1; // 1, 2, 3, 4, 5
int randNumColumn = currentColumns[randNum];
int temp = maps[randNumRow][randNumColumn]; // map[1][0]
maps[randNumRow][randNumColumn] = 0; // maps[1][0]=0
maps[randNumRow][randNumColumn + 1] = temp; // maps[1][1]=temp;
++currentColumns[randNum];
}
{//게임오버
for (int i = 0; i < 5; i++)
{
if (currentColumns[i] > 23)
{
isGameOver = true;
}
}
}
{//순위
for (int i = 0; i < 5; i++)
{
ranks[i] = 0;
for (int j = 0; j < 5; j++)
{
if (i == j) continue;
if (currentColumns[i] < currentColumns[j])
{
++ranks[i];
}
}
}
}
if(isGameOver)
{//결과
int results[5] = { 0, };
int indexResult = 0;
bool isFinished = false;
for (int checkResult = 0; checkResult < 5; checkResult++)
{
for (int i = 0; i < 5; i++)
{
if (checkResult == ranks[i])
{
results[indexResult] = i + 1;
++indexResult;
}
if (indexResult >= 5)
{
break;
}
}
if (indexResult >= 5)
{
break;
}
}
{
std::cout << "결과" << std::endl;
std::cout << "-------------------------" << std::endl;
std::cout << "1등: " << results[0] << std::endl;
std::cout << "2등: " << results[1] << std::endl;
std::cout << "3등: " << results[2] << ", " << results[3] << std::endl;
std::cout << "-------------------------" << std::endl;
}
}
{//화면에 표시하기
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 25; j++)
{
char tile = tiles[maps[i][j]];
std::cout << tile;
}
std::cout << std::endl;
}
}
}
}
 2. 포인터 변수: 메모리 주소를 저장하는 변수
#include
void main() { int num = 100; int* ptr = # std::cout << “num: ” << num << std::endl; // 100 std::cout << “ptr: ” << ptr << std::endl; // a의 주소 std::cout << “*ptr: ” << *ptr << std::endl; // ptr에 들어있는 주소(a)의 값 std::cout << “&ptr: ” << &ptr << std::endl; // ptr의 주소
*ptr = 3000;
std::cout << "num: " << num << std::endl; // 3000
}
 3. 함수
#include
//함수 선언 void Print(); // 파라미터 x, 리턴 x void PrintParam(int a, int b); // 파라미터 o, 리턴 x int MaxInt(); // 파라미터 x, 리턴 o int Sum(int a, int b); // 파라미터 o, 리턴 o
//함수 실행 int main() { Print(); PrintParam(10, 20); int num = MaxInt(); std::cout << “MaxInt: ” << num << std::endl;
std::cout << "Sum: " << Sum(10, 20) << std::endl;
}
//함수 정의 void Print() { std::cout << “Hello World” << std::endl; }
void PrintParam(int a, int b) { std::cout << “a: ” << a << std::endl; std::cout << “b: ” << a << std::endl; }
int MaxInt() { return INT16_MAX; }
int Sum(int a, int b)
{
int total = a + b;
return total;
}
 4. CheckPoint5 함수화 1. 함수화 하는 김에 user의 입력을 받아서 별을 그리는 것으로 수정해봤다.
#include
int UserNumber(); void Top(int setStars); void Bottom(int setStars);
void main() { int setStars = UserNumber();
Top(setStars);
Bottom(setStars);
}
int UserNumber() { int userNumber = 0; while (true) { std::cout << “원하는 숫자(홀수)를 입력해주세요 :”; std::cin >> userNumber; std::cout << std::endl; if (userNumber % 2 != 0) { return userNumber; } } }
void Top(int setStars) { int countStar = setStars; int countBlank = 0; { while (countStar >= 1) { for (int j = 0; j < countBlank; j++) { std::cout << ” ”; } for (int k = 0; k < countStar; k++) { std::cout << ”*”; } countStar -= 2; ++countBlank; std::cout << std::endl; } } }
void Bottom(int setStars)
{
{
int countStar = 3;
int countBlank = (setStars - 3) / 2;
while (countStar <= setStars)
{
for (int j = 0; j < countBlank; j++)
{
std::cout << ” ”;
}
for (int k = 0; k < countStar; k++)
{
std::cout << ”*”;
}
countStar += 2;
—countBlank;
std::cout << std::endl;
}
}
}
```

어려웠던 점
- NRWC의 로직은 이해했는데 마지막 results[]에 내용물 넣는 과정에서 1등만 확인하고 끝나는 상황이 만들어짐. while을 썼다가 isFinished 로직이 1회만 돌고 탈출하는 문제 발생. → checkResult를 for 조건식 안에 선언하는 방식으로 해결.
배운 점
- 메모리와 포인터 변수: FK/ID처럼 “직접 값을 갖는 게 아니라 어딘가를 가리킨다”는 개념은 비슷하지만, 포인터는 실제 물리적 메모리 주소를 가리킴.
- C++에서 함수의 선언과 정의: 컴파일 시 위에서부터 아래로 읽기 때문.
해야 할 일
- CheckPoint2(숫자 맞추기 게임) development
- 반복문 추가(정답을 맞추거나 0을 입력하면 종료)
- 함수화
- 입력 예외 처리하지 않음