알려주세요 제발 인풋을 읽질못함
아리솔
질문 제목 : 알려주세요 제발 인풋을 읽질못함인풋파일에 빈줄이 없으면 되는데.... 빈줄이 잇으면 아웃풋이 에러가 나네요;;;
질문 내용 :
빈줄을 어떻게 읽을까요? ㄷㄷ
#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; -
아라
빈줄만 있는 것은 어떻게 구분하시나요?
해당 줄이 빈줄인지 여부를 체크하는 부분이 안보이는 것 같아서...
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2694069 | unsigned 질문입니다. | 힘차 | 2025-05-07 |
2694012 | 전공 비전공자 개발자 (10) | 말글 | 2025-05-07 |
2693984 | 오버로딩이 무엇인가요? (2) | 헛매질 | 2025-05-07 |
2693956 | PlaySound재생이 안됩니다!(C에 음악넣기) | 지존 | 2025-05-06 |
2693928 | &와 *의 사용에 관한 명확한 이해 | 제나 | 2025-05-06 |
2693903 | 반복문 설명좀요 ㅠㅠ (2) | 란새 | 2025-05-06 |
2693869 | stdio.h 는 왜 쓰는건가요? (1) | 큰꽃들 | 2025-05-06 |
2693842 | 포인터 변수의 주소값끼리 더하는 것에 대해서 질문드립니다. (1) | 진솔 | 2025-05-05 |
2693811 | 소수 출력;;;; | 화이트캣 | 2025-05-05 |
2693788 | 이런 함수는 없나요? (3) | 앤드류 | 2025-05-05 |
2693758 | txt파일 불러와서 행렬로 저장 | 큰애 | 2025-05-05 |
2693727 | scanf 오류 문제!! (2) | 큰나래 | 2025-05-04 |
2693704 | 구조체 주소록 문제인데 도와주세요 (2) | 도1도캣 | 2025-05-04 |
2693676 | 열혈강의 c언어 질문입니다 | 하양이 | 2025-05-04 |
2693647 | 12.620000 을요 12.620 으로 어떻게 표현해요? (2) | 파도 | 2025-05-04 |
2693619 | 타이틀 코드.. | 단순드립 | 2025-05-03 |
2693591 | 컴파일 에러에서 질문드립니다 (3) | 게자리 | 2025-05-03 |
2693463 | 동적할당 이용시 fwrite사용을 어떻게 해야하나요..? (10) | 일본어못해요 | 2025-05-02 |
2693387 | 배열문제입니다 수정오류캡쳐했습니다 (6) | 연하얀 | 2025-05-01 |
2693356 | text 입출력 내림차순 질문입니다 ㅠ | 빛글 | 2025-05-01 |