알려주세요 제발 인풋을 읽질못함
아리솔
질문 제목 : 알려주세요 제발 인풋을 읽질못함인풋파일에 빈줄이 없으면 되는데.... 빈줄이 잇으면 아웃풋이 에러가 나네요;;;
질문 내용 :
빈줄을 어떻게 읽을까요? ㄷㄷ
#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; -
아라
빈줄만 있는 것은 어떻게 구분하시나요?
해당 줄이 빈줄인지 여부를 체크하는 부분이 안보이는 것 같아서...
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |
2691393 | UNIX 시스템을 사용하려면 어떤 프로그램이 좋을까요? (5) | 든솔 | 2025-04-13 |