How Modify Data In a Linked List

This is a discussion on How Modify Data In a Linked List within the C++ Programming forums, part of the General Programming Boards category; I have made a Student record system in c++ using the concept of linked list. The program will take the ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    How Modify Data In a Linked List

    I have made a Student record system in c++ using the concept of linked list. The program will take the student id, name and marks for 3 different exams. Then it will calculate the aggregate percentage based on those exams marks and display the relevant message according to the aggregate percentage. All is going well.
    But there is only one problem. When I modify the student record especially the marks, it doesn't change the aggregate percentage of that specific record. It shows the old one. Similarly the relevant message doesn't change too. Any help will be appreciated.

    Code:
    struct student
    {
        int id;
        char name[MAX];
        string status;
        double aggr;
        int matric_marks, inter_marks, entryTest_marks;
    
    
    };
    
    
    struct node
    {
        struct student *data;
        struct node *next;
    
    
        node()                  
        {                       
         data = 0;              
         next = NULL;            
        }    
    };
    
    
    
    
    struct student *readStudent()
    {
        clearWindow();
        struct student *stdnt = new student;
        gotoxy(33,8);cout<<"Student ID: ";
        while(!(cin >> stdnt->id) || cin.peek() != '\n')
        {
             char ch;
             cin.clear();
             cout << "sorry";
             while(cin.get(ch) && ch != '\n');
        }
        cin.ignore();
        gotoxy(33,10);cout<<"Student Name: ";
        cin.getline(stdnt->name, 50);
        gotoxy(33,12);cout<<"Enter Matriculation Marks: ";
        cin>>(stdnt->matric_marks);
    
    
        gotoxy(33,14);cout<<"Enter Intermediate Marks: ";
        cin>>(stdnt->inter_marks);
    
    
        gotoxy(33,16);cout<<"Enter Entry Test Marks: ";
        cin>>(stdnt->entryTest_marks);
        stdnt->aggr = calculate_aggregiate(stdnt);
        gotoxy(33,18);cout<<"Student Aggregate Marks: "<< stdnt->aggr;
        if (stdnt->aggr >= 70)
        {
            gotoxy(33,20);cout<<"Student Registered In Electrical Engg";
            
        }
        else if (stdnt->aggr >= 60)
        {
            gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";
            
        }
        else if (stdnt->aggr >=50)
        {
            gotoxy(33,24);cout<<"Student Registered In Computer Science";
            
        }
        else
        {
            gotoxy(33,20);cout<<"Sorry! The Student Doesnt Qualify";
            
        }
        
        return stdnt;
    }
    
    
    struct node *createDatabase(int size)
    {
        int i = 0;
        
        struct node *head = NULL;
        struct node *last = NULL;
    
    
        for(i = 0; i < size; i++)
        {
            struct student *stdnt = readStudent();
    
    
            struct node * current = new node;
            current->data = stdnt;
            current->next = NULL;
    
    
            if(last)
                last->next = current;
            else
                head = current;
    
    
            last = current;
        }
        return head;
    }
    
    
    struct node  *InsertRecord(struct node *head)
    {
        struct  node *record = new node;
        
        struct student *stdnt = readStudent();
        record->data=stdnt;
        
        record->next=head;
        
        return record;
        
    }
    
    
    double calculate_aggregiate(struct student *stud)
    {    
        student *stdnt = stud;
        double aggr;
        aggr = stdnt->matric_marks * 10/100  + stdnt->inter_marks * 50/100 + 
            stdnt->entryTest_marks * 40/100;
            
        return aggr;
    
    
    }
    
    
    struct node *DeleteRecord(struct node *head)
    {
        clearWindow();
        struct node *curr,*prev, *temp;
      int tdata;
      if(head==NULL)
      {
        gotoxy(33,10);cout<<"NO RECORDS TO DELETE......!";
      }
      else
      {
      gotoxy(33,10);cout<<"Enter Student ID To Be Deleted: ";
      cin>>tdata;
      prev=curr=head;
      while((curr!=NULL)&&(curr->data->id!=tdata))
      {
        prev=curr;
        curr=curr->next;
      }
      if(curr==NULL)
      {
        gotoxy(33,12);cout<<"The Requested ID Is Not Found...!";
      }
      else if(curr==head)
      {
        head=head->next;
        gotoxy(33,12);cout<<"DATA DELETED....!";
      }
      else
      {
        prev->next=curr->next;
        if(curr->next==NULL)
        {
          temp=prev;
        }
        gotoxy(33,12);cout<<"DATA DELETED....!"<<tdata;
      }
      delete(curr);
      }    
      return head;
    }
    
    
    void ModifyRecord(struct node *head)
    {
        
        clearWindow();
         int ch, sid;
         struct node *current;
         struct student *stdnt;
         if (head==NULL)
         {
             gotoxy(33,8);cout<<"NO RECORD TO MODIFY..!";
         }
         else
         {
             gotoxy(33,8);cout<<"Enter Student ID To Modify: ";
             cin>>sid;
             current=head;
     while((current!=NULL) && (current->data->id!=sid))
     {
         current=current->next;
     }
      if (current==NULL) 
      {gotoxy(33,10);cout<<"The Requested ID is Not Found";}
    
    
      else if(current->data->id==sid)
        {
         gotoxy(33,10);cout<<"What Do You Want To Modify";
      gotoxy(33,12);cout<<"1. Student's Name";
      gotoxy(33,14);cout<<"2. Student's Matric Marks";
      gotoxy(33,16);cout<<"3. Student's Intermediate Marks";
      gotoxy(33,18);cout<<"4. Student's Entry Test Marks";
      gotoxy(33,20);cout<<"Enter Your Choice: ";
      cin>>ch;
      switch(ch)
      {
      case 1 :  gotoxy(33,22);cout<<"Enter New Name: ";
              cin.getline(current->data->name, 50);break;
      case 2 :  gotoxy(33,22);cout<<"Enter New Matric Marks: ";
              cin>>(current->data->matric_marks);break;
      case 3 :  gotoxy(33,22);cout<<"Enter New Intermediate Marks: ";
              cin>>(current->data->inter_marks);break;
      case 4 :  gotoxy(33,22);cout<<"Enter New Entry Test Marks: ";
              cin>>(current->data->entryTest_marks);break;
              current->data->aggr = current->data->matric_marks * 10/100  + current->data->inter_marks * 50/100 + 
            current->data->entryTest_marks * 40/100;
      }
      gotoxy(33,24);cout<<"RECORD MODIFIED....!";
      
     }
      
      
     }
    
    
         }
    
    
    
    
    void SearchRecord(struct node *head)
    {
        clearWindow();
        int s_id;
         struct node *current;
         if (head==NULL)
         {
             gotoxy(33,8);cout<<"NO RECORD TO SEARCH..!";
         }
         else
         {
             gotoxy(33,8);cout<<"Enter Student ID To Be Searched: ";
             cin>>s_id;
             current=head;
         while ((current!=NULL) && (current->data->id!=s_id))
        {
         current=current->next;
        }
         if (current==NULL) 
      {gotoxy(33,10);cout<<"The Requested ID is Not Found";}
    
    
         else     if (current->data->id==s_id)
            {
                 gotoxy(33,10);cout<<"Student ID: "<<current->data->id;
                 gotoxy(33,12);cout<<"Student Name: "<<current->data->name;
                 gotoxy(33,14);cout<<"Student Matric Marks: "<<current->data->matric_marks;
                 gotoxy(33,16);cout<<"Student Intermediate Marks: "<<current->data->inter_marks;
                 gotoxy(33,18);cout<<"Student Entry Test  Marks: "<<current->data->entryTest_marks;
                 gotoxy(33,20);cout<<"Student Aggregate Marks: "<<current->data->aggr;
            if (current->data->aggr >= 70)
        {
            gotoxy(33,22);cout<<"Student Registered In Electrical Engg";
            
        }
        else if (current->data->aggr >= 60)
        {
            gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";
            
        }
        else if (current->data->aggr >=50)
        {
            gotoxy(33,22);cout<<"Student Registered In Computer Science";
            
        }
        else
        {
            gotoxy(33,22);cout<<"Sorry! The Student Doesnt Qualify";
            
        }
             }     
             
            
         }
         
         }
    
    
    
    
    void print(struct node *head)
    {
       clearWindow_p();
        
        struct node *current = head;
        if (head == NULL)
        {
            gotoxy(33,8);cout<<"No Student Registered Yet......!";
        }
        else
        {
            
            cout<<"\n\t\t\t\t\tSTUDENTS STATISTICS";
        while(current)
        {
            struct student *stdnt = current->data;
            cout<<"\n\t\t\t\t\t--------------------------------  ";
            
            cout<<"\n\t\t\t\t\tStudent ID   :"<<stdnt->id;
            cout<<"\n\t\t\t\t\tStudent Name   :"<<stdnt->name;
            cout<<"\n\t\t\t\t\tMatric Marks :"<<stdnt->matric_marks;
            cout<<"\n\t\t\t\t\tIntermediate Marks :"<<stdnt->inter_marks;
            cout<<"\n\t\t\t\t\tEntry Test Marks: "<<stdnt->entryTest_marks;
            cout<<"\n\t\t\t\t\tAggregate: "<<stdnt->aggr;
            if (stdnt->aggr >= 70)
        {
            cout<<"\n\t\t\t\t\tStudent Registered In Electrical Engg";
            
        }
        else if (stdnt->aggr >= 60)
        {
            cout<<"\n\t\t\t\t\tStudent Registered In Mechanical Engg";
            
        }
        else if (stdnt->aggr >=50)
        {
            cout<<"\n\t\t\t\t\tStudent Registered In Computer Science";
            
        }
        else
        {
            cout<<"\n\t\t\t\t\tSorry! The Student Doesnt Qualify";
            
        }
    
    
            current=current->next;
        }
            cout<<"\n\t\t\t\t\t--------------------------------";
    
    
        }
    }
    
    
    
    
    int compareStudents(struct student *left, struct student *right)
    {
        return strcmp(left->name, right->name);
    }
    
    
    
    
    struct node *sort(struct node *head)
    {
    
    
        //using bubble sort
    
    
        int swapped = 0;
    
    
        do
        {    
            swapped = 0;
            struct node *current = head;
            struct node *previous = NULL;
    
    
            while(current && current->next)
            {
                if(compareStudents(current->data, current->next->data) > 0)
                {
                    //swap here
                    
                    struct node *next = current->next;
                    
                    if(previous)
                    {
                        previous->next = next;        
                    }
                    else
                    {
                        head = next;
                    }
                    
                    
                    current->next = next->next;
                    previous = next;
                    previous->next = current;    
                    swapped = 1;
                }
                else
                {
                    previous = current;
                    current = current->next;
                }
    
    
            }
    
    
        } while(swapped);    
    
    
        return head;
    }
    
    
    int main()
    {
        system("color f0");
        window();
        SetColor(28);
            int total,i,choice;
            int x = 2;
            struct node *head;
            head=NULL;
            do
            {
                menu:
                gotoxy(x, 8);cout<<"1.Create a Record File";
                gotoxy(x, 10);cout<<"2.Insert Student's Record";
                gotoxy(x, 12);cout<<"3.Modify Student's Record";
                gotoxy(x, 14);cout<<"4.Delete Student's Record";
                gotoxy(x, 16);cout<<"5.Search Student's Record";
                gotoxy(x, 18);cout<<"6.Print All Records";
                gotoxy(x, 20);cout<<"7.Sort According To Names";
                gotoxy(x, 22);cout<<"8.Clear The Screen";
                gotoxy(x, 24);cout<<"9.Exit";
                gotoxy(x, 26);cout<<"Enter your choice";
                cout<<" [ ]\b\b";
                cin>>choice;
                switch(choice)
                {
                    case 1:
                        clearWindow();
                        gotoxy(33, 8);cout<<"How Many Students Do You Want To Register: ";
                        cin>>total;
                        head=createDatabase(total);
                        break;
                    case 2:
                        head=InsertRecord(head);
                        break;
                    case 3:
                        ModifyRecord(head);
                        break;
                    
                    case 4:
                        DeleteRecord(head);
                        break;
                    case 5:
                        SearchRecord(head);
                        break;
                    case 6:
                        print(head);
                        break;
                    case 7:
                        sort(head);
                        print(head);
                        break;
                    case 8:
                        main();
                        break;
                    default: gotoxy(33,8);cout<<"INVALID OPTION";
            }
            }while(choice!=9);
        getch();
        return 0;
    }


  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Let me introduce you to the concept of unreachable code.
    Code:
          case 4:
            gotoxy(33, 22);
            cout << "Enter New Entry Test Marks: ";
            cin >> (current->data->entryTest_marks);
            break;
            current->data->aggr =
                current->data->matric_marks * 10 / 100 + current->data->inter_marks * 50 / 100 +
                current->data->entryTest_marks * 40 / 100;
    
    Your calculation NEVER happens, not with that break statement where it is.

    Some other things as well.
    Code:
    $ g++ -Wall -Wextra bar.cpp
    bar.cpp: In function ‘node* DeleteRecord(node*)’:
    bar.cpp:155:31: warning: variable ‘temp’ set but not used [-Wunused-but-set-variable]
    bar.cpp: In function ‘void ModifyRecord(node*)’:
    bar.cpp:201:22: warning: unused variable ‘stdnt’ [-Wunused-variable]
    bar.cpp: In function ‘int main()’:
    bar.cpp:441:19: warning: unused variable ‘i’ [-Wunused-variable]
    bar.cpp:447:13: warning: label ‘menu’ defined but not used [-Wunused-label]
    Compile with as many warnings enabled as you can find, and make sure you fix them before running the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
      switch(ch)
      {
      case 1 :  gotoxy(33,22);cout<<"Enter New Name: ";
              cin.getline(current->data->name, 50);break;
      case 2 :  gotoxy(33,22);cout<<"Enter New Matric Marks: ";
              cin>>(current->data->matric_marks);break;
      case 3 :  gotoxy(33,22);cout<<"Enter New Intermediate Marks: ";
              cin>>(current->data->inter_marks);break;
      case 4 :  gotoxy(33,22);cout<<"Enter New Entry Test Marks: ";
              cin>>(current->data->entryTest_marks);break;
              current->data->aggr = current->data->matric_marks * 10/100  + current->data->inter_marks * 50/100 + 
            current->data->entryTest_marks * 40/100;
      }
    You' calculating the aggregate percentage inside the switch statement.
    Move it out.

    Better indentation would help to find the error.
    Kurt

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    2
    thank you so much. It worked. One more thing I want to ask you if you don't mind that I also want student id to be unique. Means if the user entered a student id that is already exist in other record then it display a message. How would I do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to access data from a Linked List?
    By gabrielksa in forum C Programming
    Replies: 2
    Last Post: 01-25-2011, 08:51 PM
  2. linked list data input.
    By Springy in forum C Programming
    Replies: 5
    Last Post: 10-19-2009, 01:55 AM
  3. Modify to make Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 10
    Last Post: 11-03-2008, 06:25 PM
  4. linked list/ stored data
    By SpEkTrE in forum C++ Programming
    Replies: 12
    Last Post: 04-11-2005, 02:51 PM
  5. Read Data-Linked list pro
    By Supra in forum C Programming
    Replies: 8
    Last Post: 09-15-2001, 05:34 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21