c++语言有关问题 急需大神帮助
c++语言问题 急需大神帮助
//第三题 该题说明了如何对自定义类型进行输入
#include <iostream>
#include <iomanip>
using namespace std;
const int n=5;
struct student
{
int num[6];
char name[8];
int score[4];
};
struct student stu[n];
//*********************************
int main()
{
//输入
void print(student stu[]);
int i,j;
for(i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个学生的信息:"<<endl;
cout<<"学号:";
cin>>stu[i].num;
cout<<"姓名:";
cin>>stu[i].name;
for(j=0;j<3;j++)
{
cout<<"成绩"<<j+1<<":";
cin>>stu[i].score[j];
}
cout<<endl;
}
print(stu);
return 0;
}
//*********************************
void print(student stu[])
{
//打印
int i,j;
cout<<"NO. name score1 score2 score3"<<endl;
for(i=0;i<n;i++)
{
cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for(j=0;j<3;j++)
{
cout<<setw(3)<<stu[i].score[j]<<" ";
}
cout<<endl;
}
}
编译错误: 第七行:error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int [6]' (or there is no acceptable conversion)
但是如果将第七行的int num[6]改为char num[6]就不会出现错误,请问为什么num[6]必须是char类型,如何改为int 不出现错误?
------解决思路----------------------
------解决思路----------------------
这样就可以了
------解决思路----------------------
char num[6]
这个应该是学号元素,对应的不是数字,而是字符串,如001,a001,a002......
------解决思路----------------------
书上的例子很多都是为了用到某个知识点故意这么写的。作为一个栗子,能吃就好了。
//第三题 该题说明了如何对自定义类型进行输入
#include <iostream>
#include <iomanip>
using namespace std;
const int n=5;
struct student
{
int num[6];
char name[8];
int score[4];
};
struct student stu[n];
//*********************************
int main()
{
//输入
void print(student stu[]);
int i,j;
for(i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个学生的信息:"<<endl;
cout<<"学号:";
cin>>stu[i].num;
cout<<"姓名:";
cin>>stu[i].name;
for(j=0;j<3;j++)
{
cout<<"成绩"<<j+1<<":";
cin>>stu[i].score[j];
}
cout<<endl;
}
print(stu);
return 0;
}
//*********************************
void print(student stu[])
{
//打印
int i,j;
cout<<"NO. name score1 score2 score3"<<endl;
for(i=0;i<n;i++)
{
cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for(j=0;j<3;j++)
{
cout<<setw(3)<<stu[i].score[j]<<" ";
}
cout<<endl;
}
}
编译错误: 第七行:error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int [6]' (or there is no acceptable conversion)
但是如果将第七行的int num[6]改为char num[6]就不会出现错误,请问为什么num[6]必须是char类型,如何改为int 不出现错误?
------解决思路----------------------
cin>>stu[i].num;你这是想干嘛?数组很好用,也不要乱用!一个int不就OK了,非得自己给自己找麻烦……
------解决思路----------------------
#include <iostream>
#include <iomanip>
using namespace std;
const int n=5;
struct student
{
int num;
char name[8];
int score[4];
};
struct student stu[n];
//*********************************
int main()
{
//输入
void print(student stu[]);
int i,j;
for(i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个学生的信息:"<<endl;
cout<<"学号:";
cin>>stu[i].num;
cout<<"姓名:";
cin>>stu[i].name;
for(j=0;j<3;j++)
{
cout<<"成绩"<<j+1<<":";
cin>>stu[i].score[j];
}
cout<<endl;
}
print(stu);
return 0;
}
//*********************************
void print(student stu[])
{
//打印
int i,j;
cout<<"NO. name score1 score2 score3"<<endl;
for(i=0;i<n;i++)
{
cout<<stu[i].num<<" "<<setw(10)<<stu[i].name<<" ";
for(j=0;j<3;j++)
{
cout<<setw(3)<<stu[i].score[j]<<" ";
}
cout<<endl;
}
}
这样就可以了
------解决思路----------------------
char num[6]
这个应该是学号元素,对应的不是数字,而是字符串,如001,a001,a002......
------解决思路----------------------
书上的例子很多都是为了用到某个知识点故意这么写的。作为一个栗子,能吃就好了。