不知道这段代码的目的是啥?
尤其是while那个getchar的目的
if (ret_val)//retval返回值不为0时,这行的目的是?
{
find = strchr(st, '\n'); // 查找换行符
if (find) // if the address is not NULL=0,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
//* book.c -- one-book inventory */
#include <iostream>
#include <string.h>
char * s_gets(char * st, int n);//函数的参数是一个指针 一个整型变量(最长书名,最长作者名)
#define MAXTITL 41 /* maximum length of title + 1 */
#define MAXAUTL 31 /* maximum length of author's name + 1 */
struct book { /* 结构声明: tag is book *///模板
char title[MAXTITL];
char author[MAXAUTL];
float value;
};//注意分号,如果放在函数内部,就只有内部可以使用 /* end of structure template */
//可以定义 结构变量的指针
int main(void)
{
struct book library; /* 创建 library as a book variable *///可以定义两个不同的变量,book a;book b;都是可以的
printf("Please enter the book title.\n");
s_gets(library.title, MAXTITL); /* access to the title portion */
printf("Now enter the author.\n");
s_gets(library.author, MAXAUTL);
printf("Now enter the value.\n");
scanf("%f", &library.value);
printf("%s by %s: $%.2f\n",library.title,
library.author, library.value);
printf("%s: \"%s\" ($%.2f)\n", library.author,
library.title, library.value);
printf("Done.\n");
return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)//retval返回值不为0时,这行的目的是?
{
find = strchr(st, '\n'); // 查找换行符
if (find) // if the address is not NULL=0,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}