input 파일에 빈줄때문에 ㄷㄷ
사과
질문 제목 : input 파일에 빈줄때문에 ㄷㄷinput 파일에 있는 데이터를 이용하여 계산하는 건데요
중간에 스페이스 한칸 때문에 계속 에러가 나와요
질문 내용 :
제 코드는
#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 = ;
void square (double);
void rectangle (double, double);
void circle (double);
void cube (double);
void prism (double, double, double);
void cylinder (double, double);
int main()
{
ifstream fin;
fin.open(geo.txt);
if (!fin.good())
return 1;
double sside=0,
rlength=0,
rwidth=0,
curadius=0,
cside=0,
plength=0,
pwidth=0,
pheight=0,
cyradius=0,
cyheight=0;
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] || token[n]==0) 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)))
{
cout token[0] invalid object endl;
}
if(strcmp(token[0], square)==0)
{
if (n == 1)
{
token[1] = 0;
}
sside=atof(token[1]);
square (sside);
}
if(strcmp(token[0], rectangle) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
}
if (n == 2)
{
token[2] = 0;
}
rlength = atof(token[1]);
rwidth = atof(token[2]);
rectangle (rlength, rwidth);
}
if(strcmp(token[0], circle) == 0)
{
if (n == 1)
{
token[1] = 0;
}
curadius = atof(token[1]);
circle (curadius);
}
if(strcmp(token[0], cube, cube) == 0)
{
if (n == 1)
{
token[1] = 0;
}
cside = atof(token[1]);
cube (cside);
}
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;
}
plength = atof(token[1]);
pwidth = atof(token[2]);
pheight = atof(token[3]);
prism (plength, pwidth, pheight);
}
if(strcmp(token[0], cylinder) == 0)
{
if (n == 1)
{
token[1] = 0;
token[2] = 0;
}
if (n == 2)
{
token[2] = 0;
}
cyradius = atof(token[1]);
cyheight = atof(token[2]);
cylinder (cyradius, cyheight);
}
}
}
void square(double side)
{
double area, perimeter;
area=pow(side, 2);
perimeter=4*side;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutsquare side=side;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area perimeter=perimeterendl;
}
void rectangle(double length, double width)
{
double area, perimeter;
area=length*width;
perimeter=2*length+2*width;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutrectangle length=length width=width;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area perimeter=perimeterendl;
}
void circle (double radius)
{
double area, circumference;
area=4*atan(1.0)*pow(radius, 2);
circumference=2*4*atan(1.0)*radius;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcircle radius=radius;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) area=area circumference=circumferenceendl;
}
void cube(double side)
{
double surface,volume;
volume=pow(side, 3);
surface=6*pow(side, 2);
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcube side=side;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
void prism(double length, double width, double height)
{
double surface,volume;
volume=length*width*height;
surface=2*length*width+2*width*height+2*height*length;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutprism length=length width=width height=height;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
void cylinder(double radius, double height)
{
double surface,volume;
volume=4*atan(1.0)*pow(radius, 2)*height;
surface=2*4*atan(1.0)*pow(radius, 2)+2*4*atan(1.0)*radius*height;
cout.unsetf(ios::fixed|ios::showpoint);
cout setprecision(6);
coutcylinder radius=radius height=height;
cout.setf(ios::fixed|ios::showpoint);
coutsetprecision(2) surface area=surface volume=volumeendl;
}
이거고요
input file이
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
입니다... 보시다시피 prism 과 spheres 사이에 빈칸때문에 에러가 나요
이것만 없애면 잘되는데 말이죠
무엇이 잘못 된 것일까요?
-
댓걸
빈 공란 값을 strtok을 하면 오작동할 수 있습니다.
읽은 후 공란인지 확인하는게 좋을꺼같네요. -
물보라
if (token[0])
{
for (n = 1; n max_tokens_per_line; n++)
{
token[n] = strtok(0, delimiter);
if (!token[n] || token[n]==0) break;
}
}
else continue;// 이것을 추가해보세요. -
한가람
for (int i = 0; i n; i++)
{
if(strcmp(token[0], \SQUARE\)==0)
{
...
...
break;
}
....
....
}
이렇게 해도 맞나요 일단 실험차 했는데 돌아가긴 하더군요 -
갅지삘여우
아..... 빈줄은 없애면 안되요..... 조금만 더 힌트를 주시면 안될까요
어떻게 읽을 buf가 없다는 걸 알수 잇나요? -
날개
저 빈줄을 input 파일에서 지우면 안되나요?
또는 소스로 해결하시려면 읽으신 후, 읽은 buf가 없으면 continue를 통해 다시 읽도록 하세요. -
벼리
어느부분을 없애라는 건가요?
죄송해요 ㅜㅜ 잘 모르겠어요 -
로운
잘못됬다기보다 저 부분을 없애세요.
읽으면서 읽어드리면서 읽은게 없으면 무시하고 다시 읽도록 하세요. 저장하지말고
fin.getline(buf, MAX_CHARS_PER_LINE); 에서 읽었을 때 buf를 확인해서 데이터 없으면
continue를 통해 다시 읽으세요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695355 | c언어 for함수 | 미쿡 | 2025-05-19 |
2695327 | 안녕하세요 제가 이번에 좀 큰 프로그램을.. | 악당 | 2025-05-19 |
2695295 | mutex동기화의 thread기반 채팅 서버소스 질문입니다 | 그루터기 | 2025-05-19 |
2695270 | 질문이요..swap 관한겁니다..ㅠㅠ (3) | 콩알녀 | 2025-05-19 |
2695244 | 노땅초보궁금한게 하나 있는데요..반복문(while문)초보자질문 (6) | 큰꽃늘 | 2025-05-18 |
2695166 | do while 문 어떤것이잘못된건지 모르겠어요 (2) | 아이폰 | 2025-05-18 |
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
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 |