11 04 结构体数组(rewind(stdin)、getchar()、BUFSIZ)

11 04 结构体数组
C语言中的输入输出流和缓冲区(重点)详解
解释:为什么scanf被跳过或不执行

缓冲区

fflush(stdout);//过期
rewind(stdin);//清空缓存区
getchar();//本文件用来吃掉\n
printf("%d\n", BUFSIZ);//缓冲区大小,512B

好多人都会遇到这问题,因为scanf没读完所有缓存中的信息。把回车剩外面了,被下次的scanf接收到了。
缓冲区就像菜鸟驿站,协调用户(零零散散来拿)和快递员(一次处理很多件),一个慢,一个快的问题。
分为全、行、不带缓冲区,scanf遇到的问题属于行缓冲区
https://blog.csdn.net/HNAKXR/article/details/81047391

01 问题:for循环 输入次数和scanf对应不上

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
	char name[21];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
};

void main()
{
	struct students stu[2];
	
	for (int i = 0; i < 2; i++) {
		printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
		scanf("%s", stu[i].name);
		scanf("%d", &stu[i].age);
		scanf("%s", stu[i].tel);
		scanf("%f", &stu[i].scores[0]);
		scanf("%f", &stu[i].scores[1]);
		scanf("%f", &stu[i].scores[2]);
		scanf("%c", &stu[i].sex);
	}
	for (int i = 0; i < 2; i++) {
		printf("%s\n", stu[i].name);
		printf("%d\n", stu[i].age);
		printf("%s\n", stu[i].tel);
		printf("%.1f\n", stu[i].scores[0]);
		printf("%.1f\n", stu[i].scores[1]);
		printf("%.1f\n", stu[i].scores[2]);
		printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
	}

	system("pause");
}

在这里插入图片描述
在这里插入图片描述

01 01 原因:%c会直接读取缓冲区上一个字符

在这里插入图片描述

01 02 方法一:getchar()吃掉\n

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
	char name[21];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
};

void main()
{
	struct students stu[2];
	
	for (int i = 0; i < 2; i++) {
		printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
		scanf("%s", stu[i].name);
		scanf("%d", &stu[i].age);
		scanf("%s", stu[i].tel);
		scanf("%f", &stu[i].scores[0]);
		scanf("%f", &stu[i].scores[1]);
		scanf("%f", &stu[i].scores[2]);
		scanf("%c", &stu[i].sex);
		
		getchar();
	}
	
	for (int i = 0; i < 2; i++) {
		printf("%s\n", stu[i].name);
		printf("%d\n", stu[i].age);
		printf("%s\n", stu[i].tel);
		printf("%.1f\n", stu[i].scores[0]);
		printf("%.1f\n", stu[i].scores[1]);
		printf("%.1f\n", stu[i].scores[2]);
		printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
	}

	system("pause");
}


在这里插入图片描述

01 03 方法二:rewind(stdin)清空缓存区

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
	char name[21];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
};

void main()
{
	struct students stu[2];
	
	for (int i = 0; i < 2; i++) {
		printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
		scanf("%s", stu[i].name);
		scanf("%d", &stu[i].age);
		scanf("%s", stu[i].tel);
		scanf("%f", &stu[i].scores[0]);
		scanf("%f", &stu[i].scores[1]);
		scanf("%f", &stu[i].scores[2]);
		scanf("%c", &stu[i].sex);
		
		rewind(stdin);
	}

	for (int i = 0; i < 2; i++) {
		printf("%s\n", stu[i].name);
		printf("%d\n", stu[i].age);
		printf("%s\n", stu[i].tel);
		printf("%.1f\n", stu[i].scores[0]);
		printf("%.1f\n", stu[i].scores[1]);
		printf("%.1f\n", stu[i].scores[2]);
		printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
	}

	system("pause");
}

//printf("%d\n", BUFSIZ);

在这里插入图片描述

02 问题:scanf输入所有信息,第2个scanf时出错

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
	char name[21];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
};

void main()
{
	struct students stu[2];

	for (int i = 0; i < 2; i++) {
		printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
		scanf("%s%d%s%f%f%f%c", stu[i].name, &stu[i].age, stu[i].tel, &stu[i].scores[0], &stu[i].scores[1], &stu[i].scores[2], &stu[i].sex);
	}

	for (int i = 0; i < 2; i++) {
		printf("%s\n", stu[i].name);
		printf("%d\n", stu[i].age);
		printf("%s\n", stu[i].tel);
		printf("%.1f\n", stu[i].scores[0]);
		printf("%.1f\n", stu[i].scores[1]);
		printf("%.1f\n", stu[i].scores[2]);
		printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
	}

	system("pause");
}

在这里插入图片描述

02 01 原因:%c读取了空格’F’,导致后面的顺延乱码

02 02 方法一:getchar()吃掉\n

02 03 方法二:rewind(stdin)清空缓存区

02 04 方法三:规定输入,

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
	char name[21];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
};

void main()
{
	struct students stu[2];

	for (int i = 0; i < 2; i++) {
		printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
		scanf("%s%d%s%f%f%f,%c", stu[i].name, &stu[i].age, stu[i].tel, &stu[i].scores[0], &stu[i].scores[1], &stu[i].scores[2], &stu[i].sex);
	}

	for (int i = 0; i < 2; i++) {
		printf("%s\n", stu[i].name);
		printf("%d\n", stu[i].age);
		printf("%s\n", stu[i].tel);
		printf("%.1f\n", stu[i].scores[0]);
		printf("%.1f\n", stu[i].scores[1]);
		printf("%.1f\n", stu[i].scores[2]);
		printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
	}

	system("pause");
}

在这里插入图片描述

03 BUFSIZ 512B 缓冲区大小

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
	char name[21];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
};

void main()
{
	struct students stu[1];
	
	for (int i = 0; i < 1; i++) {
		printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
		scanf("%s", stu[i].name);
		scanf("%d", &stu[i].age);
		scanf("%s", stu[i].tel);
		scanf("%f", &stu[i].scores[0]);
		scanf("%f", &stu[i].scores[1]);
		scanf("%f", &stu[i].scores[2]);
		scanf("%*c", &stu[i].sex);
		printf("%d\n", BUFSIZ);
	}
	for (int i = 0; i < 1; i++) {
		printf("%s\n", stu[i].name);
		printf("%d\n", stu[i].age);
		printf("%s\n", stu[i].tel);
		printf("%.1f\n", stu[i].scores[0]);
		printf("%.1f\n", stu[i].scores[1]);
		printf("%.1f\n", stu[i].scores[2]);
		printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
	}

	system("pause");
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值