C++输入数据重复
题为收集学生信息,下面是我写的代码#include<iostream>#include<cstring>#include<cmath>usingnamespacestd;...
题为收集学生信息,下面是我写的代码
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
struct person
{char name[10];
int num;
int age;
float score[3],s,p;
}a[45];
main()
{
int i,k,m,n;
cout<<"请输入班级人数"<<endl;
cin>>m;
person x;
cout<<"从键盘输入具有学生的4个信息"<<endl;
for (i=0;i<m;i++)
{cin>>x.name;
cin>>x.num;
cin>>x.age;
for (n=0;n<3;n++) cin>>x.score[n];
a[i]=x;
}
cout<<"具有的信息"<<endl;
for (i=0;i<m;i++)
{cout<<a[i].name<<" ";
cout<<a[i].num;
cout<<a[i].age<<" ";
for (n=0;n<3;n++) cout<<x.score[n]<<" ";
a[i].s=x.score[0]+x.score[1]+x.score[2];
a[i].p=a[i].s/3;
cout<<a[i].s<<" "<<a[i].p<<endl;
}
}
运行后发现输出的name num age都正常,但全部score包括平均分和总分显示的都是第一个人的信息。
请问错在哪里了? 展开
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
struct person
{char name[10];
int num;
int age;
float score[3],s,p;
}a[45];
main()
{
int i,k,m,n;
cout<<"请输入班级人数"<<endl;
cin>>m;
person x;
cout<<"从键盘输入具有学生的4个信息"<<endl;
for (i=0;i<m;i++)
{cin>>x.name;
cin>>x.num;
cin>>x.age;
for (n=0;n<3;n++) cin>>x.score[n];
a[i]=x;
}
cout<<"具有的信息"<<endl;
for (i=0;i<m;i++)
{cout<<a[i].name<<" ";
cout<<a[i].num;
cout<<a[i].age<<" ";
for (n=0;n<3;n++) cout<<x.score[n]<<" ";
a[i].s=x.score[0]+x.score[1]+x.score[2];
a[i].p=a[i].s/3;
cout<<a[i].s<<" "<<a[i].p<<endl;
}
}
运行后发现输出的name num age都正常,但全部score包括平均分和总分显示的都是第一个人的信息。
请问错在哪里了? 展开
1个回答
展开全部
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
struct person
{char name[10];
int num;
int age;
float score[3],s,p;
}a[45];
main()
{
int i,k,m,n;
cout<<"请输入班级人数"<<endl;
cin>>m;
person x;
cout<<"从键盘输入具有学生的4个信息"<<endl;
for (i=0;i<m;i++)
{cin>>x.name;
cin>>x.num;
cin>>x.age;
cout<<"输入3个成绩"<<endl;
for (n=0;n<3;n++) cin>>x.score[n];
a[i]=x;
}
cout<<"具有的信息"<<endl;
for (i=0;i<m;i++)
{cout<<a[i].name<<" ";
cout<<a[i].num;
cout<<a[i].age<<" ";
for (n=0;n<3;n++) cout<<a[i].score[n]<<" ";
a[i].s+=a[i].score[n];
a[i].p=a[i].s/3;
cout<<a[i].s<<" "<<a[i].p<<endl;
}
}
#include <cstring>
#include <cmath>
using namespace std;
struct person
{char name[10];
int num;
int age;
float score[3],s,p;
}a[45];
main()
{
int i,k,m,n;
cout<<"请输入班级人数"<<endl;
cin>>m;
person x;
cout<<"从键盘输入具有学生的4个信息"<<endl;
for (i=0;i<m;i++)
{cin>>x.name;
cin>>x.num;
cin>>x.age;
cout<<"输入3个成绩"<<endl;
for (n=0;n<3;n++) cin>>x.score[n];
a[i]=x;
}
cout<<"具有的信息"<<endl;
for (i=0;i<m;i++)
{cout<<a[i].name<<" ";
cout<<a[i].num;
cout<<a[i].age<<" ";
for (n=0;n<3;n++) cout<<a[i].score[n]<<" ";
a[i].s+=a[i].score[n];
a[i].p=a[i].s/3;
cout<<a[i].s<<" "<<a[i].p<<endl;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询