1๋ฒ ์ฝ๋
#include<iostream>
#include<string>
int main()
{
using namespace std;
string first,last;
char grade;
int age;
cout<<"์ด๋ฆ : ";
getline(cin,first);
cout<<"์ฑ : ";
getline(cin,last);
cout<<"์ํ๋ ํ์ : ";
cin>>grade;
grade+=1;
cout<<"๋์ด : ";
cin>>age;
cout<<"์ด๋ฆ : "<<first<<endl<<"์ฑ : "<<last<<endl<<"ํ์ : "<<grade<<endl<<"๋์ด : "<<age;
}
ํ ๋ฒ์ ํ ํ์ ๋ฌธ์์ด ์ ๋ ฅ ์ฝ๊ธฐ
char๋ก ์์ฑํ ๋ฌธ์์ด์ ๊ฒฝ์ฐ
cin.getline(์ ๋ ฅํ ํ์ ์ ์ฅํ ๋ฐฐ์ด์ ์ด๋ฆ, ์ ๋ ฅ๋ฐ์ ๋ฌธ์๋ค์ ํ๊ณ);
cin.getline(name,20);
getline() ํจ์๋ ๊ฐํ ๋ฌธ์๋ฅผ ์ ๋ ฅ์ ๋์ผ๋ก ๊ฐ์ฃผํ์ฌ ํ ํ ์ ์ฒด๋ฅผ ์ฝ๋๋ค.
cin.get(name,20);
cin.get();
cin.get(dessert,20);
getline() ํจ์์ฒ๋ผ ๋์ํ๋ get() ํจ์, ๊ฐํ ๋ฌธ์๋ฅผ ์ฝ์ด์ ๋ฒ๋ฆฌ์ง ์๊ณ ์ ๋ ฅ ํ์ ๊ทธ๋๋ก ๋จ๊ฒจ๋ .
string๋ก ์์ฑํ ๋ฌธ์์ด์ ๊ฒฝ์ฐ
getline(cin,str);
์ ๋ ฅ์ ๋ํ๋ด๋ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก cin์ ์ฌ์ฉ, ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ์ ๋ ฅํ ํ์ ์ ์ฅํ ๋ฐฐ์ด์ ์ด๋ฆ)
string๊ฐ์ฒด๋ ๋ฌธ์์ด์ ๋ง๊ฒ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋ฏ๋ก ๋ฌธ์์ด์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ ๋งค๊ฐ๋ณ์ ์์.
2๋ฒ ์ฝ๋
#include<iostream>
#include<string>
int main()
{
using namespace std;
string name;
string dessert;
cout<<"์ด๋ฆ ์
๋ ฅ :\n";
getline(cin,name);
cout<<"์ข์ํ๋ ๋์ ํธ ์
๋ ฅ:\n";
getline(cin,dessert);
cout<<"๋ง์๋"<<dessert<<" ๋์ ํธ๋ฅผ ์ค๋นํ๊ฒ ์ต๋๋ค. "<<name<<" ๋!\n";
return 0;
}
3๋ฒ ์ฝ๋
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
char first[20];
char last[20];
char full[20];
cout<<"์ด๋ฆ : ";
cin.getline(first,20);
cout<<"์ฑ : ";
cin.getline(last,20);
strcpy(full,last);
strcat(full,", ");
strcat(full,first);
cout<<"ํ๋์ ๋ฌธ์์ด : "<<full<<endl;
return 0;
}
strcpy(charr1,charr2); //charr2๋ฅผ charr1์ ๋ณต์ฌ
strcat(charr1,charr2); //charr2์ ๋ด์ฉ์ charr1์ ์ถ๊ฐ
4๋ฒ ์ฝ๋
#include<iostream>
#include<string>
int main()
{
using namespace std;
string first,last,full;
cout<<"์ด๋ฆ : ";
getline(cin,first);
cout<<"์ฑ : ";
getline(cin,last);
full=last+", "+first;
cout<<"ํ๋์ ๋ฌธ์์ด : "<<full<<endl;
return 0;
}