0

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

Asked for more of the code so here is the entire program: (issue in displayID)

header file

#ifndef structures_h
    #define structures_h
    #include <vector>
    #include <iostream>
    #include <stdlib.h> 
    #include <stdio.h>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <algorithm>
    #include <stdio.h>
    #include <map>

    using namespace std;

main program

#endif
typedef pair<string, string> Name; // first name, last name
typedef map<string, int> Grades; // map courses to scores


#include "struct.h"

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_;
};

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

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

void Student::setName(string first, string last) {
    name_ = pair<string,string>(first, last);
}

string Student::getName() {
    return get<0>(name_) + ' ' + get<1>(name_);
}

void Student::setGrades(string course, int score) {
    grades_.insert(make_pair(course, score));
}

void Student::getGrades() {

    for(auto it = grades_.begin(); it != grades_.end(); ++it) {
        cout << it -> first << ' ' <<  it -> second << endl;
    }
}




vector<Student> addStudent(int count, int x, vector<Student>& vStu, string file) {
    string line, first, last;

    ifstream infile(file);

    while (getline(infile, line)) {

        vStu.push_back(Student());
        vStu[count].setId(line);
        getline(infile, line);
        istringstream iss(line);

        if (!(iss >> first >> last)) {
            cout << "failed to get name" << endl;
            break;
        }
        vStu[count].setName(first, last);

        getline(infile, line);
        istringstream iss2(line);
        if (!(iss2 >> x)) {
            cout << "failed to get class number" << endl;
            break;
        }

        for (int i = 0; i < x; i++) {
            string sClass;
            int grade;

            getline(infile, line);
            istringstream iss3(line);

            if (!(iss3 >> sClass >> grade)) {
                cout << "failed to get class and grade" << endl;
                break;
            }

            vStu[count].setGrades(sClass, grade);
        }
        count++;
    }
    return vStu;
}

void display(vector<Student>& vStu) {
    sort(vStu.begin(), vStu.end());
    cout << endl;
    int count = vStu.size();
    for (int i = 0; i<count;i++) {
        cout << vStu[i].getId() << endl;
        cout << vStu[i].getName() << endl;
        vStu[i].getGrades();
        cout << endl;
    }   
}

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;
}


void mainMenu(vector<Student>& vStu) {
    string input;
    string word;
    vector<string> com;

    while(1) {
        cout << "Enter command: ";
        getline(cin,input);

        istringstream iss(input);

        while(iss >> word) {
            com.push_back(word);
        }
        for (int i = 0; i < (int)com.size(); i++) {
            transform(com[i].begin(), com[i].end(), com[i].begin(), ::tolower);
            if (com[i] == "show") {
                display(vStu);

            } else if (com[i] == "showid") {
                displayID(vStu, "a11111111");

            }

        }

    com.clear();
    }
}

int main(int argc, char *argv[]) {
    vector<Student> vStu;
    int count = 0, x = 0;


    if (argc != 2) {
        cout << "Incorrectly called" << endl;
        cout << "    "  << argv[0] << ' ' << "<filename>" << endl; 
        return 1;
    }

    addStudent(count, x, vStu, argv[1]);
    mainMenu(vStu);


}
ZeroScifer
  • 53
  • 8
  • Since we can't see how the data gets into your class we can only guess. Is there also whitespace in the string in the class? Print/compare the lengths. – Retired Ninja Feb 21 '15 at 01:05
  • You need to show some more code, how are you filling the vector, how are you passing it to function, etc – P0W Feb 21 '15 at 01:06
  • ok added the entire thing, the code having issues is in the displayID method – ZeroScifer Feb 21 '15 at 01:19

1 Answers1

0

The only possibility I see is that there is some whitespace at the end of the string that gets passed into your function. Try trimming the end of the string's like this this thread suggests before comparing and see if they still don't compare correctly.

Community
  • 1
  • 1
vdelricco
  • 719
  • 4
  • 15
  • Huh. Have you tried other comparison techniques? Like `strcmp` or just straight up `==`? – vdelricco Feb 21 '15 at 01:19
  • Also maybe try `strncmp` and use `n` as the length of `ID` since you explicitly set that string. – vdelricco Feb 21 '15 at 01:22
  • actually getting this error on strcmp cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’ int comp = strcmp(ID, test); – ZeroScifer Feb 21 '15 at 01:32
  • Hmm, you may have to use `string.c_str()` to convert to a C-string. – vdelricco Feb 21 '15 at 01:34