我刚刚从C到C++并发现比较字符串方法的困难。 我有一个简单的任务。我需要创建一类学校教师,然后创建一个对象数组,然后在所有老师之后,谁的主题与testSubject类似。probmem与比较字符串C++
因此,这里是我的课
class Teacher
{
private:
string FamilyName;
string Name;
string Patronymic;
string sex;
int exp;
std::string subject;
string speciality;
int age;
public:
Teacher();
int getExp();
string getSubject();
int getAge();
void show();
};
这里是我的FUNC,即withdrow教师的名单,教学输入主题
void ListTeacherSub (Teacher spis[], int n)
{
//List of teachers, who's subject is like testSubject
std::string testSubject;
cout<<"Enter test subject "; cin>>testSubject;
for (int i=0; i<n; i++)
{
if (spis[n].getSubject().compare(testSubject) == 0)
spis[i].show();
}
}
这是main()函数
int main()
{
Teacher *spis;
int n;
cout<<"Enter numbers of student "; cin>>n;
spis = new Teacher[n];
for (int i=0; i<n; i++)
{
spis[i].show();
}
ListTeacherAge(spis, n);
ListTeacherEx(spis, n);
ListTeacherSub(spis, n);
delete[] spis;
return 0;
}
所以,一切工作都很好,但是当程序到达ListTeacherSub(spis,n)时它停止工作。据我所知,我以前只用strcmp工作,但它不能用于字符串。 因此,我决定寻找不同的认识,并发现一个http://www.cplusplus.com/reference/string/string/compare/
我该如何解决我的问题?
“它停止工作” - 这是什么意思? – 2014-11-05 15:47:44