Thread: I need help for student information in Linkedlist

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    7

    Unhappy I need help for student information in Linkedlist

    I am having a problem that I could not figure it out. Help

    1- Enter student information
    2 - Quite

    Enter your choice: 1 (Suppose user entered 1)
    (Now the details of Student will be entered)
    Student ID: 1400 (Suppose user entered 1400)
    Student Name: Ali (Suppose user entered Ali)
    (If user enters an ID that is already in the list, a message should be displayed “Already in the list.”) -> I need this help

    Here is my source code:

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    class Node
    {
        public:
            int StudentID;
            string StudentName;
            
            Node *next;
    };
    
    class Linkedlist
    {
        public:
            Node *head;
            
            Linkedlist();
            void StudentInformation(int ID, string name);
            
            
            
    };
    
    Linkedlist::Linkedlist()
    {
        head == NULL;
    }
    
    void Linkedlist::StudentInformation(int ID, string name)
    {
        Node *temp = new Node;
        
        temp->StudentID = ID;
        temp->StudentName = name;
        temp->next = NULL;
        head = temp;
    }
    
    int main()
    {
        Linkedlist list;
        
        int choice;
        int ID;
        string name;
        
        
        while(1)
        {
            cout<<"1. Enter student information \n";
            cout<<"2. Search student by ID \n";
            cout<<"3. Search student by Name \n";
            cout<<"4. Delete student information \n";
            cout<<"5. Update student information \n";
            cout<<"6. Print all students \n";
            cout<<"7. Quit \n";
            cout<<"Enter yoru choice: ";
            cin>>choice;
            
            switch(choice)
            {
                case 1:
                    system("CLS");
                    cout<<"Enter the following student detail: \n";
                    cout<<"ID: ";
                    cin>>ID;
                    cout<<"Name: ";
                    cin>>name;
                    list.StudentInformation(ID, name);
                    system("CLS");
                    break;
                    
                case 7:
                    exit(1);
                
                default:
                    cout<<"Error! Invalid Key... \n";
                    cout<<"Enter any key to get back menu. \n";
                    getch();
                    system("CLS");
            }
        }
        
    }

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    In my short experience it is highly unusual that a reasonably framed question, such as I believe yours to be, does not elicit a single response on this board over 2 days. (Unreasonable questions often return greater number of responses but that's another matter). So in the spirit of fools rushing in where angels fear &c. here's my 2 cents:


    the LinkedList class requires an additional method (let's call it Check_ID) that'd display the message you want. The method would look like:
    Code:
    bool LinkedList::Match_ID(int& ID)const{
        Node* sNode; 
        while(sNode != NULL){
            if (sNode -> GetStudentID() == ID){
                cout<<"Already in the list"<<endl;
                return true;
                }
                sNode = sNode->GetNext();
            
            }
            return false;
        }
    Note that your class' data members are horribly exposed, being declared public, and I had to offer them some modesty by declaring them private and hence the getter methods GetNext(), GetStudentID(). So now, upon cin>>ID, ID is passed through Match_ID() until it returns false and then you'd proceed as before.


    However, if you are not constrained to eschew STL and C++11, may I also propose an alternative, fuller (incl. your menu items 2 - 6) suggestion based on them, as below?
    Code:
    #include<iostream>
    #include<map>
    #include<string>
    #include<algorithm>
    
    
    using namespace std;
    
    
    int main(){
    map<int, string> StudentList;
    int choice, id;
    
    
    while(1){
         cout<<"1. Enter student information        2. Search student by ID             3. Search student by Name \n";
         cout<<"4. Delete student information       5. Update student information       6. Print all students       7. Quit \n";
         cout<<"Enter your choice: ";
         cin>>choice;
        switch(choice){
            case 1:{
                do{cout<<"Enter student id: "<<endl;
                cin>>id;
                } while(StudentList.count(id));/*no matches, returns 0, breaks loop; map, by definition, does not accept duplicate keys but this check ensures that any value corresponding to a duplicate key is not lost */
                cout<<"Enter student name: "<<endl;
                string name;
                cin>>name;
                StudentList.insert(pair<int,string>(id,name));
                }
            break;
            case 2:{
                cout<<"Enter student id to search: "<<endl;
                int search_id;
                cin>>search_id;
                auto search = StudentList.find(search_id);
                if(search != StudentList.end()){
                    cout<<"ID "<<search->first<<" belongs to student: "<<search->second<<endl;
                }else{
                cout<<"ID not found"<<endl;
                    }
                }
             break;
            case 3:{
                cout<<"Enter student name to search: "<<endl;
                string search_name;
                cin>>search_name;
                bool name_found = false;
                map<int, string>::iterator itr;
                for(itr = StudentList.begin(); itr != StudentList.end(); ++itr){
                if(itr->second == search_name){
                    cout<<"Student: "<<itr->second<< " has ID:  "<<itr->first<<endl;
                    name_found = true;
                    }
                }
                if(itr ==StudentList.end() && name_found == false){//how to use range loops and print no name found only once????
                cout<<"No student with that name"<<endl;
                    }
                 }
             break;
            case 4:{cout<<"Enter student id to delete: "<<endl;
                int delete_id;
                cin>>delete_id;
                auto search = StudentList.find(delete_id);
                if(search != StudentList.end()){
                    StudentList.erase(search);
                    cout<<"Records of student with ID: "<<delete_id<<" deleted"<<endl;
                }else{
                cout<<"ID not found"<<endl;
                    }
                }
             break;
            case 5:{cout<<"Enter student id to update: "<<endl;
                int update_id;
                cin>>update_id;
                cout<<"Enter student name to update: "<<endl;
                string update_name;
                cin>>update_name;
                map<int,string>::iterator  search = StudentList.find(update_id);
                if(search != StudentList.end()){//first check key exists, else new element inserted into map;
                    StudentList[update_id] = update_name;
                    cout<<"Records of student with ID: "<<update_id<<" updated"<<endl;
                }else{
                cout<<"ID not found"<<endl;
                    }
            }
            break;
            case 6:{
                for(map<int,string>::iterator itr = StudentList.begin(); itr != StudentList.end(); ++itr){
                    cout<<"StudentID: "<<itr->first<<", StudentName: "<<itr->second<<endl;
                    }
                }
            break;
            case 7: exit(1);
    
    
            default:
                    cout<<"Error! Invalid Key... \n";
                    cout<<"Enter any key to get back menu. \n";
                    cin.get();
                    system("CLS");
            }
        }
    }
    Last edited by sean_cantab; 09-27-2016 at 07:47 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In my short experience it is highly unusual that a reasonably framed question, such as I believe yours to be, does not elicit a single response on this board over 2 days. (Unreasonable questions often return greater number of responses but that's another matter). So in the spirit of fools rushing in where angels fear &c. here's my 2 cents:
    My guess is because the class is so bare, that a decent answer requires substantial time and effort to teach about linked lists. So much that you need to do to get a working linked list is missing that I guess most people concluded the OP didn't really expend that much effort on his homework.

    StudentInformation() will leak memory if there is more than one student because links are not managed at all. The linked list class failed to do anything required. Normally when you implement a linked list, you have some basic, private access housekeeping functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using struct to store to student information
    By 1997king2 in forum C Programming
    Replies: 1
    Last Post: 11-29-2015, 05:08 PM
  2. Replies: 2
    Last Post: 07-23-2015, 08:49 PM
  3. Replies: 17
    Last Post: 11-16-2013, 06:18 PM
  4. Help LinkedList!!
    By dagbai in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:56 PM
  5. Assignment Help !! (Student information system)
    By ashb in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 05:32 AM

Tags for this Thread