본문 바로가기

언어/C++

전화번호 부

#include <iostream>
using namespace std;

class Person{
	string name;
	string tel;
	public:
		string getName(){return name;
		}
		string getTel(){
			return tel;
		}
		void set(string name, string tel){
			this->name=name;
			this->tel=tel;
		}
};

int main(int argc, char** argv) {
	
	int n;
	Person p[3];
	string name, number;
	
	cout << "이름과 전화 번호를 입력해 주세요" << endl;
	
	for(int i=0; i<3; i++){
		cout<<"사람" << i+1 << ">> ";
		cin>> name>> number;
		p[i].set(name,number); 
	}
	
	cout << "모든 사람의 이름은 ";
	for(int i=0; i<3; i++){
		cout<<p[i].getName() << " ";
	}
	
	cout << endl << "전화번호를 검색합니다. 이름을 입력하세요>>";
	cin>>name;
	for(int i=0; i<3; i++){
		if(p[i].getName()==name) n=i;
	}
	
	cout << "전화 번호는 " << p[n].getTel() << endl;
	
	
	return 0;
}

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

5단원 실습문제 (참조, 복사생성자 )  (1) 2019.11.12
복사생성자  (0) 2019.11.12
함수와 참조  (0) 2019.11.12
히스토그램  (0) 2019.11.11
원의 개수 와 면적 구하기  (0) 2019.11.11