I've made a C++ program to register student's program based on their percentage in 3 subject using linked list.

Here is the code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121`` ``````#include using namespace std; string info; int node_number = 0; struct node { string data; node *next; }; node *head = NULL; int countEE=0,countME=0,countBIS=0; int enterStudent(int counter) { string name; double percent,physics,percentPhysics,maths,percentMaths,program,percentProgram; cout<<"Write name of student: "; cin>>name; cout<<"Enter Physics Result: "; cin>>physics; cout<<"Enter Mathematics Result: "; cin>>maths; cout<<"Enter Programming Result: "; cin>>program; percentPhysics = (physics/100)*40; percentMaths = (maths/100)*40; percentprogram = (entry/100)*20; percent = percentPhysics + percentMaths + percentProgram; if (percent > 70) { cout<<"Student registered at the Electrical & Electronic Engineering Program."< 60) { cout<<"Student registered at the Mechanical Engineering Program."< 50) { cout<<"Student registered at the Business Information Program."<data = name; temp->next=head; head = temp; return counter + 1; } int viewList() { cout<<"Students statistics are: "< data; temp1 = temp1 -> next; } } return counter; } int main() { char ch; int counter = 0; do{ cout<<"\n\n"; cout<<"Write 1 to register a new student."<>ch; switch(ch) { case '1':{ counter = enterStudent(counter); break; } case '2':{ counter = viewList(); break; } case '3': break; } }while(ch!='3'); return 0; }``````

When I choose 1, it works fine but how do I modify the code so that whenever I view the list of student, it displays the number of student in a program and the program that the student is registered to?

Expected output:
 ``123456789101112`` ``````Press 1 to register a student Press 2 to view the student's details Press 3 to quit Choice: 2 Name of student: Ali Total student: 3 //in a program Student registered in Electrical & Electronic Engineering Name of student: John Total student: 4 Student registered in Business Information Program ``````

Thanks
Last edited on
Why do you need to create a node when viewing the list?
bcoz i have to use linked list to do this program.
That's irrelevant; why do you have to create a node when viewing the list?
I guess the new node for viewing the list is not necessary.Here it is without a new node.

 ``123456789101112131415161718192021`` ``````int viewList() { cout<<"Students statistics are: "< data; head = head -> next; } } return counter; }``````
Your use of the temporary variable was correct, you just didn't need to have the "new node;" part. In this code, though, you overwrite head, which means after viewing the list you have lost track of where the beginning is so you basically leaked memory and lost your list.
 how do I modify the code so that whenever I view the list of student, it displays the number of student in a program
You need to store the Program somewhere. You can either add Program as a record to the Student linked list, or create a seperate linked list of Programs and add a pointer to one of these entries for each student.

Depending on how you implement it determines how you'd get the counts and so on.
Topic archived. No new replies allowed.