수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

파일 입출력과 리턴값 관련질문입니다.

방방

2023.04.01

#include iostream
#include string
#include fstream
using namespace std;
#define MAX_SIZE 100
#define QUIT 6

class Person{
protected:
string name; //공통적인 부분 이름
string phone; //공통적인 부분 전화번호
string address; //공통적인 부분 집주소
public:
/* get */
virtual void print(){
cout 가상 함수endl;
}
virtual void inputData(){
cout 가상 함수endl;
}
virtual const char* get_class(){
return Person;
}
virtual string getNumber(){
return zz;
}
virtual string getMajor(){
return zz;
}
virtual string getClub(){
return zz;
}
string getName(){
return name;
}
string getPhone(){
return phone;
}
string getAddress(){
return address;
}
virtual string getWorkarea(){//연구분야 get
return 연구담당 담당부서;
}
virtual string getOfficeinform(){//연구실 정보 get
return 연구실 사무실 정보;
}
virtualstring getGrade(){
return 학년;
}
virtual string getAdviser(){
return 지도교수;
}
/* set */
virtual void setNumber(string a){
cout가상함수;
}
virtual void setMajor(string a){
string c=a;
}
virtual void setClub(string a){
string c=a;
}
void setName(string name){
this-name=name;
}
void setPhone(string phone){
this-phone=phone;
}
void setAddress(string address){
this-address=address;
}
virtual void setWorkarea(string a){//연구분야 get
string c=a;
}
virtual void setOfficeinform(string a){//연구실 정보 get
string c=a;
}
virtualvoid setGrade(string a){
string c=a;
}
virtual void setAdviser(string a){
string c=a;
}
};
typedef Person* pPerson; // 매개변수로 넘겨줄때 유용하게 사용함.
class Faculty : public Person{
private:
string facultyNumber; // 사번
string facultyMajor; // 소속학과 (교수학과)
string studyArea; // 연구분야
string facultyOffice; //연구실 정보
public:
void print()
{
cout facultyNumber;
cout facultyMajor;
cout studyArea;
cout facultyOffice;
cout name;
cout phone;
cout address;
}
void inputData(){
cout이름 : ; cinname;
cout전번 : ; cinphone;
cout주소 : ; cinaddress;
cout사번 : ; cinfacultyNumber;
cout소속학과 : ; cinfacultyMajor;
cout연구분야 : ; cinstudyArea;
cout연구실 정보 : ; cinfacultyOffice;
}
const char * get_class() { return Faculty; }
string getNumber(){//사번 get
return facultyNumber;
}
string getMajor(){//학과 get
return facultyMajor;
}
string getWorkarea(){//연구분야 get
return studyArea;
}
string getOfficeinform(){//연구실 정보 get
return facultyOffice;
}
void setNumber(string fatring facultyNumber){//사번 set
this-facultyNumber = facultyNumber;
}
void setMajor(string facultyMajor){//학과 set
this-facultyMajor = facultyMajor;
}
void setWorkarea(string studyArea){//연구분야 set
this-studyArea = studyArea;
}
void setOfficeinform(string facultyOffice){//연구실 정보 set
this-facultyOffice = facultyOffice;
}
};
class Staff : public Person{
private:
string staffNumber; // 사번
string staffMajor; // 소속부서
string staffWork; // 담당업무
string staffOffice; //사무실 정보
public:
void print()
{
cout staffNumber;
cout staffMajor;
cout staffWork;
cout staffOffice;
cout name;
cout phone;
cout address;
}
void inputData(){
cout이름 : ; cinname;
cout전번 : ; cinphone;
cout주소 : ; cinaddress;
cout사번 : ; cinstaffNumber;
cout소속부서 : ; cinstaffMajor;
cout담당업무 : ; cinstaffWork;
cout사무실 정보 : ; cinstaffOffice;
}
const char * get_class() { return Staff; }
string getNumber(){
return staffNumber;
}
string getMajor(){
return staffMajor;
}
string getWorkarea(){
return staffWork;
}
string getOfficeinform(){
return staffOffice;
}
void setNumber(string staffNumber){
this-staffNumber = staffNumber;
}
void setMajor(string staffMajor){
this-staffMajor = staffMajor;
}
void setWorkarea(string staffWork){
this-staffWork=staffWork;
}
void setOfficeinform(string staffOffice){
this-staffOffice = staffOffice;
}
};
class Student : public Person{
protected :
string studentNumber; // 학번
string grade; //학년
string studentMajor; // 소속학과
string adviser; // 지도교수
public :
string getNumber(){
return studentNumber;
}
string getMajor(){
return studentMajor;
}
string getGrade(){
return grade;
}
string getAdviser(){
return adviser;
}
void setNumber(string studentNumber){
this-studentNumber = studentNumber;
}
void setMajor(string studentMajor){
this-studentMajor = studentMajor;
}
void setGrade(string grade){
this-grade = grade;
}
void setAdviser(string adviser){
this-adviser = adviser;
}
};
class UnderGraduate : public Student{
private:
string club; //동아리
public:
void print()
{
cout studentNumber;
cout grade;
cout studentMajor;
cout adviser;
cout club;
cout name;
cout phone;
cout address;
}
void inputData(){
cout이름 : ; cinname;
cout전번 : ; cinphone;
cout주소 : ; cinaddress;
cout학번 : ; cinstudentNumber;
cout학년 : ; cingrade;
cout소속학과 : ; cinstudentMajor;
cout지도 교수 : ; cinadviser;
cout동아리 : ; cinclub;
}
const char * get_class() { return UnderGraduate; }
string getClub(){
return club;
}
void setClub(string club){
this-club = club;
}
};
class Graduate : public Student{
private:
string studyArea2; // 연구분야
string facultyOffice2; //연구실 정보
public:
void print()
{
cout studentNumber;
cout grade;
cout studentMajor;
cout adviser;
cout studyArea2;
cout facultyOffice2;
cout name;
cout phone;
cout address;
}
void inputData(){
cout이름 : ; cinname;
cout전번 : ; cinphone;
cout주소 : ; cinaddress;
cout학번 : ; cinstudentNumber;
cout학년 : ; cingrade;
cout소속학과 : ; cinstudentMajor;
cout지도 교수 : ; cinadviser;
cout연구 분야 : ; cinstudyArea2;
cout연구실 정보 : ; cinfacultyOffice2;
}
const char * get_class() { return Graduate; }
string getWorkarea(){//연구분야 get
return studyArea2;
}
string getOfficeinform(){//연구실 정보 get
return facultyOffice2;
}
void setWorkarea(string studyArea2){//연구분야 set
this-studyArea2 = studyArea2;
}
void setOfficeinform(string facultyOffice2){//연구실 정보 set
this-facultyOffice2 = facultyOffice2;
}
};
class Iohandler{
public:
void menu(){
cout 1. 인력 입력endl;
cout 2. 인력 출력endl;
cout 3. 인력 삭제endl;
cout 4. 인력 검색endl;
cout 5. 저 장endl;
cout 6. 종 료endl;
cout 메뉴 입력 : ;
}//menu
void subMenu(int select){
switch(select){
case 1:
cout 1.1 교수 입력endl;
cout 1.2 직원 입력endl;
cout 1.3 대학생 입력endl;
cout 1.4 대학원생 입력endl;
cout 선택 : ;
break;
case 2:
cout 2.1 모두 출력endl;
cout 2.2 교수 출력endl;
cout 2.3 직원 출력endl;
cout 2.4 대학생 출력endl;
cout 2.5 대학원생 출력endl;
cout 선택 : ;
break;
case 3:
cout 3.1 교수 삭제endl;
cout 3.2 직원 삭제endl;
cout 3.3 대학생 삭제endl;
cout 3.4 대학원생 삭제endl;
cout 선택 : ;
break;
case 4:
cout 4.1 이름 검색endl;
cout 4.2 학과 검색endl;
cout 4.3 동아리 검색講?endl;
cout 선택 : ;
break;
}//switch
}//submenu
};
class Control{
private :
string searchNumber;
string choiseNumber;
public :
void inputData(pPerson* ps,int subSelectNum, int index){
if(subSelectNum == 1){
ps[index] = new Faculty(); //교수입력
ps[index]-inputData();
}
else if(subSelectNum == 2){
ps[index] = new Staff(); //직원입력
ps[index]-inputData();
}//else if
else if(subSelectNum == 3){
ps[index] = new UnderGraduate(); //대학생입력
ps[index]-inputData();
}//else if
else if(subSelectNum == 4){
ps[index] = new Graduate(); //대학원생입력
ps[index]-inputData();
}//else if
}
void showData(pPerson* ps, int subSelectNum, int index){
if(subSelectNum == 1){
cout [교수] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Faculty ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
cout [직원] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Staff ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
cout [대학생] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), UnderGraduate ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
cout [대학원생] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Graduate ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
}//if 모두 출력
else if(subSelectNum == 2){
cout [교수] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Faculty ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
}//else if 교수출력
else if(subSelectNum == 3){
cout [직원] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Staff ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
}//else if 직원출력
else if(subSelectNum == 4){
cout [대학생] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), UnderGraduate ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
}//대학생 출력
else if(subSelectNum == 5){
cout [대학원생] endl;
for(int i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Graduate ) == 0 ){
ps[i]-print();
coutendl;
}
}//for i
}//대학원생 출력
}
int deleteData(pPerson* ps, int subSelectNum, int index){
if(subSelectNum == 1){
int i;
cout삭제할 사번을 입력하세요 : ; cinsearchNumber;
for(i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Faculty ) == 0 ){
if(searchNumber == ps[i]-getNumber()){
ps[i]-print();
identifyInform(ps,i,index);
}//if
}
}//for i
}//교수 삭제
else if(subSelectNum == 2){
int i;
cout삭제할 사번을 입력하세요 : ; cinsearchNumber;
for(i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Staff ) == 0 ){
if(searchNumber == ps[i]-getNumber()){
ps[i]-print();
identifyInform(ps,i,index);
}//if
}
}//for i
}//직원 삭제
else if(subSelectNum == 3){
int i;
cout삭제할 학번을 입력하세요 : ; cinsearchNumber;
for(i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), UnderGraduate ) == 0 ){
if(searchNumber == ps[i]-getNumber()){
ps[i]-print();
identifyInform(ps,i,index);
}//if
}
}//for i
}//대학생 삭제
else if(subSelectNum == 4){
int i;
cout삭제할 학번을 입력하세요 : ; cinsearchNumber;
for(i=0; iindex; i++){
if ( strcmp( ps[i]-get_class(), Graduate ) == 0 ){
if(searchNumber == ps[i]-getNumber()){
ps[i]-print();
identifyInform(ps,i,index);
}//if
}
}//for i
}//대학원생 삭제
index--;
return index;
}
int identifyInform(pPerson* ps, int i , int index){
coutendl삭제하는 정보가 확실합니까 ? (y or n); cinchoiseNumber;
if(choiseNumber == y || choiseNumber ==Y){
for(int k=i; kindex-1; k++){
ps[k]=ps[k+1];
}
return index;
}
else if(choiseNumber == n || choiseNumber ==N)
return 1;
else
return identifyInform(ps, i, index);
}
void searchData(pPerson* ps, int subSelectNum, int index){
int i;
if(subSelectNum == 1){
cout검색할 이름을 입력하세요 : ; cinsearchNumber;
for(i=0; iindex; i++){
if(searchNumber == ps[i]-getName()){
ps[i]-print();
}
coutendl;
}
}//이름 검색
else if(subSelectNum == 2){
int i;
cout검색할 학과을 입력하세요 : ; cinsearchNumber;
&nbnbsp;for(i=0; iindex; i++){
if(searchNumber == ps[i]-getMajor()){
ps[i]-print();
}
coutendl;
}
}//학과 검색
else if(subSelectNum == 3){
int i;
cout검색할 동아리을 입력하세요 : ; cinsearchNumber;
for(i=0; iindex; i++){
if(searchNumber == ps[i]-getClub()){
ps[i]-print();
}
coutendl;
}
}//동아리 검색
}
void outpuFile(pPerson* ps, int index){
ofstream fout(person.txt);
for(int i=0; iindex; i++){
if( i != index-1 ){
if ( strcmp( ps[i]-get_class(), Faculty ) == 0 ){
fout교수 ;
foutps[i]-getNumber() ;
foutps[i]-getMajor() ;
foutps[i]-getWorkarea() ;
foutps[i]-getOfficeinform() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
else if( strcmp( ps[i]-get_class(), Staff ) == 0 ){
fout직원 ;
foutps[i]-getNumber() ;
foutps[i]-getMajor() ;
foutps[i]-getWorkarea() ;
foutps[i]-getOfficeinform() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
else if( strcmp( ps[i]-get_class(), UnderGraduate ) == 0 ){
fout대학생 ;
foutps[i]-getNumber() ;
foutps[i]-getGrade() ;
foutps[i]-getMajor() ;
foutps[i]-getAdviser() ;
foutps[i]-getClub() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
else if( strcmp( ps[i]-get_class(), Graduate ) == 0 ){
fout대학원생 ;
foutps[i]-getNumber() ;
foutps[i]-getGrade() ;
foutps[i]-getMajor() ;
foutps[i]-getAdviser() ;
foutps[i]-getWorkarea() ;
foutps[i]-getOfficeinform() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
foutendl;
}
else{
if ( strcmp( ps[i]-get_class(), Faculty ) == 0 ){
fout교수 ;
foutps[i]-getNumber() ;
foutps[i]-getMajor() ;
foutps[i]-getWorkarea() ;
foutps[i]-getOfficeinform() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
else if( strcmp( ps[i]-get_class(), Staff ) == 0 ){
fout직원 ;
foutps[i]-getNumber() ;
foutps[i]-getMajor() ;
foutps[i]-getWorkarea() ;
foutps[i]-getOfficeinform() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
else if( strcmp( ps[i]-get_class(), UnderGraduate ) == 0 ){
fout대학생 ;
foutps[i]-getNumber() ;
foutps[i]-getGrade() ;
foutps[i]-getMajor() ;
foutps[i]-getAdviser() ;
foutps[i]-getClub() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
else if( strcmp( ps[i]-get_class(), Graduate ) == 0 ){
fout대학원생 ;
foutps[i]-getNumber() ;
foutps[i]-getGrade() ;
foutps[i]-getMajor() ;
foutps[i]-getAdviser() ;
foutps[i]-getWorkarea() ;
foutps[i]-getOfficeinform() ;
foutps[i]-getName() ;
foutps[i]-getPhone() ;
foutps[i]-getAddress();
}
}
}//for
}
void inputFile(pPerson* ps, int index){
string identify;
string name,phone,address; //person
string facultyNumber , facultyMajor , studyArea , facultyOffice; //Faculty
string staffNumber , staffMajor , staffWork , staffOffice;//Staff
string studentNumber , grade , studentMajor , adviser , studyArea2 , facultyOffice2, club; //Student
ifstream fin(person.txt);
while(! fin.eof()){
fin identify;
if(identify == 교수){
finfacultyNumberfacultyMajorstudyAreafacultyOfficenamephoneaddress;
ps[index]-setNumber(facultyNumber);
ps[index]-setMajor(facultyMajor);
ps[index]-setWorkarea(studyArea);
ps[index]-setOfficeinform(facultyOffice);
ps[index]-setName(name);
ps[index]-setPhone(phone);
&;
ps[index]-setAddress(address);
}
else if(identify == 직원){
finstaffNumberstaffMajorstaffWorkstaffOfficenamephoneaddress;
ps[index]-setNumber(staffNumber);
ps[index]-setMajor(staffMajor);
ps[index]-setWorkarea(staffWork);
ps[index]-setOfficeinform(staffOffice);
ps[index]-setName(name);
ps[index]-setPhone(phone);
ps[index]-setAddress(address);
}
else if(identify == 대학생){
finstudentNumbergradestudentMajoradviserclubnamephoneaddress;
ps[index]-setNumber(studentNumber);
ps[index]-setGrade(grade);
ps[index]-setMajor(studentMajor);
ps[index]-setAdviser(adviser);
ps[index]-setClub(club);
ps[index]-setName(name);
ps[index]-setPhone(phone);
ps[index]-setAddress(address);
}
else if(identify == 대학원생){
finstudentNumbergradestudentMajoradviserstudyArea2facultyOffice2namephoneaddress;
ps[index]-setNumber(studentNumber);
ps[index]-setGrade(grade);
ps[index]-setMajor(studentMajor);
ps[index]-setAdviser(adviser);
ps[index]-setWorkarea(studyArea2);
ps[index]-setOfficeinform(facultyOffice2);
ps[index]-setName(name);
ps[index]-setPhone(phone);
ps[index]-setAddress(address);
}
index++;
}
}
};
int main()
{
pPerson ps[MAX_SIZE];
Iohandler io;
Control ct;
int selectNum, subSelectNum;
static int index=0;
//ct.inputFile(ps,index);
while(1){
io.menu(); // 메뉴
cin selectNum; // 메뉴선택
if(selectNum == QUIT)
return 0;
coutendl;
if(selectNum != 5){
io.subMenu(selectNum); //서브메뉴 불러오기
cin subSelectNum;
}

switch(selectNum)
{
case 1:
ct.inputData(ps,subSelectNum,index);
index++;
break;
case 2:
ct.showData(ps,subSelectNum,index);
break;
case 3:
ct.deleteData(ps,subSelectNum,index);
break;
case 4:
ct.searchData(ps,subSelectNum,index);
break;
case 5:
cout Save it! endlendl;
ct.outpuFile(ps,index);
break;
}//switch
}//while
}//main

자료첨부합니다.. 파일 출력은 되는데 파일 입력이 되질않습니다 set함수에서 문제가 생기는것 같은데 원인을 모르겠습니다.

그리고 삭제하고 index값을 감소 시켜서 리턴해주고싶은데. 감소시켜도 다시 메인문에서는 원래의 index값으로 됩니다.ㅠ

index값을 전역으로 썼는데도 같습니다.ㅠ
만약세 N 또는 n을 입력받았을때는 그냥 수행되지 않게 하고싶은데..그렇게 하기위해서는 값이 리턴되야하는데 .ㅠㅠ

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

번호 제 목 글쓴이 날짜
2695091 txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) 헛장사 2025-05-17
2695063 수도요금 프로그램좀 짜주세요. 시내 2025-05-17
2695033 답변좀요ㅠㅠ (1) 비사벌 2025-05-16
2695010 C++의 STL은 왜 굳이 템플릿화 시켜서 라이브러리를 만드나요? (초보수준의 질문..) (2) 엘보어 2025-05-16
2694958 로직이 변한다는 것에 대해서 궁금합니다. 튼동 2025-05-16
2694929 열혈강의 25-2 두번째 문제 질문 지우개 2025-05-15
2694900 dequeue 에서 리턴값 프린트 방법알려주세요 오늘 12시까지 대화방에 있습니다 도와주세요 미투리 2025-05-15
2694854 절대값을 구할때 (2) 그녀는귀여웠다 2025-05-15
2694827 이제 어떻게 공부해야할지 모르겠네요 새얀 2025-05-14
2694778 순열 계산요. 맛조이 2025-05-14
2694754 ShowWindow 함수를 이용하려 하는데 질문있습니다. (2) 파도 2025-05-14
2694731 리눅스 커널의 시작점 질문 미르 2025-05-13
2694702 이거 뭐가문제인가요 코드수정좀 (3) 맑은 2025-05-13
2694675 C언어 후위표기를 중위표기로 앨런 2025-05-13
2694646 안녕하세요 파일 합치기 함수! (1) 연블루 2025-05-13
2694618 잘몰라서 설명부탁드립니다. scanf 관련 (3) 파라 2025-05-12
2694590 이 코드가 뭐하는 코드일까요? #2 빵순 2025-05-12
2694559 동적할당으로 배열(2차원열)을 만드는데 있어 그걸 함수화시키는데... (1) 늘솔길 2025-05-12
2694532 네트워크에 관하여... (4) 황소자리 2025-05-12
2694503 프로그램 연산 후 바로 종료되는 현상 (6) Judicious 2025-05-11
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com