Studies/C++

02 C++ 기본입출력(2)

쿠뱃봉 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) 실행결과