#include <iostream>
#include <string>
// 소스가 실행 되면 main() 부터 실행됨.
int main() {
std::cout << "Please enter your first name : ";
std::string name;
// 문자열을 초기화 해서 name 에 정의.
std::cin >> name;
// >> 는 입력 연산자
// 출력연산을 받으면 버퍼를 채운다
// 입력연산을 받으면 버퍼가 가득차지 않아도 지운다
const std::string greeting = "Hello, " + name + "!";
const std::string spaces(greeting.size(), ' ');
// '' 는 문자리터럴 (char)
// "" 문자열 리터럴 (string)
// 문자열 변수는 변수명(size, 문자) 이런식으로
// size의 크키만큼 문자를 반복해서 만들어 문자열을
// 반환하여 문자열에 넣어준다
const std::string second = "* " + spaces + " *";
const std::string first(second.size(), ' *');
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;
return 0;
// main()함수에서 return 은 생략 가능
// return 정수를 반환해야함
}
std
는 namespace이고, cout, endl, cin, string
등은 std
namespace 안에 있는 기능 (?) 이다.
namespace는 중복이 안되며, 같은 이름으로 namespace를 정의하지 않도록 조심 또 조심해야한다.
string
변수를 선언해 줄 때, ()
로 묶어서 선언 가능 한데, 이럴 떄는 변수명(문자열의크기, 문자)
이렇게 선언 해줄 수 있다. 이 때 2번째 인자로 들어갈 문자는 string
이 아닌 char
즉, 1글자 문자이다.