C++.1 iostream 표준함수

lsw·2021년 3월 22일
0

C++

목록 보기
1/8
post-thumbnail

1. 문제

int type 숫자 다섯개를 입력받은 후 그들의 합을 출력하시오.


2. 알고리즘

  1. set int total=0
  2. set variable "num"
  3. fill values into num
  4. repeat 3 by using for loop

3.1. 코드

  • std::cout
#include <iostream>

int main()
{
  int num, total=0;
  for(int i=1 ; i<=5 ; i++)
  {
    std::cout << i << "번째 정수 입력 : " ;
    std::cin >> num; // 변수 초기회
    total+=i; // 숫자 합
  }
  std::cout << "total : " << total << std::endl; // 숫자 총 합 출력
  return 0;
}

3.2. 결과


4.1. 코드

  • std::cin
#include <iostream>

int main()
{
// string을 받을 수 있는 char형 배열(크기 100)
  char phone[100];
  char name[100];
  char id[100];

  std::cout << "전화번호, 이름, id를 입력하세요 : ";
// 입력
  std::cin >> phone >> name >> id;
// 출력
  std::cout << "name : " << name << std::endl << 
   "phone : " << phone << std::endl << "id : " << id << std::endl;
   return 0;
}

4.2. 결과

  • 알게된점
    1. C++을 받아들이는 모든 컴파일러는 변수 선언의 위치 제약을 받지 않는다. which is different from C
    2. std::cout <<(출력 : ← 방향, '<<' 연산자) + 피연산자 : 인자의 서식에따라 서식자를 정해야했던 C의 함수들과 달리 C++의 i/o 함수의 피연산자는 별도 서식지정이 필요없다.
    3. std::cin >>(입력 to 변수 → 방향, '>>'연산자) + 피연산자, 마찬가지 서식자 지정이 필요치 않음.

5. 결론

C++는 iostream 헤더파일의 표준함수를 통해 입, 출력을 별도 서식지정 없이 사용할 수 있음, 변수를 지정하는데에 위치제약이 없음을 알게되었다.

profile
미생 개발자

0개의 댓글