1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>
 
using namespace std;
 
 
int main() {
    
    cout << "문장 입력:";
    string str;
 
    // cin >> str; 공백을 끝어읽기 때문에 문장 입력 불가
 
    getline(cin, str); //getline()함수를 통해 입력 받음
 
    cout << str;
 
    return 0;
}
 
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>
#include <sstream>
 
using namespace std;
 
 
int main() {
    
    string mystr;
    cout << "enter your scores:"// 80 40 14
    getline(cin, mystr);
 
    stringstream ss;
    ss.str(mystr);
 
    string data;         
    while (ss >> data) {         //공백을 기준으로 string 추출
        cout << data << endl;
    }
 
    return 0;
}
 
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string>
#include <iostream>
#include <sstream>
 
using namespace std;
 
 
int main() {
    
    string mystr;
    cout << "enter your scores:"// Sudal 40 14
    getline(cin, mystr);
 
    stringstream ss;
    ss.str(mystr);
 
    string name;
    int score, sum = 0;
 
    ss >> name;
 
    while (ss >> score) {         //공백을 기준으로 string 추출
        sum += score;
    }
 
    cout << name <<" "<< sum << endl;
 
    return 0;
}
 
cs

 

 

 

반응형

+ Recent posts