두 숫자 서로 바꾸기 프로그램 실행 스냅샷.

'C++로 시작하는 언리얼 4 게임 프로그래밍'에서 Visual Studio Express 2013 for Windows Desktop을 다운받으라하여 가보니 Visual Studio Community를 추천한다.
나무위키 Visual Studio 항목을 보니 Community 버전에 와서 개인사용자에겐 무료로 풀렸단다. 하여 Visual Studio Community 2015 다운로드를 눌러 받아 실행해보니 올바른 Win32 응용 프로그램이 아니라며 실행되지 않는다. 이것도 64bit용인듯.
다시 보니 시스템 요구사항 중 지원 운영체제가 Windows 7 Service Pack 1 부터다. 이것 때문에 생긴 문제인듯. 하드 디스크는 4GB를 요구하고 있다.
어차피 책에서 요구하는 건 내가 이전에 깔아놓은 Visual Studio 2008에서도 가능한 듯 하여 그냥 패스하기로.

Visual Studio 2008로 'C++로 시작하는 언리얼 4 게임 프로그래밍'의 코드들 따라하기.

* cout << pl.name << endl; 에서 계속 에러가 나서 헤멨는데
#include <string>를 빼먹은 채 string 변수를 써서 생긴 문제였다.

* 이전에 C++ 공부할 때 몰라서 헤멨던 기호 ->는 구조체 포인터의 연산자였다. 객체 내부 변수를 가리킬 때 사용하는 기호.
ptrPl->name 는 (*ptrPl).name 이나 pl.name 와 같은 뜻이다.

유튜브에서 포인터 강좌 동영상(https://www.youtube.com/watch?v=gv6i2CJm57Q)을 보다.
'C++로 시작하는 언리얼 4 게임 프로그래밍' 2장 변수와 메모리까지 따라하기 완료.

* Visual C++에서 코드의 컴파일과 실행은 ctrl-F5.

16/9/6 화

testText01.cpp===========================================================
//cout을 이용한 간단 입출력 테스트
#include <iostream> //입출력 라이브러리 불러오기
using namespace std; //std::cout 대신 cout 사용

int main(){
       cout << "Hello World testing" << endl;
       cout << "cout 출력 테스팅" << endl;
       return 0; //운영체제로 돌아가기.
}

--------------------------
결과: 
--------------------------
Hello World testing
cout 출력 테스팅

testVar.cpp=============================================================
//변수 테스트
#include <iostream>
#include <string> using namespace std; int main(){ string name; int goldPieces; float hp; name = "Player"; goldPieces = 322; hp = 75.5f; cout << "캐릭터 " << name << "의 체력은 " << hp << ", 골드는 " << goldPieces << "입니다." << endl; } -------------------------- 결과: -------------------------- 캐릭터 Player의 체력은 75.5, 골드는 322입니다. testVector.cpp========================================================== //구조체로 객체 만들기. 포인터. #include <iostream>
#include <string>
using namespace std; struct Vector{ //구조체로 Vector 객체 정의 시작 float x, y, z; //x, y, z 위치 float으로 설정 }; //Vector 객체 정의 끝 struct Player{ string name; int hp; Vector position; }; //마지막 ; 잊지말기. int main(){ Player pl; pl.name = "Player"; pl.hp = 100; pl.position.x = 20, pl.position.y = 30, pl.position.z = 40; Player* ptrPl = 0;//플레이어 포인터, 널포인터 만들기(실수 예방용) ptrPl = &pl; //포인터 연결하기 cout << "플레이어 명: " << pl.name << endl; cout << "플레이어 체력: " << pl.hp << endl; cout << "플레이어 위치: " << pl.position.x << ", " << pl.position.y << ", " << pl.position.z << endl; cout << "플레이어 주소: " << &pl << endl; cout << "플레이어 명 주소: " << &pl.name << endl; cout << "플레이어 체력 주소: " << &pl.hp << endl; cout << "플레이어 위치 주소: " << &pl.position << endl; cout << "플레이어 y 위치 주소: " << &pl.position.y << endl; cout << "플레이어 z 위치 주소: " << &pl.position.z << endl; cout << "플레이어 명: " << ptrPl->name << endl; cout << "플레이어 명: " << (*ptrPl).name << endl; } -------------------------- 결과: -------------------------- 플레이어 명: Player 플레이어 체력: 100 플레이어 위치: 20, 30, 40 플레이어 주소: 0012FF24 플레이어 명 주소: 0012FF24 플레이어 체력 주소: 0012FF44 플레이어 위치 주소: 0012FF48 플레이어 y 위치 주소: 0012FF4C 플레이어 z 위치 주소: 0012FF50 플레이어 명: Player 플레이어 명: Player testPointer.cpp====================================================== //두 숫자 바꾸기. 포인터. cin, printf. #include <iostream>
#include <string>
using namespace std; void swap2s(int* a, int* b); int main(){ int x, y; string t = "바꾸기"; cout << "바꿀 2 숫자 중 첫번째를 입력: "; cin >> x; cout << "바꿀 2 숫자 중 2번째를 입력: "; cin >> y; cout << "x: " << x << endl; cout << "y: " << y << endl; printf("%s 전 x: %d, y: %d \n", t.c_str(), x, y); swap2s(&x, &y); cout << "x: " << x << endl; cout << "y: " << y << endl; printf("%s 뒤 x: %d, y: %d \n", t.c_str(), x, y); } void swap2s(int* a, int* b){ int temp = *a; *a = *b; *b = temp; } -------------------------- 결과: -------------------------- 바꿀 2 숫자 중 첫번째를 입력: 3 바꿀 2 숫자 중 2번째를 입력: 70 x: 3 y: 70 바꾸기 전 x: 3, y: 70 x: 70 y: 3 바꾸기 뒤 x: 70, y: 3