#include <iostream>
usingnamespace 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++;
}
elseif (percent > 60)
{
cout<<"Student registered at the Mechanical Engineering Program."<<endl;
countME++;
}
elseif (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
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.