|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
对于上次的帖子忘贴代码感到十分羞愧 这次重新发一个帖子 希望路过的大佬能帮帮我 程序使用vs2019写的
- void input_print() //输入与输出的函数
- {
- struct node* p1, * p2, * head;
- int n, temp;
- char temp_2;
- p1 = p2 = (struct node*)malloc(sizeof(struct node));
- printf("Plese input the book name:");
- scanf("%s", &p1->name);
- printf("Plese input the book author:");
- scanf("%s", &p1->author);
- head = NULL;
- n = 0;
- while (1)
- {
- n++;
- if (n == 1)
- {
- head = p1;
- }
- else
- {
- p2->next = p1;
- }
- p2 = p1;
- p1 = (struct node*)malloc(sizeof(struct node));
- printf("Do you want to input information again? Y/N");
- scanf("%s", &temp_2);
- if (temp_2 == 'n' || temp_2 == 'N')
- {
- break;
- }
- printf("Plese input the book name:");
- scanf("%s", &p1->name);
- printf("Plese input the book author:");
- scanf("%s", &p1->author);
- }
- p1 = NULL;
- while (head != NULL)
- {
-
- for (temp = 1; temp <= n; temp++)
- {
- printf("第%d本书", temp);
- printf("书名:《%s》\n", head->name);
- printf("作者: %s", head->author);
- printf("\n \n");
-
- head = head->next;
- }
-
- head = NULL;
- }
-
- } //这里出现"Run-Time Check Failure #2 - Stack around the variable 'temp_2' was corrupted."错误提示
复制代码
这是node结构体的内容
- struct node
- {
- char name[50];
- char author[50];
- struct node* next;
- };
复制代码
本帖最后由 jackz007 于 2021-3-15 23:33 编辑
- char temp_2;
- . . . . . .
- while (1)
- {
- . . . . . .
- scanf("%s", & temp_2) ; // char 型的变量不可以这样获取键盘输入
复制代码
正确的方法
- fflush(stdin) ; // 添加此句,读取单个字符之前,需要清空键盘缓冲区
- scanf("%c" , & temp_2) ; // char 型的变量应该这样获取键盘输入
复制代码
也就是说,把原来的 scanf() 那一句改成上面的那两句就可以了。
|
|