在控制臺輸出程式時出錯,輸入十個學生的資料時第一行輸入完準備輸入下一行,但一按回車程式自己結束了。求求了幫忙解決一下吧


uj5u.com熱心網友回復:
只能一行輸入uj5u.com熱心網友回復:
( ?????)我裂開來,這個代碼是應該分行輸入十個學生資訊的,結果輸入了一個按回車就結束了
uj5u.com熱心網友回復:
你的源代碼呢?uj5u.com熱心網友回復:
scanf是female的,你的輸入和它的需求不匹配,它生氣了。uj5u.com熱心網友回復:
好家伙,不愧是你
uj5u.com熱心網友回復:
#include<stdio.h>
#define N 10//學生數為10
struct Student//建立結構體型別struct Student
{
int num;//學號
char name[20];//成績
float score[3];//3門課成績
float aver;//平均成績
};
int main()
{
void input(struct Student stu[]);//函式宣告
struct Student max(struct Student stu[]);//函式宣告
void print(struct Student stu);//函式宣告
struct Student stu[N], * p = stu;//定義結構體陣列和指標
input(p);//呼叫input函式
print(max(p));//呼叫print函式,以max函式的回傳值作為實參
return 0;
}
void input(struct Student stu[])//定義input函式
{
int i;
printf("請輸入各學生的資訊:學號、姓名、3門課成績:\n");
for (i = 0; i < N; i++)
{
scanf_s("%d %s %f %f %f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);//輸入資料
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;//求平均成績
}
}
struct Student max(struct Student stu[])//定義max函式
{
int i, m = 0;//用m存放成績最高的學生在陣列中的序號
for (i = 0; i < N; i++)
if (stu[i].aver > stu[m].aver)//找出最高成績學生在陣列中的序號
m = i;
return stu[m];//回傳包含該生資訊的結構體元素
}
void print(struct Student stud)//定義print函式
{
uj5u.com熱心網友回復:
這是代碼#include<stdio.h>
#define N 10//學生數為10
struct Student//建立結構體型別struct Student
{
int num;//學號
char name[20];//成績
float score[3];//3門課成績
float aver;//平均成績
};
int main()
{
void input(struct Student stu[]);//函式宣告
struct Student max(struct Student stu[]);//函式宣告
void print(struct Student stu);//函式宣告
struct Student stu[N], * p = stu;//定義結構體陣列和指標
input(p);//呼叫input函式
print(max(p));//呼叫print函式,以max函式的回傳值作為實參
return 0;
}
void input(struct Student stu[])//定義input函式
{
int i;
printf("請輸入各學生的資訊:學號、姓名、3門課成績:\n");
for (i = 0; i < N; i++)
{
scanf_s("%d %s %f %f %f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);//輸入資料
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;//求平均成績
}
}
struct Student max(struct Student stu[])//定義max函式
{
int i, m = 0;//用m存放成績最高的學生在陣列中的序號
for (i = 0; i < N; i++)
if (stu[i].aver > stu[m].aver)//找出最高成績學生在陣列中的序號
m = i;
return stu[m];//回傳包含該生資訊的結構體元素
}
void print(struct Student stud)//定義print函式
{
printf("\n成績最高的學生是:\n");
printf("學號:%d\n姓名:%s\n三門課成績:%5.1f,%5.1f,%5.1f\n平均成績:%6.2f\n", stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2],stud.aver);
}
uj5u.com熱心網友回復:
就光說輸入的問題哈scanf_s("%d %s %f %f %f", &stu[i].num, stu[i].name, 19,&stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
刪了一個&,你懂的。
加了一個19,這是后綴_s的scanf_s的額外的小脾氣。
uj5u.com熱心網友回復:
去搜下scanf_s()函式的用法。這里在輸入函式改了下,沒毛病,供參考:
void input(struct Student stu[])//定義input函式
{
int i;
printf("請輸入各學生的資訊:學號、姓名、3門課成績:\n");
for (i = 0; i < N; i++)
{
scanf("%d %s %f %f %f",
&stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);//輸入資料
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2])/3.0;//求平均成績
}
}
運行效果如下,選了3個輸入:
uj5u.com熱心網友回復:
把scanf_s括號里不要有空格。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/244069.html
標籤:新手樂園
上一篇:關于設計俄羅斯方塊
下一篇:if用法
