我剛剛從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