为什么这个函数无法在一次运行当中进行多次输入,在进行第二次输入时会自行退出。
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void shuru(int *p1,int *p2,int *p3,long long int *x,char *xing[],int *n2,int *p,double *pi){//1
int n3=0,m;
printf("请输入您要登记的学生数目:");
scanf("%d",&m);
//system("cls");
for(n3=0;n3<m;n3++){
printf("%d\n",n3);
printf("请输入第%d位学生学号:",n3+1);
scanf("%lld",(x+*n2));
printf("\n请输入第%d位学生姓名:",n3+1);
scanf("%s",xing[*n2]);
printf("\n请输入第%d位学生语文成绩:",n3+1);
scanf("%d",(p1+*n2));
while(*(p1+*n2)>150){
if(*(p1+*n2)>150){
printf("输入错误,请重新开始\n");
printf("请输入第%d位学生语文成绩:",n3+1);
scanf("%d",(p1+*n2));
}
}
printf("\n请输入第%d位学生数学成绩:",n3+1);
scanf("%d", (p2+*n2));
while(*(p2+*n2)>150){
if(*(p2+*n2)>150){
printf("输入错误,请重新开始\n");
printf("请输入第%d位学生数学成绩:",n3+1);
scanf("%d",(p2+*n2));
}
}
printf("\n请输入第%d位学生英语成绩:",n3+1);
scanf("%d", (p3+*n2));
while(*(p3+*n2)>150){
if(*(p3+*n2)>150){
printf("输入错误,请重新开始\n");
printf("请输入第%d位学生语文成绩:",n3+1);
scanf("%d",(p3+*n2));
}
}
*(p+*n2)=*(p1+*n2)+*(p2+*n2)+*(p3+*n2);
printf("\n第%d位学生总成绩为:%d",n3+1,*(p+*n2));
*(pi+*n2) =(double)*(p+*n2)/3;
printf("\n第%d位学生平均成绩为:%f",n3+1,*(pi+*n2));
printf("\n");
printf("第%d位学生信息录入完毕!\n",*n2+1);
(*n2)++;
}//会莫名其妙地退出。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return;
}
int main(){
int number=0,n1=0,n,*n2;//*n2用来表示总数;
n2 = &number;
double pb;
double *pi;
pi = pb;
long long int *x,xuehao;
int *p,*p1,*p2,*p3,b1,b2,b3;
int sum;
char xingming;
char *xing;
x = xuehao;
p = sum;
p1 = b1;
p2 = b2;
p3 = b3;
xing = xingming;
do{
n=0;
printf("请选择您的操作:");
scanf("%d",&n);
p1 = b1;
p2 = b2;
p3 = b3;
printf("n = %d\n",n);
if(n==1){
shuru(p1,p2,p3,x,xing,n2,p,pi);
}
}while(n!=0);
return 0;
}
这段代码存在几个问题:
1. 缺少头文件
#include <stdio.h>
#include <stdlib.h>
2. 变量未初始化
在 main 函数中,有很多变量未进行初始化操作。你可以给它们分配内存并初始化。
3. 指针传参错误
在 shuru 函数中,`x`、`xing`、`n2`、`p`、`pi` 对应的都是指针变量。而在主函数中,你不需要将其赋值给其他变量,直接使用指针即可。
综上所述,你可以修改如下代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void shuru(int *p1, int *p2, int *p3, long long int *x, char *xing, int *n2, int *p, double *pi) {
int n3 = 0, m;
printf("请输入您要登记的学生数目:");
scanf("%d", &m);
for (n3 = 0; n3 < m; n3++) {
printf("请输入第%d位学生语文成绩:", n3 + 1);
scanf("%d", (p1 + *n2));
while (*(p1 + *n2) > 150) {
printf("输入错误,请重新开始\n");
printf("请输入第%d位学生语文成绩:", n3 + 1);
scanf("%d", (p1 + *n2));
}
printf("\n请输入第%d位学生数学成绩:", n3 + 1);
scanf("%d", (p2 + *n2));
while (*(p2 + *n2) > 150) {
printf("输入错误,请重新开始\n");
printf("请输入第%d位学生数学成绩:", n3 + 1);
scanf("%d", (p2 + *n2));
}
printf("\n请输入第%d位学生英语成绩:", n3 + 1);
scanf("%d", (p3 + *n2));
while (*(p3 + *n2) > 150) {
printf("输入错误,请重新开始\n");
printf("请输入第%d位学生语文成绩:", n3 + 1);
scanf("%d", (p3 + *n2));
}
*(p + *n2) = *(p1 + *n2) + *(p2 + *n2) + *(p3 + *n2);
printf("\n第%d位学生总成绩为:%d", n3 + 1, *(p + *n2));
*(pi + *n2) = (double) *(p + *n2) / 3;
printf("\n第%d位学生平均成绩为:%f", n3 + 1, *(pi + *n2));
printf("\n");
printf("第%d位学生信息录入完毕!\n", *n2 + 1);
(*n2)++;
}
return;
}
int main() {
int number = 0, *n2;
n2 = &number;
double pb, *pi;
pi = pb;
long long int xuehao, *x;
int sum, b1, b2, b3, *p, *p1, *p2, *p3;
p = sum;
p1 = b1;
p2 = b2;
p3 = b3;
char xingming, *xing;
xing = xingming;
int n;
do {
printf("请选择您的操作:");
scanf("%d", &n);
if (n == 1) {
shuru(p1, p2, p3, x, xing, n2, p, pi);
}
} while (n != 0);
return 0;
}
现在,你可以在一次运行时进行多次输入了。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]