ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 02 C++ 기본입출력(2)
    Studies/C++ 2022. 1. 3. 16:16

    1. stringstream으로 string parsing / 형변환해서 parsing된 내용 출력하기

     

    (1) 코드

    #include <iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    int main() {
    	cout << "200000000 박지연" << endl;
    	string str1;
    	getline(cin, str1); //cin은 키보드로부터 한 줄 입력 받겠다
    	                          //str1에 저장
    	cout << str1 << endl; //str1 출력
    
    	stringstream ss;
    	ss.str(str1);
    	int score;
    	while (ss >> score)
    		cout << score << endl;
    }

    (2) 실행결과

     

     

    2. stringstream으로 학생 점수 총합 계산하기

     

    (1) 코드

    #include <iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    int main() {
    	cout << "200000000 박지연" << endl;
    	string str1;
    	getline(cin, str1); //cin은 키보드로부터 한 줄 입력 받겠다
    	                          //str1에 저장
    	cout << str1 << endl; //str1 출력
    
    	stringstream ss;
    	ss.str(str1);
    	string name;
    	ss >> name;
    
    	int score,sum=0;
    	while (ss >> score)
    		sum += score;
    		
    	
    	cout <<name<<" : "<< sum << endl;
    }

    (2) 실행결과

    'Studies > C++' 카테고리의 다른 글

    05 제어문과 함수/배열  (0) 2022.01.18
    STL 자료구조  (0) 2022.01.09
    04 c++ 배열  (0) 2022.01.08
    03 c++ 조건문 활용(카페 프로그램)  (0) 2022.01.08
    01 C++ 기본입출력(1)  (0) 2022.01.03

    댓글

Designed by Tistory.