Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd item to list issue
    text
    copied!<p>I am creating a simple problem. I try to add students to one list then split it in two lists. I created a general method that adds a node to a list but it doesn't seem to function well. This is what I tried. What I am doing wrong?</p> <pre><code>int main() { for(int i=0;i&lt;NR_STUDENTS;i++) { Student *studToAdd=new Student; cout&lt;&lt;"Student #"&lt;&lt;i&lt;&lt;endl; cout&lt;&lt;"Name:"; cin&gt;&gt;studToAdd-&gt;name; cout&lt;&lt;"Grade:"; cin&gt;&gt;studToAdd-&gt;grade; Add(studToAdd); } Split(); Print(); } #include "students.h" Node *allFirstNode; Node *admitedFirstNode; Node * rejectedFirstNode; void AddNodeToList(Node *firstNode, Student *studToAdd) { Node *nodeToAdd=new Node; nodeToAdd-&gt;student=studToAdd; nodeToAdd-&gt;next=NULL; if(firstNode==NULL) { firstNode=nodeToAdd; } else { Node *temp; temp=firstNode; while(temp-&gt;next != NULL) { temp=temp-&gt;next; } temp-&gt;next=nodeToAdd; } } void addStudent(Student *studentToAdd) { AddNodeToList(allFirstNode,studentToAdd); } void split() { Node *temp=allFirstNode; while(temp-&gt;next != NULL) { Student *currentStud=temp-&gt;student; if(currentStud-&gt;grade &lt; GR) { AddNodeToList(rejectedFirstNode,currentStud); } else { AddNodeToList(admitedFirstNode,currentStud); } } } void PrintList(Node *first) { Node *temp=first; while(temp!=0) { cout&lt;&lt;temp-&gt;student-&gt;name&lt;&lt;":"&lt;&lt;temp-&gt;student-&gt;grade&lt;&lt;endl; temp=temp-&gt;next; } } void Print() { PrintList(admitedFirstNode); PrintList(rejectedFirstNode); } #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; const int NR_STUDENTS=2; const double GR=5.0; struct Student { string name; double grade; }; struct Node { Student *student; Node *next; }; extern Node *allFirstNode; extern Node *admitedFirstNode; extern Node * rejectedFirstNode; void addStudent(Student *studentToAdd); void split(); void sort(); void print(Node *firsNode); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload