수다닷컴

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

왜 output이 이상할까요...

큰모듬

2023.04.01


질문 제목 : 왜 output이 이상할까요...제가 코드를 다시짜서 했는데요
왜 ouput이 square 밖에 안나오는 걸까요?....
다른 것도 다 나와야하는데 계속 square 에서 끈기네요질문 내용 :

제생각은 for (int i = 0; i n; i++)
이부분정도가 틀린거 같은데 왜 다른 도형의 output은 안나올까요

코드입니다

#include iostream
using std::cout;
using std::endl;
using std::cin;
#include fstream
using std::ifstream;
#include iomanip
using std::setprecision;
using std::ios;
#include cmath
#include cstring
#include cstdlib
const int max_chars_per_line = 200;
const int max_tokens_per_line = 4;
const char* const delimiter = ;
struct square
{
double side;
};
struct rectangle
{
double length;
double width;
};

struct circle
{
double radius;
};

struct cube
{
double side;
};
struct prism
{
double length;
double width;
double height;
};

struct cylinder
{
double radius;
double height;
};
void printsquare(square*);
void printrectangle(rectangle*);
void printcircle(circle*);
void printcube(cube*);
void printprism(prism*);
void printcylinder(cylinder*);

int main()
{
void* shapes[100]={0};
int shapeid[100]={0};
ifstream fin;
fin.open(geo.txt);
if (!fin.good())
return 1;

coutprogrammer: jaeyoung paik\n;
coutdescription: this program calculate shape \n;
cout(sqare,rectangle,circle,cube,prism,cylinder) \n;
coutusing the input data \n;
coutendl;
while (!fin.eof())
{
char buf[max_chars_per_line];
fin.getline(buf, max_chars_per_line);
int n=0;
const char* token[max_tokens_per_line] = {0};
token[0] = strtok(buf, delimiter);
if (token[0])
{
for (n = 1; n max_tokens_per_line; n++)
{
token[n] = strtok(0, delimiter);
if (!token[n]) break;
}
}
for (int i = 0; i n; i++)
{
if (!((strcmp(token[0],square) == 0) || (strcmp(token[0],rectangle) == 0) || (strcmp(token[0],cube) == 0) || (strcmp(token[0],circle) == 0) || (strcmp(token[0],prism) == 0) || (strcmp(token[0],cylinder) == 0)))
{
cout token[0] invalid object endl;
break;
}
else if(strcmp(token[0], square)==0)
{
if (n == 1)
{
token[1] = 0;
}
p; square* s = new square;
s-side = token[1] == 0? 0.0 : atof(token[1]);
shapes[i] = s;
shapeid[i] = 1;
printsquare((square*)shapes[i]);
break;
}

else if(strcmp(token[0], rectangle) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
}
if (n == 2)
{
token[2] = 0;
}
rectangle* r = new rectangle;
r-length = token[1] == 0? 0.0 : atof(token[1]);
r-width = token[2] == 0? 0.0 : atof(token[2]);
shapes[i] = r;
shapeid[i] = 2;
printrectangle((rectangle*)shapes[i]);
break;
}
else if(strcmp(token[0], circle) == 0)
{
if (n == 1)
{
token[1] = 0;
}
circle* c = new circle;
c-radius = token[1] == 0? 0.0 : atof(token[1]);
shapes[i] = c;
shapeid[i] = 3;
printcircle((circle*)shapes[i]);
break;
}
else if(strcmp(token[0], cube) == 0)
{
if (n == 1)
{
token[1] = 0;
}
cube* c = new cube;
c-side = token[1] == 0? 0.0 : atof(token[1]);
shapes[i] = c;
shapeid[i] = 4;
printcube((cube*)shapes[i]);
break;
}
else if(strcmp(token[0], prism) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
token[3] = 0;
}
if (n == 2)
{
token[2] = 0;
token[3] = 0;
}
if (n == 3)
{
token[3] = 0;
}
prism* p = new prism;
p-length = token[1] == 0? 0.0 : atof(token[1]);
p-width = token[2] == 0? 0.0 : atof(token[2]);
p-height = token[3] == 0? 0.0 : atof(token[3]);
shapes[i] = p;
shapeid[i] = 5;
printprism((prism*)shapes[i]);
break;
}

else if(strcmp(token[0], cylinder) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
}
if (n == 2)
{
token[2] = 0;
}
cylinder* c = new cylinder;
c-radius = token[1] == 0? 0.0 : atof(token[1]);
c-height = token[2] == 0? 0.0 : atof(token[2]);
shapes[i] = c;
shapeid[i] = 6;
printcylinder((cylinder*)shapes[i]);
break;
}
}
fin.close();
}
}
void printsquare(square* s)
{
double area, perimeter;
area=pow(s-side, 2);
perimeter=4*s-side;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutsquare side=s-side;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area perimeter=perimeterendl;
}
void printrectangle(rectangle* r)
{
double area, perimeter;
area=(r-length)*(r-width);
perimeter=2*(r-length)+2*(r-width);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutrectangle length=(r-length) width=(r-width);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area perimeter=perimeterendl;
}
void printcircle (circle* r)
{
double area, circumference;
area=4*atan(1.0)*pow((r-radius), 2);
circumference=2*4*atan(1.0)*(r-radius);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcircle radius=(r-radius);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area circumference=circumferenceendl;
}
void printcube(cube* s)
{
double surface,volume;
volume=pow((s-side), 3);
surface=6*pow((s-side), 2);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcube side=(s-side);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
void printprism(prism* p)
{
double surface,volume;
volume=(p-length)*(p-width)*(p-height);
surface=2*(p-length)*(p-width)+2*(p-width)*(p-height)+2*(p-height)*(p-length);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutprism length=(p-length) width=(p-width) height=(p-height);
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
void printcylinder(cylinder* c)
{
double surface,volume;
volume=4*atan(1.0)*pow((c-radius), 2)*(c-height);
surface=2*4*atan(1.0)*pow((c-radius), 2)+2*4*atan(1.0)*(c-radius)*(c-height);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcylinder radius=(c-radius) height=(c-height);
cout.setf(it.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}input

square 14.5
rectangle 14.5 4.65
circle 14.5
cube 13
prism 1 2 3
spheres 2.4
cylinder 1.23
cylinder 50 1.23
triangle 1.2 3.2

output
programmer: jaeyoung paik
description: this program calculate shape
(sqare,rectangle,circle,cube,prism,cylinder)
using the input data

square side=14.5 area=210.25 perimeter=58.00

신청하기





COMMENT

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

  • 터전

    ㅜㅜ 정말 감사합니다
    해결 ㄳㄳㄳㄳ

  • 앵겨쪼

    fin.close(); for문 다음에 닫아 버리기 때문입니다. while 문 밖으로 보내 주세요.

번호 제 목 글쓴이 날짜
2699816 오류 질문입니다.. (1) 해비치 2025-06-29
2699763 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) 미라 2025-06-28
2699555 c언어 다항식을 입력을 했는데 왜 출력이 안될까요? 피스케스 2025-06-27
2699528 C언어 포인터연산 질문입니다. (3) 안녕나야 2025-06-26
2699476 끌어올림;;달력 짜봤는데요 이 소스 줄일 수 있나요? - 스샷첨부 (2) 클라우드 2025-06-26
2699444 [좀 급함] system("explorer [주소] ") 문에 변수를 사용할 수 있나요? 알 2025-06-26
2699415 파일//read//와 배열 아란 2025-06-25
2699386 구조체 안에 일부분만 char 배열에 복사하려면 어떻게 해야하나요? (1) 미즈 2025-06-25
2699361 연결리스트 정렬하는 부분에 대해서 질문 드립니다 아이처럼 2025-06-25
2699304 [기초]아직 안주무시는분 계신가요..?포인터배열? 좀 도와주세요. 놀리기 2025-06-24
2699272 printf() 함수이용해서 프로그램 만들기 질문요! (5) 다가 2025-06-24
2699221 PUSH와 POP코드를 더 간단하게 어떻게 해야할까요? 파라미 2025-06-24
2699192 설치오류가 자꾸 나요 한번봐주세여~ (1) 소녀틳향기 2025-06-23
2699161 for loop안에 있는 if문 (9) Orange 2025-06-23
2699105 링크더리스트 이전 링크값 출력함수. 꼬꼬마 2025-06-23
2699078 정수를 한자리씩 배열에 담는 법은 어떻게 하나요.. (4) 귀염포텐 2025-06-22
2699024 C언어 공부하려는데 도와주세요!!! (2) 달님 2025-06-22
2698994 날짜 계산하는 C 코드 짜고 있는데 꽉 막혀서 질문드립니다.. (6) 별 2025-06-22
2698967 파일삭제 윈도우 폴더까지 접근하게하는 함수가 뭔가요 (2) 샤인 2025-06-21
2698938 c언어 메모리질문 (3) 나래 2025-06-21
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

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