vendredi 20 février 2015

.compare not matching a string pulled from an object

I am trying to go threw a vector of Student objects. If I find a matching ID to the one I am searching for it will display their info.


However, when I try to find a specific ID .compare isn't seeing a match even though it should.


My output: first line is the ID I am looking for, second is the current ID being looked at, then is the result of the compare.



a11111111
a22222222
-1
no match

a11111111
a11111111
-1
no match


The code being run at this point:



void displayID(vector<Student>& vStu, string ID) {
int count = vStu.size();
string test;
ID = "a11111111";


for (int i = 0; i<count;i++) {
cout<< endl;

test = vStu[i].getId();
cout << ID << endl;
cout << test << endl;
cout << ID.compare(test) << endl;

if (ID.compare(test) == 0) {
cout << "match" << endl;
cout << vStu[i].getId() << endl;
cout << vStu[i].getName() << endl;
vStu[i].getGrades();
cout << endl;
} else {
cout << "no match" << endl;
}
}
cout << endl;
}


Incase it will help, my student class:



class Student {
public:
void display(ostream& os) const;
void setId(string);
void setName(string, string);
void setGrades(string, int);
string getId();
string getName();
void getGrades();
bool operator<(const Student &rhs) const { return id_ < rhs.id_;}
private:
string id_; // e.g. "a22222222"
Name name_; // e.g. {"ned", "flanders"}
Grades grades_;
};

string Student::getId() {
return id_;
}

Aucun commentaire:

Enregistrer un commentaire