为什么我的 fflush 不起作用?跳过循环请求字符串
Why does my fflush not work? Skips asking for string in loop
我正在尝试为 class 编写一个实验室代码,但是在我 运行 时的输出中它只询问一次字符串,当它循环时它跳过让我输入它并继续到下一个功能。我在那里放了一个 fflush
但它不起作用。我正在使用 visual studio 2015
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define size 3
struct empsal
{
char first[15];
int t1, t2, t3;
float avg;
char grade;
};
void load(struct empsal s[], int n)
{
int i;
for (i = 0; i<n; i++)
{
printf("Enter first name ");
gets_s(s[i].first);
printf("Enter 3 test scores ");
scanf("%d%d%d", &s[i].t1, &s[i].t2, &s[i].t3);
s[i].avg = (s[i].t1 + s[i].t2 + s[i].t3) / (float)3;
if (s[i].avg>70)
s[i].grade = 'p';
if (s[i].avg<70)
s[i].grade = 'f';
fflush(stdin);
}
}
void print(struct empsal s[], int n)
{
int i;
for (i = 0; i<n; i++)
{
printf("%s\n", s[i].first);
printf("%d %d %d\n", s[i].t1, s[i].t2, s[i].t3);
printf("Average is %f Grade is %c\n\n", s[i].avg, s[i].grade);
}
}
//sort
void sort(struct empsal s[], int n)
{
int i, j;
empsal t;
for (i = 0; i<n - 1; i++)
{
for (j = 0; j<n - 1; j++)
{
t = s[j];
s[j] = s[j + 1];
s[j + 1] = t;
}
}
}
//amount passed
void passed(struct empsal s[], int n)
{
int count = 0;
int i;
for (i = 0; i<n; i++)
{
if (s[i].grade = 'p')
count++;
}
printf("Number of passes %d\n\n", count);
}
void main()
{
empsal s[size];//array
load(s, size);
sort(s, size);
passed(s, size);
print(s, size);
}
输出:
输入名字阿马尔
输入 3 考试成绩 90
90后
90后
输入名字 输入 3 个考试成绩 80
90后
100
输入名字 输入 3 个测试分数 90
60
80
传球次数 3
90 60 80
平均值为 76.666664 等级为 p
阿马尔
90 90 90
平均值为 90.000000 等级为 p
80 90 100
平均值为 90.000000 等级为 p
按任意键继续。 . .
根据标准定义 fflush(stdin);
调用 undefined behavior.
引用 C11
,第 7.21.5.2 章(强调我的)
If stream
points to an output stream or an update stream in which the most recent
operation was not input, the fflush
function causes any unwritten data for that stream
to be delivered to the host environment to be written to the file; otherwise, the behavior is
undefined.
如果您想清除剩余输入(和换行符)的输入缓冲区,请自己动手。一个简单的方法是
while (getchar() != '\n');
我正在尝试为 class 编写一个实验室代码,但是在我 运行 时的输出中它只询问一次字符串,当它循环时它跳过让我输入它并继续到下一个功能。我在那里放了一个 fflush
但它不起作用。我正在使用 visual studio 2015
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define size 3
struct empsal
{
char first[15];
int t1, t2, t3;
float avg;
char grade;
};
void load(struct empsal s[], int n)
{
int i;
for (i = 0; i<n; i++)
{
printf("Enter first name ");
gets_s(s[i].first);
printf("Enter 3 test scores ");
scanf("%d%d%d", &s[i].t1, &s[i].t2, &s[i].t3);
s[i].avg = (s[i].t1 + s[i].t2 + s[i].t3) / (float)3;
if (s[i].avg>70)
s[i].grade = 'p';
if (s[i].avg<70)
s[i].grade = 'f';
fflush(stdin);
}
}
void print(struct empsal s[], int n)
{
int i;
for (i = 0; i<n; i++)
{
printf("%s\n", s[i].first);
printf("%d %d %d\n", s[i].t1, s[i].t2, s[i].t3);
printf("Average is %f Grade is %c\n\n", s[i].avg, s[i].grade);
}
}
//sort
void sort(struct empsal s[], int n)
{
int i, j;
empsal t;
for (i = 0; i<n - 1; i++)
{
for (j = 0; j<n - 1; j++)
{
t = s[j];
s[j] = s[j + 1];
s[j + 1] = t;
}
}
}
//amount passed
void passed(struct empsal s[], int n)
{
int count = 0;
int i;
for (i = 0; i<n; i++)
{
if (s[i].grade = 'p')
count++;
}
printf("Number of passes %d\n\n", count);
}
void main()
{
empsal s[size];//array
load(s, size);
sort(s, size);
passed(s, size);
print(s, size);
}
输出:
输入名字阿马尔 输入 3 考试成绩 90 90后 90后 输入名字 输入 3 个考试成绩 80 90后 100 输入名字 输入 3 个测试分数 90 60 80 传球次数 3
90 60 80 平均值为 76.666664 等级为 p
阿马尔 90 90 90 平均值为 90.000000 等级为 p
80 90 100 平均值为 90.000000 等级为 p
按任意键继续。 . .
根据标准定义 fflush(stdin);
调用 undefined behavior.
引用 C11
,第 7.21.5.2 章(强调我的)
If
stream
points to an output stream or an update stream in which the most recent operation was not input, thefflush
function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
如果您想清除剩余输入(和换行符)的输入缓冲区,请自己动手。一个简单的方法是
while (getchar() != '\n');