2

My list contains a name and a campus id(CWID). How can i compare the cwid that is in my list to the integer i am passing in? I wrote the psuedo code of the comparison below of what i am trying to do.

void check_cwid(studentList& list, int cwid) {

studentNode *p = list.head_;

while(p != nullptr){

 //Something like this 
   if *p.cwid() == cwid
  //do something

  p = p->next_;

}

I am trying to accomplish what is in the code above. I just dont know how to compare specific items in my list. Here is my entire practice project below.

#include <iostream>
using namespace std;

class student {

public:

    student(const string& sname = "", int cwid = 0) : sname_(sname), cwid_(cwid) {}

    string sname() const {return sname_;}
    int cwid() const {return cwid_;}

    friend ostream& operator <<(ostream& os, student st){
        return os << endl << st.sname_ << endl << st.cwid_ << endl;
    }



private:

    string sname_;
    int cwid_;

};


struct studentNode {

    studentNode(const string& sname, int cwid, studentNode* next=nullptr) :st_(sname, cwid), next_(next) {}

    studentNode(student& st, studentNode* next=nullptr) : st_(st), next_(next) {}

    friend ostream& operator << (ostream& os, studentNode node) {
        return os << node.st_;
    }


    studentNode* next_;
    student st_;

};

struct studentList {

    studentList() : head_(nullptr), size_(0) {}

    studentNode* head_;
    size_t size_;

};


///******************** what im trying to do
void check_cwid(studentList& list, int cwid) {

    studentNode *p = list.head_;

    while(p != nullptr){




    }
}
5
  • 1
    That is a giant mountain of code and it's cluttered with vast amounts of white space. Can you clean it up and delete anything not strictly related to the problem at hand? Commented Oct 12, 2017 at 2:36
  • ok how about now Commented Oct 12, 2017 at 2:45
  • Still a lot of dead space there, plus mystery code that distracts from the core of the problem, whatever that is. Read up on how to make a good minimal, self-contained example. Commented Oct 12, 2017 at 2:46
  • can you less critique my code and help me solve it. Obviously my point is getting out there. I am new here just relax. Commented Oct 12, 2017 at 2:58
  • It's because you're new here I'm taking time to explain: Presentation is important. The way these things scroll is awful and the more code you have, the harder it is to see what's going on. Commented Oct 12, 2017 at 3:02

1 Answer 1

2
int check_cwid(studentList& list, int cwid) {

studentNode *p = list.head_;
int list_size = list.size_;
while(list_size--){

 //Something like this 
   if (p -> st_.cwid() == cwid)
  //do something
   break;
   else
       p = p -> next_;

}
Sign up to request clarification or add additional context in comments.

3 Comments

Fixed. Could't you did it by yourself? Just wrote st instead of st_.
Thank you i tried that but was getting an error for some . reason but now its working.
Thank you! for your time and help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.