알려주세요 제발 인풋을 읽질못함
아리솔
질문 제목 : 알려주세요 제발 인풋을 읽질못함인풋파일에 빈줄이 없으면 되는데.... 빈줄이 잇으면 아웃풋이 에러가 나네요;;;
질문 내용 :
빈줄을 어떻게 읽을까요? ㄷㄷ
#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*);
void printerror(char* str)
{
cout str invalid object endl;
}
int main()
{
void* shapes[100]={};
int shapeid[100]={};
int i;
ifstream fin;
fin.open(input.txt);
if (!fin.good())
return 1;
coutprogrammer: jaeyoung paik\n;
coutdescription: this program calculate shape \n;
cout(square,rectangle,circle,cube,prism,cylinder) \n;
coutusing the input data \n;
coutendl;
int n=0;
int count = 0;
while (!fin.eof())
{
char buf[max_chars_per_line];
fin.getline(buf, max_chars_per_line);
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;
}
}
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)))
{
char str[20];
strcpy(str,token[0]);
shapes[count] = str;
shapeid[count] = 0;
}
else if(strcmp(token[0], square)==0)
{
if (n == 1)
{
token[1] = 0;
}
square* s = new square;
s-side = token[1] == 0? 0.0 : atof(token[1]);
shapes[count] = s;
shapeid[count] = 1;
}
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[count] = r;
shapeid[count] = 2;
}
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[count] = c;
shapeid[count] = 3;
}
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[count] = c;
shapeid[count] = 4;
}
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[count] = p;
shapeid[count] = 5;
}
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[count] = c;
shapeid[count] = 6;
}
count++;
}
fin.close();
for (i = 0; i count; i++)
{
if (shapeid[i] == 1)
printsquare((square*)shapes[i]);
else if (shapeid[i] == 2)
printrectangle((rectangle*)shapes[i]);
else if (shapeid[i] == 3)
printcircle((circle*)shapes[i]);
else if (shapeid[i] == 4)
printcube((cube*)shapes[i]);
else if (shapeid[i] == 5)
printprism((prism*)shapes[i]);
else if (shapeid[i] == 6)
printcylinder((cylinder*)shapes[i]);
else
printerror((char*)shapes[i]);
}
system(pause);
return 0;
}
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(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
저빈줄도 포함입니다.;;
-
큰돛
왜 C++언어 질문을 C언어 Q&A 게시판에 올리는지 이해가 안 되네요.
-
의사양반
\tif (token[0])
\t{
\t\tfor (n = 1; n MAX_TOKENS_PER_LINE; n++)
\t\t{
\t\t\ttoken[n] = strtok(0, DELIMITER);
\t\t\tif (!token[n]) break;
\t\t}
\t}
\telse
\t\tcontinue;
빈줄 확인은 strtok 반환값이 NULL 이 들어 옵니다. NULL 일땐 다시 while 문으로 고고싱 -
놓아주세요
\tif (token[0])
\t{
\t\tfor (n = 1; n MAX_TOKENS_PER_LINE; n++)
\t\t{
\t\t\ttoken[n] = strtok(0, DELIMITER);
\t\t\tif (!token[n]) break;
\t\t}
\t}
\telse
\t continue; -
아라
빈줄만 있는 것은 어떻게 구분하시나요?
해당 줄이 빈줄인지 여부를 체크하는 부분이 안보이는 것 같아서...
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2698782 | 기초적인 함수 질문이요ㅠㅠㅠㅠ | 내담 | 2025-06-20 |
2698749 | 프로그램 짜던 도중 패닉입니다...ㅜ | 파랑 | 2025-06-19 |
2698719 | 조건부컴파일 질문입니다.~ (2) | 큐트 | 2025-06-19 |
2698693 | 재귀 함수 에러 | 바닐라 | 2025-06-19 |
2698673 | 고민이있는데 들어좀주세요!! (1) | 초코맛캔디 | 2025-06-19 |
2698644 | 1부터 n까지의 합을 구하는데 엄청긴숫자의 합을 구할때는 어떻게 해야하나요? (4) | 슬우 | 2025-06-18 |
2698616 | 다른 함수로 안넘어갑니다..;;; | 도1도캣 | 2025-06-18 |
2698587 | 배열하다 막혀서... (3) | WhiteCat | 2025-06-18 |
2698559 | 문자열을 비우는방법 (2) | 하늘 | 2025-06-18 |
2698528 | 착하고 친절한 선생씌구해염~ㅋㅋ (4) | 옆집언니야 | 2025-06-17 |
2698502 | 자료구조 큐 | 캔서 | 2025-06-17 |
2698477 | 실행화면 배경문의요 | 선아 | 2025-06-17 |
2698430 | 변수의 값이 저장이 않되네요;; (4) | 피네 | 2025-06-16 |
2698404 | C#을 배울려고 하는데 C나 C++을 알아야 하나요 ?? (1) | 신당 | 2025-06-16 |
2698342 | 프로그램 질문점녀 (4) | 데빌의눈물 | 2025-06-16 |
2698318 | 파일 입출력 질문입니다~ (2) | 꽃 | 2025-06-15 |
2698291 | 문자 출력 함수 : putchar, fputc에 관하여. | 으뜸 | 2025-06-15 |
2698261 | 씨언어 (1) | 마리 | 2025-06-15 |
2698212 | 구조체, 포인터가 같이 들어간 프로그램 소스코드 있으신분? (4) | 그림자 | 2025-06-14 |
2698184 | 간단한 C언어 인데 .. | 붕붕 | 2025-06-14 |