当前位置:编程学习 > C/C++ >>

请教达人--c++中读取字符串并赋值给数组

我这样的赋值该怎么修改。代码如下
#include <string>
#include <iostream>
using namespace std;

int main()
{
char stuname[5];
int Chinese[5];
int English[5];
int Math[5];
char a[20];
int sumC=0, sumE=0,sumM=0;
cout << "成绩管理系统1.0\
";
cout << "###############\
";
for (int n=0; n<5; n++) {
cout << "请输入第"<<n+1<<"个学生姓名:" << endl;
cin >> a;
strcpy (stuname,a);
cout << "请输入语文成绩:" << endl;
cin >> Chinese[n];
cout << "请输入英语成绩:" << endl;
cin >> English[n];
cout << "请输入数学成绩:" << endl;

cin >> Math[n];

sumC = sumC+Chinese[n];
sumE = sumE+English[n];
sumM = sumM+Math[n];

}

for (int i=0;i<5;i++){
cout << stuname[i] << "的语文成绩是:"<< Chinese[i];

cout << "\
英语成绩是:"<< English[i] << "\
数学成绩是:"<< Math[i] << endl;
}
cout << "\
语文平均成绩是:" << sumC / 5;
cout << "\
英语平均成绩是:" << sumE / 5;
cout << "\
数学平均成绩是:" << sumM / 5 << endl;
//cin.get();
return 0;
}
答案:我把你的代码给改了,建议你用struct活着class


#include <string>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string stuname[5];
int Chinese[5];
int English[5];
int Math[5];
string a;
int sumC=0, sumE=0,sumM=0;
cout << "成绩管理系统1.0\
";
cout << "###############\
";
for (int n=0; n<5; n++)
{
cout<< "请输入第"<<n+1<<"个学生姓名:" << endl;
cin>>a;
//strcpy (stuname,a);
stuname[n]=a;
cout << "请输入语文成绩:" << endl;
cin >> Chinese[n];
cout << "请输入英语成绩:" << endl;
cin >> English[n];
cout << "请输入数学成绩:" << endl;
cin >> Math[n];

sumC = sumC+Chinese[n];
sumE = sumE+English[n];
sumM = sumM+Math[n];

}

for (int i=0;i<5;i++)
cout<<stuname[i]<< "\
语文:"<< Chinese[i]<<"\
英语:"<< English[i] <<"\
数学:"<<Math[i]<< endl;
cout << "\
语文平均成绩是:" << sumC/5;
cout << "\
英语平均成绩是:" << sumE/5;
cout << "\
数学平均成绩是:" << sumM/5 << endl;
return 0;
}

上一个:C++语言 里面的英语是初中还是 大学什么的
下一个:C++比C#难在指针,该怎么学呢?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,