永发信息网

高分:我这段C++代码有问题 请指点 之后必加分

答案:2  悬赏:50  手机版
解决时间 2021-03-12 08:25
  • 提问者网友:送舟行
  • 2021-03-12 03:02
高分:我这段C++代码有问题 请指点 之后必加分

这段代码目的是输入姓名、学号、成绩,之后还可以查询,这样的功能。代码和调试信息如下,请帮忙解决所有提示错误。另外,main函数的编写也许有点“词不达意”,也请不吝赐教。之后必加分。

#include <iostream>
#include <vector>
#include <list>
#include <string>

using namespace std;

struct Sutdent
{
unsigned int number;
string name;
};

struct Score
{
unsigned int number;
float mark;
};

class Manager
{
public:
void InputStudents();
void InputScores();
void OutputScores() const;
private:
vector<Student> students;
list<Score> scores;
};

void Manager::InputStudents()
{
int number=1;

while(true)
{
cout<<"输入"<<number<<"号姓名(x表示结束)"<<endl;
string name;

getline(cin,name);

if(name=="x")
{
break;
}

Student student;
student.number=number;
student.name=name;

student.push_back(student);
++number;
}
}

void Manager::InputScores()
{
while(true)
{
unsigned int number;
cout<<"请输入学号(0表示结束)";
cin>>number;

if(number==0)
{
break;
}

//简单判断学号是否正确

if(number>students.size())
{
cout<<"学号输入错误,数字必须在1~"<<students.size()<<"之间。"<<endl;
continue;
}

float mark;
cout<<"输入分数:";
cin>>mark;

Score score;
score.number=number;
score.mark=mark;

scores.push_front(score);
}
}

void Manager::OutputScores() const
{
for(unsigned int i=0;i<students.size();++i)
{
cout<<"学号 姓名"<<students[i].number<<" "<<students[i].name<<endl;

bool found=false;

for(list<Score>::const iterator iter=scores.begin();iter!=scores.end();++iter)
{
if((*iter).number==number)
{
found=true;
cout<<"成绩"<<(*iter).mark<<endl;
break;
}

}

if(found=false)
{
cout<<"缺考"<<endl;
}
}

}

int main()
{
Manager M;
M.InputStudents();
M.InputScores();
M.OutputScores();
return 0;
}

调试信息:见补充
27| `Student' was not declared in this scope|
27| template argument 1 is invalid|
27| template argument 2 is invalid|
27| ISO C++ forbids declaration of `students' with no type|
47| `Student' was not declared in this scope|
47| expected `;' before "student"|
48| `student' was not declared in this scope|
71| request for member `size' in `((Manager*)this)->Manager::students', which is of non-class type `int'|
73| request for member `size' in `((Manager*)this)->Manager::students', which is of non-class type `int'|
91| request for member `size' in `((const Manager*)this)->Manager::students', which is of non-class type `const int'|
93| invalid types `const int[unsigned int]' for array subscript|
93| invalid types `const int[unsigned int]' for array subscript|
97| expected unqualified-id before "const"|
97| expected `;' before "const"|
97| `iter' was not declared in this scope|
99| `number' was not declared in this scope|
最佳答案
  • 五星知识达人网友:不甚了了
  • 2021-03-12 04:34
先改了语法错误 词不达意的还没动~
#include <iostream>
#include <vector>
#include <list>
#include <string>

using namespace std;

struct Student
{
unsigned int number;
string name;
};

struct Score
{
unsigned int number;
float mark;
};

class Manager
{
public:
void InputStudents();
void InputScores();
void OutputScores() const;
private:
vector<Student> students;
list<Score> scores;
};

void Manager::InputStudents()
{
int number=1;

while(true)
{
cout<<"输入"<<number<<"号姓名(x表示结束)"<<endl;
string name;

getline(cin,name);

if(name=="x")
{
break;
}

Student student;
student.number=number;
student.name=name;

students.push_back(student);
++number;
}
}

void Manager::InputScores()
{
while(true)
{
unsigned int number;
cout<<"请输入学号(0表示结束)";
cin>>number;

if(number==0)
{
break;
}

//简单判断学号是否正确

if(number>students.size())
{
cout<<"学号输入错误,数字必须在1~"<<students.size()<<"之间。"<<endl;
continue;
}

float mark;
cout<<"输入分数:";
cin>>mark;

Score score;
score.number=number;
score.mark=mark;

scores.push_front(score);
}
}

void Manager::OutputScores() const
{
for(unsigned int i=0;i<students.size();++i)
{
cout<<"学号 姓名"<<students[i].number<<" "<<students[i].name<<endl;

bool found=false;

for(list<Score>::const_iterator iter=scores.begin();iter!=scores.end();++iter)
{
if((*iter).number==students[i].number)
{
found=true;
cout<<"成绩"<<(*iter).mark<<endl;
break;
}

}

if(found=false)
{
cout<<"缺考"<<endl;
}
}

}

int main()
{
Manager M;
M.InputStudents();
M.InputScores();
M.OutputScores();
return 0;
}
全部回答
  • 1楼网友:不甚了了
  • 2021-03-12 05:54
你好! 我教你 打字不易,采纳哦!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯