C 为什么我的程序在while循环后终止?
下面的程序应该询问用户他想要给多少学生打分,然后让用户输入分数并得到平均值C 为什么我的程序在while循环后终止?,c,while-loop,C,While Loop,下面的程序应该询问用户他想要给多少学生打分,然后让用户输入分数并得到平均值 #include <stdio.h> #include <stdlib.h> struct person { char Name[100]; int ID; int Age; double Score; }; int main(void) { setvbuf(stdout, NULL, _IONBF, 0); int size; pr
#include <stdio.h>
#include <stdlib.h>
struct person {
char Name[100];
int ID;
int Age;
double Score;
};
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
int size;
printf("How many students are you grading?(Not more than 100)-->");
scanf("%d",&size);
struct person *student;
double total=0,i=0, average;
student= malloc(sizeof(struct person));
//struct person student;
printf("Enter the following information:\n");
while (i<size) {
printf("%lf\n%lf\n",total,i);
printf("\n");
printf("Name:");
scanf("%s",student->Name);
printf("ID:");
scanf("%d",&student->ID);
printf("Age:");
scanf("%d",&student->Age);
printf("Score:");
scanf("%lf",&student->Score);
total = total + student->Score;
i++;
}
我认为你遇到的问题是,在你可以读取平均值之前,控制台已经关闭
#include <stdio.h>
#include <stdlib.h>
struct person {
char Name[100];
int ID;
int Age;
double Score;
};
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
int size;
printf("How many students are you grading?(Not more than 100)-->");
scanf("%d",&size);
struct person *student;
double total=0,i=0, average;
student= malloc(sizeof(struct person));
//struct person student;
printf("Enter the following information:\n");
while (i<size) {
printf("%lf\n%lf\n",total,i);
printf("\n");
printf("Name:");
scanf("%s",student->Name);
printf("ID:");
scanf("%d",&student->ID);
printf("Age:");
scanf("%d",&student->Age);
printf("Score:");
scanf("%lf",&student->Score);
total = total + student->Score;
i++;
}
你可以加上:
getchar()
在返回0之前;在int main中。我认为您遇到的问题是,在您可以读取平均值之前,控制台正在关闭
#include <stdio.h>
#include <stdlib.h>
struct person {
char Name[100];
int ID;
int Age;
double Score;
};
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
int size;
printf("How many students are you grading?(Not more than 100)-->");
scanf("%d",&size);
struct person *student;
double total=0,i=0, average;
student= malloc(sizeof(struct person));
//struct person student;
printf("Enter the following information:\n");
while (i<size) {
printf("%lf\n%lf\n",total,i);
printf("\n");
printf("Name:");
scanf("%s",student->Name);
printf("ID:");
scanf("%d",&student->ID);
printf("Age:");
scanf("%d",&student->Age);
printf("Score:");
scanf("%lf",&student->Score);
total = total + student->Score;
i++;
}
你可以加上:
getchar()
在返回0之前;在int main中。大多数控制台程序都打算从某种控制台主机(如windows cmd)上运行,在该主机上,输出保持不变,用户需要做一些额外的工作来结束程序,以便运行另一个命令,这会很烦人,但由于您只是在测试它,您应该在末尾放置一些代码以等待用户输入,现在有很多方法可以做到这一点,如:getch()、getchar()、scanf(“%*c”)、system(“pause”),因此您可以尝试这些方法,但有些编译器会在调试模式下自动执行此操作,有时,您只需要在IDE设置中指定行为。大多数控制台程序都打算从某种控制台主机(如windows cmd)上运行,在该主机上,输出会停留在那里,用户需要做一些额外的工作来结束程序,以便可以运行另一个命令,这会很烦人,但由于您只是在测试它,您应该在最后放一些代码以等待用户输入,现在有很多方法可以做到这一点,比如:getch()、getchar()、scanf(“%*c”)、system(“pause”),所以您可以尝试这些方法,但有些编译器会在调试模式下自动执行此操作,有时您只需要在IDE设置中指定行为。。请确保您正在编译和运行最新的预期代码,并请发布。您使用的是什么环境?它可能正在打印结果,然后立即退出。尝试从命令提示符或终端软件运行代码。。请确保您正在编译和运行最新的预期代码,并请发布。您使用的是什么环境?它可能正在打印结果,然后立即退出。尝试从命令提示符或终端软件运行代码。您是对的。有趣的是,我已经有了在code::block中使用system(“pause”)的习惯,但出于某种原因,我没有想到这就是这里的问题所在。我需要多加注意,你是对的。有趣的是,我已经有了在code::block中使用system(“pause”)的习惯,但出于某种原因,我没有想到这就是这里的问题所在。我需要多加注意,这可能还不够。在最后一次.
scanf(“%lf”,…)
之后,换行符可能会保留在输入流的缓冲区中,然后getchar()
可能会使用它并立即返回。这可能不够。在最后一次.scanf(“%lf”,…)
之后,换行符可能会保留在输入流的缓冲区中,然后getchar()
可能会使用它并立即返回。