C++ linked list function

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
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."<<endl; 
          countEE++;
     }
     else 
     if (percent > 60)
     {
          cout<<"Student registered at the Mechanical Engineering Program."<<endl; 
          countME++;
     }
     else 
     if (percent > 50)
     {
          cout<<"Student registered at the Business Information Program."<<endl;
          countBIS++;
     }
     else
          cout<<"Student cannot be registered."<<endl;         
     
     node *temp = new node;	
     temp->data = name;	
     temp->next=head;	
     head = temp;	
     return counter + 1;
}

int viewList()
{
     cout<<"Students statistics are: "<<endl;
     node *temp1 = new node;	
     temp1 = head;
     int counter = 0;
     if(temp1 == NULL)
     {
          cout<<endl<<"The list is empty."<<endl;
     }
     else
     {
          while( temp1!=NULL )
          {
               cout<<endl;
               cout<<"Name of student: "<<temp1 -> 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."<<endl;
          cout<<"Write 2 to display list of students registered."<<endl;
          cout<<"Write 3 to quit."<<endl;
          
          cout<<"Enter your choice: ";
          cin>>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:
1
2
3
4
5
6
7
8
9
10
11
12
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int viewList()
{
     cout<<"Students statistics are: "<<endl;
     
     int counter = 0;
     if(head == NULL)
     {
          cout<<endl<<"The list is empty."<<endl;
     }
     else
     {
          while( head!=NULL )
          {
               cout<<endl;
               cout<<"Name of student: "<<head -> 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.