게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[본삭금] 구조체 내 문자열에 자꾸 쓰레기값이 들어갑니다 ㅠㅠ
게시물ID : programmer_20255짧은주소 복사하기
작성자 : 브레멘음악대
추천 : 1
조회수 : 1202회
댓글수 : 8개
등록시간 : 2017/04/10 20:35:49
옵션
  • 베스트금지
  • 본인삭제금지
struct MONSTER
{
void(*Skillslot[4])(struct MONSTER *MyPM, struct MONSTER *AnPM);
char NAME[10];
int HPMAX;
int HPNOW;
int SPMAX;
int SPNOW;
int ATT;
int DP;
int TYPE;
};

이런 구조체를 만들었구요
처음에 있는 함수포인터는 사용자에게 입력받아 8개중 4개를 선택하게 하기위해 만들었구요.

void NmAttack(struct MONSTER *MyPM, struct MONSTER *AnPM);
void Smite(struct MONSTER *MyPM, struct MONSTER *AnPM);
void DoubleAttack(struct MONSTER *MyPM, struct MONSTER *AnPM);
void Pounding(struct MONSTER *MyPM, struct MONSTER *AnPM);
void Uppercut(struct MONSTER *MyPM, struct MONSTER *AnPM);
void Beam(struct MONSTER *MyPM, struct MONSTER *AnPM);
void SolarBeam(struct MONSTER *MyPM, struct MONSTER *AnPM);
void SharpAttack(struct MONSTER *MyPM, struct MONSTER *AnPM);

이게 스킬들입니다.

TYPE은
printf("원하는 속성을 선택하시오.\n1. 노멀 2. 땅 3. 물 4. 불 5. 바람\n -> ");
scanf("%d", &type);
요렇게 해서 0~4까지 숫자를 받기로 했구요

for (int i = 0; i < 4; i++)
{
printf("원하는 스킬을 선택하시오.(%d/4)\n1. 공격 2. 강타 3. 더블어택 4. 파운딩\n5. 어퍼컷 6. 빔 7. 솔라빔 8. 샤프 어택\n -> ", i + 1);
scanf("%d", &select[i]);

if (select[i] > 8 || select[i] < 1) i--;
else select[i]--;
}

for (int i = 0; i < 4; i++)
{
switch (select[i])
{
case 0:
SKILLSLOT[i] = NmAttack;
break;
case 1:
SKILLSLOT[i] = Smite;
break;
case 2:
SKILLSLOT[i] = DoubleAttack;
break;
case 3:
SKILLSLOT[i] = Pounding;
break;
case 4:
SKILLSLOT[i] = Uppercut;
break;
case 5:
SKILLSLOT[i] = Beam;
break;
case 6:
SKILLSLOT[i] = SolarBeam;
break;
case 7:
SKILLSLOT[i] = SharpAttack;
break;
}
}

0번부터 3번슬롯까지 원하는 스킬을 입력받도록 만들었습니다.

struct MONSTER MyPM =
{
SKILLSLOT[0],SKILLSLOT[1],SKILLSLOT[2],SKILLSLOT[3],
"MyPoketmon",
100,100,//HPMAX / HPNOW
100,100,//SPMAX / HPNOW
10,
5,
type
};
그리고 고대하던 구조체 선언 및 초기화...

void printMonster(struct MONSTER *monster)
{
printf("%s\n%d/%d\n%d/%d\n%d\n%d\n%d\n",monster->NAME,monster->HPNOW,monster->HPMAX,monster->SPNOW ,monster->SPMAX ,monster->ATT,monster->DP,monster->TYPE);
monster->Skillslot[0](monster, monster);
monster->Skillslot[1](monster, monster);
monster->Skillslot[2](monster, monster);
monster->Skillslot[3](monster, monster);
}

그리고 시험삼아 이렇게 한번 찍어봤습니다.

그런데...

도움!.PNG

이름이!
이름이!!!
이름 끝에 쓰레기값이...!!!

얘 왜 이러나요...
엉엉...

이제 몬스터 한마리 더 만들어서 대충 치고받게 만들면 과제 끝인데
또 이런다 또... ㅠㅠ
꼬릿말 보기
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호