//SimpleC++.cpp
//cout, << 접하기
#inclde<istream>
int main(){ //main부터 실행 시작
std::cout<<"Hello\n";
std::cout<<"맛보기";
return 0;
}
실행결과
Hello
맛보기
여러줄 : /* */
한줄 : //
- 실행 시작점
- main 종료시 프로그램 종료
- return문 생략 가능
- void main() : 표준 아님
#include <iostream>
표준입출력을 위한 클래스, 객체, 변수 선언
cout, cin, <<, >> 등 연산자 선언
스크린 출력장치에 연결된 표준 출력 객체
에 선언
std 이름 공간에 선언: std::cout
- 스트림 삽입 연산자
- 여러개로 여러값 출력 가능
std::cout<<"hello\n"<<"첫번째";
#include <iostream>
void main() {
int n = 3;
char c = '#';
std::cout << c << 5.5 << '-' << n << "hello" << true;
} //ture는 1로 출력
결과
#5.5-3hello1
std::cout << "n+5="<< n+5;
std::cout << f(); //f() 리턴값 출력
std::cout << "Hello"<< '\n'; //'\n'
std::cout << "Hello:<< std::endl; //std::endl;
#include <iostream>
double area(int r);
double area(int r) {
return 3.14 * r * r;
}
int main() {
int n = 3;
char c = '#';
std::cout << c << 5.5 << '-' << n << "hello" << true << std::endl;
std::cout << "n+5=" << n + 5 << '\n';
std::cout << "면적은" << area(n);
}
실행결과
#5.5-3hello1
n+5=8
면적은28.26
printf(), scanf()등은 사용 XX
형태: 이름공간 :: 이름
using std:: cout; //cout만 생략
cout << "hi" << std::endl;
using namespace std; //모든 이름에 대해 생략
cout << "hi" << endl;
//따라서 이렇게 씀
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main() {
cout << "너비를 입력하세요>>";
int width;;
cin >> width;
cout << "높이를 입력하세요>>";
int height;
cin >> height;
int area = width * height;
cout << "면적은" << area << "\n";
}
cout << "너비와 높이를 입력하세요>>";
cin >> width >> height;
cout << width << '\n' << height << "\n";
//입력: 23 35
cin: 입력버퍼 내장하고있음, enter 입력될때까지 입력된 키 입력 버퍼에 저장
(>>): enter 입력되면 cin의 입력버퍼에서 값 읽어 변수에 전달
변수 선언은 사용전 아무곳이나 가능
char name1[6] = {'h','i','\0'}; //문자열
char name2[2] = {'h','i'}; //단순 문자 배열
char name3[10] = "hi"; //빈공간은 '\0'으로 초기화
#include <string.h> or
#include <csting> 포함 -->바람직 (strcmp, strlen, strcpy)
#include <iostream>
using namespace std;
int main() {
cout << "이름을 입력하세요>>";
char name[11]; //한글은 5개, 영문은 10개까지
cin >> name;
cout << "이름은" << name << "입니다\n";
}
//빈칸 만나면 문자열 입력 종료 (마 이 클 -> 마)
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char password[11];
cout << "프로그램을 종료하려면 암호를 입력하세요." << endl;
while (true) {
cout << "암호>>";
cin >> password;
if (strcmp(password, "c++") == 0) {
cout << "프로그램을 정상 종료합니다." << endl;
break;
}
else
cout << "암호가 틀립니다~~" << endl;
}
}
//endl or \n
공백이 낀 문자열 입력 받음
cin.getline(char buf[], int size, char delimitChar)
//buf에 최대 size-1개
deliminChaar만나면 입력 중단, 디폴트 값은 '\n', 끝에 '\0' 붙임
enter만나면 입력 중단
#include <iostream>
using namespace std;
#include <cstring>
int main() {
cout << "주소를 입력하세요>>";
char address[100];
cin.getline(address, 100, '\n');
cout << "주소는" << address << "입니다\n";
}
//빈칸이 있어도 enter 입력될때까지 입력된다
#include <string>
#include <iostream>
#include <string>
using namespace std;
int main() {
string song("Falling in love");
string elvis("Elvis Presley");
string singer;
cout << song + "을 부른 가수는";
cout << "힌트: 첫 글자는 " << elvis[0] << ")?";
getline(cin, singer); //빈칸 포함 가능 **
if (singer == elvis)
cout << "맞았습니다";
else
cout << "틀렸습니다" + elvis + "입니다\n"; //+로 문자열 연결
}
#include <iostream>
using namespace std;
#include <헤더파일> //컴파일러가 설치된 폴더에서 헤더파일 찾아라
#include "헤더파일" //개발자의 프로젝트 폴더에서 찾아라
//- <cstring>파일에 strcpy() 함수의 원형이 선언되어 있다(코드 들어있지 않음)
//- cout & cin은 <iostream>헤더파일에 선언