samedi 21 février 2015

Cannot match string from information parsed from a file

I am trying to understand what is happening here, I made this from the program I am working on to show what is happening. The strings pulled from the text file don't match when I compare them to string. But hard coded data will.


this program should be showing:



failed again
failed again
success
success


but is does not, it fails to match the first instance of a55555555. I have found that this only happens on information I pull from the file, so I figure I must be doing something wrong during the import but I can find what. actual output:



failed again
failed again
failed again
success


the file(student.txt):



a22222222
a11111111
a55555555


the code:



#include <vector>
#include <iostream>
#include <fstream>
using namespace std;

class Student {
public:
void display(ostream& os) const;
void setId(string);
string getId();
private:
string id_;
};

void Student::setId(string id) {
id_ = id;
}

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


int main () {

vector <Student> vStudent;
int count = 0;
string line;
ifstream infile("student.txt");

while (getline(infile, line)) {
vStudent.push_back(Student());
vStudent[count].setId(line);
count++;
}

vStudent.push_back(Student());
vStudent[count].setId("a55555555");

string test = "a55555555";
for (auto & element : vStudent) {
if (test == element.getId())
cout << "success" << endl;
else
cout << "failed again" << endl;
}
}

Aucun commentaire:

Enregistrer un commentaire