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를 통해 다시 읽으세요.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700695 | 간단한 메모장 구현을 할려고 하는데요 (9) | 늘솜 | 2025-07-07 |
2700668 | c언어 질문입니다. 도와주세요~ (3) | 가자 | 2025-07-07 |
2700639 | 한글입력받아서 ㄱㄴㄷ순서대로출력하는법좀 | 두빛나래 | 2025-07-06 |
2700610 | 정말 기초적인 더하기,여백 문제 help | 무슬 | 2025-07-06 |
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |