Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Cleancode
- 종만북
- programmers
- C
- 백준
- Python
- JavaScript
- Math
- String
- 생활코딩
- 인프런
- 따라하며 배우는 C언어
- Algospot
- BOJ
- server
- C언어
- 따라하면서 배우는 C언어
- BFS
- DP
- greedy
- graph
- 따배씨
- Algorithm
- dfs
- BASIC
- udemy
- 정수론
- web
- php
- sorting
Archives
- Today
- Total
몽상실현개발주의
[따배씨] 14.4 구조체의 배열 연습 문제 본문
따배씨 - 따라하며 배우는 C언어
14강 구조체_1
14.4 구조체의 배열 연습 문제
#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);
//fgets(<#char *restrict#>, <#int#>, <#FILE *#>)
//input 과 돌일한 str 매개변수 or NULL return
if (ret_val)
{
find = strchr(st, '\n'); // look for newline
if (find) // if the address is not NULL
* find = '\0'; // place a null characgter there
else
while(getchar() != '\n')
continue;; // dispose of rest of line
}
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}, };
// array of book structrure
int count = 0;
while(1){
printf("Input a boo title or press [Enter] to stop\n>>");
if (s_gets(library[count].title, MAX_TITLE) == NULL) break;
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; /* clear input line */
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\" written by %s: $%.1f\n", library[index].title, library[index].author, library[index].price);
}
else
printf("No books to show.\n");
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n'); // look for newline
if (find) // if the address is not NULL
* find = '\0'; // place a null characgter there
else
while(getchar() != '\n')
continue;; // dispose of rest of line
}
return ret_val;
}
ret_val = fgets(st, n, stdin);
//fgets(<#char *restrict#>, <#int#>, <#FILE *#>)
- 입력 처리 함수
- fgets(string, string length, file)
- input 과 돌일한 str 매개변수 or NULL return
- 입력 file 변수를 stdin 으로 console 입력 값으로 처리 가능
if (library[count].title[0] == '\0') break;
- 입력한 문자열이 없을 시, 처리
while (getchar() != '\n')
continue; /* clear input line */
- buffer 비우기
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com
'Language > C' 카테고리의 다른 글
[따배씨] 14.9 구조체와 할당 메모리 (0) | 2021.06.26 |
---|---|
[따배씨] 14.8 구조체와 함수 연습문제 (0) | 2021.06.22 |
[따배씨] 14.4 구조체의 메모리 할당 (0) | 2021.06.22 |
[따배씨] 14.2 구조체의 기본적인 사용법 (0) | 2021.06.22 |
[따배씨] 14.1 구조체가 필요한 이유 (0) | 2021.06.22 |
Comments