很简单的一串代码 有问题 请大神求助
想问问其中的s_gets函数是来干什么的还有我的第一个输入无效和怎么退不出循环了。。。。拜托了#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TSIZE 45
struct film {
char title;
int rating;
structfilm *next ;
};
char *s_gets(char * st , int n);
int main(void)
{
struct film *head = NULL ;
struct film *prev = NULL , *current;
char input;
//收集并采集数据
puts("Enter first movie title :");
while (s_gets(input , TSIZE) != NULL && input != '\0')
{
current = (struct film *) malloc (sizeof(struct film));
if (head == NULL)
head = current;
else
prev->next = current ;
current ->next = NULL;
strcpy(current -> title, input );
puts("Enter your rating <0-10>");
scanf("%d",¤t -> rating);
while (getchar() != '\n')
continue;
puts("Enter you next movies title (empty line to exit):");
prev = current;
}
// 显示电影列表
if (head == NULL)
printf("No data entered . ");
else
printf("Here is the movies list :");
current = head ;
while (current != NULL)
{
printf("Movies: %sRating: %d \n",current ->title ,current ->rating);
current -> next = current ;
};
current = head ;
while (current != NULL)
{
free (current);
head = current ->next;
}
printf("Bye\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');
if(find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
} 从键盘流中读取数据 s_gets函数是来干什么的还有我的第一个输入无效和怎么退不出循环了
问题一, s_get函数是用来获取从键盘上输入的一串字符的,也就是你的应用程序需要显示什么,就输入什么。
问题二, strchr(st, '\n'),以\n为结束字符串的输入, 也就是直接按enter键就可以退出输入的循环了。
页:
[1]