14-4 구조체 배열 연습 문제중
275
작성한 질문수 1
구조체 연습 문제중 s_gets 함수를 통해서 책 이름을 적는 코드중에
저는 if문을 사용하지 않고
s_gets(library[count].title, MAX_AUTHOR) ;
로 문장을 끝내었는데,
강의에서는
if (s_gets(library[count].title, MAX_AUTHOR) == NULL) break;
이렇게 작성을 해주셨습니다.
분명히 전 강의해서 설명 해주셨던거 같아서 계속 찾아보다가 찾을 수 없어서 이렇게 질문을 드립니다.
왜 저렇게 작성을 하셨는지 알려주실수 있으신가요?
답변해주시면 감사드리겠습니다. ( 밑에는 강의 예제 코드를 올려드리겠습니다.)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_TITLE 40
#define MAX_AUTHOR 40
#define MAX_BOOKS 3
char* s_gets(char* st, int n)
{
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin); // scanf 불가
// 빈칸이 있는 문자열 입력 받을 수 있다.
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue; // 버퍼를 지워버린다.?!?!?!?!?!?!?!?!?
}
return ret_val;
}
struct book
{
char title[MAX_TITLE];
char author[MAX_AUTHOR];
float price;
};
int main()
{
struct book library[MAX_BOOKS] = { {"Empty","Empty",0.0f} }; //-> 기본적으로 초기화 하는 코드
int count = 0;
while (1)
{
printf("Input a book title or press[Enter] to stop.\n>>");
//if (s_gets(library[count].title, MAX_AUTHOR) == NULL) break; 이 부분이 궁금한데 답변해주시면 감사드리겠습니다.
s_gets(library[count].title, MAX_AUTHOR);
if (library[count].title[0] == '\0') break;
printf("Input the author . \n>>");
s_gets(library[count].author, MAX_AUTHOR);
printf("Input the price . \n>>");
int flag = scanf("%f", &library[count].price);
while (getchar() !='\n')
continue;
count++;
if (count == MAX_BOOKS)
{
printf("No more books . \n");
break;
}
}
if (count > 0)
{
printf("\nThe list of books :\n");
for (int index = 0; index < count; index++)
printf("\%s\" wiritten by %s: $%.1f\n",
library[index].title, library[index].author, library[index].price);
}
else
printf("No books to show. \n");
return 0;
}
답변 2
1
안녕하세요?
프로그램 실행 후 EOF를 입력으로 넣어보시는 것이 생각하시는데 도움이 될 것 같네요.
아마 성균님도 이런 프로그램의 실행을 원치 않으실 거라 생각합니다.
Export template 안됨
1
8
2
완전히 똑같이 따라해도 exe파일이 안만들어져서 실행이 안됩니다.
1
52
3
main 함수에서 왜 int만 선언이 되는걸까요
1
56
2
8비트 2진수 변환시 왜 1을 더해야하나요?
1
54
2
혹시 강의를 빠르게 수강하려면 어디서부터 듣는게 좋을까요?
1
49
1
프로토타입과 함수간의 인자 불일치
1
73
2
12.12 헤더 관련 질문
1
60
2
Visual Studio Community 2026 사용 문의
1
137
2
Q. 15:30, 부호가 있는 8비트 정수 질문
1
60
2
getchar(), putchar()
1
93
3
강의자리ㅛ
1
79
2
비주얼스튜디오코드로 공부해도 상관없나요?
1
112
2
소스파일안에 여러 파일
1
75
2
F5와 F7의 차이
1
76
2
c = TWO * (a+b); 에서 a와 b는?
1
58
2
; 세미콜론을 붙이는 기준에 문의
1
68
1
Step over 기능 문의
1
53
2
2.6 강의 따옴표 출력 규칙 문의
1
74
2
int main 함수 관련 오류 문의
1
67
2
13.4 words[0]
0
60
2
11.7 함수를 구현해 봤습니다.
1
62
2
11.6 직접 strcmp와 strncmp를 구현해 보았습니다.
1
64
2
11.6 my_strcat과 my_strncat을 구현해봤습니다.
1
53
2
11.6 fit_str함수를 구현해 봤습니다.
1
53
2





