• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

교수님 s_gets 함수를 만드는 이유는 무엇인가요?

20.02.23 21:03 작성 조회수 342

0

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX_TITLE 40
#define MAX_AUTHOR 40
#define MAX_BOOKS 3

int main()
{
	struct book
	{
		char title[MAX_TITLE];
		char author[MAX_AUTHOR];
		float price;
	};

	struct book library[MAX_BOOKS];
	int count = 0;

	while (1)
	{
		printf("input a book title or press [Enter] to stop.\n>>");
		gets(library[count].title);
		if (library[count].title[0] == '\0') break;

		printf("input the author.\n>>");
		gets(library[count].author);

		printf("iuput the price.\n>>");
		scanf("%f", &library[count].price);
		while (getchar() != '\n') continue;

		count++;
		if (count == MAX_BOOKS) break;
	}
	if (count > 0)
	{
		printf("The list of books:\n");
		for (int i = 0; i < count; i++)
			printf("\"%s\" written by %s : %.1f\n", library[i].title, library[i].author, library[i].price);
	}
	else printf("There is noo book.");

	return 0;
}

답변 2

·

답변을 작성해보세요.

0

호두님의 프로필

호두

2020.02.26

0

김성수님의 프로필

김성수

질문자

2020.02.23

위처럼 gets 함수를 써도 될거같은데.. 사실 전부분 강의에서  getchar 함수를 사용하는 부분을 좀 이해 못한 부분이 있긴 합니다.. 함수 나오는 강의 분:초는 4:26초 입니다.