CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2016
    Posts
    9

    how to add the ability to calculate GPA of every stored student in this code

    this program gets and stores information about students , you can add , remove and also add and remove course for every student , also prints the information


    but how can i add the ability to calculate GPA for every student ? like after asking the course name , can someone change it so that it would ask the grade of the course , then after that changes the GPA of the student and stores it , also prints it


    also if you can correct any part of the code and make it simpler and easier to understand it would be great


    Code:
    #include <iostream>
    #include <iomanip>
    
    template<typename T>
    class linkedlist {
    private:
        struct node {
            node *prev;
            node *next;
            T data;
    
            node() {
            }
    
            node(node *prev, node *next, const T &data) :
                    prev(prev), next(next), data(data) {
            }
        };
    
        node first;
        node last;
    
    public:
        linkedlist() :
                first(nullptr, &last, T()),
                last(&first, &last, T()) {
        }
    
        linkedlist(const T & nil) :
                first(nullptr, &last, nil),
                last(&first, &last, nil) {
        }
    
        linkedlist(const linkedlist<T> &other) :
                first(nullptr, &last, T()),
                last(&first, &last, T()) {
            (*this) = other;
        }
    
        class iterator {
            friend class linkedlist;
    
        private:
            node *current_node;
    
            iterator(node *current_node) : current_node(current_node) {
            }
    
        public:
            iterator(const iterator &other_iterator) : current_node(other_iterator.current_node) {
            }
    
            T &operator*() {
                return current_node->data;
            }
    
            const T &operator*() const {
                return current_node->data;
            }
    
            iterator &operator++() {
                current_node = current_node->next;
                return *this;
            }
    
            iterator operator++(int) {
                iterator tmp = iterator(*this);
                ++(*this);
                return tmp;
            }
    
            bool operator!=(const iterator &other) const {
                return current_node != other.current_node;
            }
    
            bool operator==(const iterator &other) const {
                return current_node == other.current_node;
            }
        };
    
        class const_iterator {
            friend class linkedlist;
    
        private:
            const node *current_node;
    
            const_iterator(const node *current_node) : current_node(current_node) {
            }
    
        public:
            const_iterator(const const_iterator &other_iterator) : current_node(other_iterator.current_node) {
            }
    
            const T &operator*() const {
                return current_node->data;
            }
    
            const_iterator &operator++() {
                current_node = current_node->next;
                return *this;
            }
    
            const_iterator operator++(int) {
                const_iterator tmp = const_iterator(*this);
                ++(*this);
                return tmp;
            }
    
            bool operator!=(const const_iterator &other) const {
                return current_node != other.current_node;
            }
    
            bool operator==(const const_iterator &other) const {
                return current_node == other.current_node;
            }
        };
    
        T &append(const T &data) {
            node *new_node = new node(last.prev, &last, data);
            new_node->prev->next = new_node;
            new_node->next->prev = new_node;
            return new_node->data;
        }
    
        iterator &erase(iterator &&iterator) {
            if (iterator.current_node == &first || iterator.current_node == &last)
                return iterator;
            node *to_remove = iterator.current_node;
            ++iterator;
            to_remove->prev->next = to_remove->next;
            to_remove->next->prev = to_remove->prev;
            delete to_remove;
            return iterator;
        }
    
        iterator erase(iterator &iterator) {
            if (iterator.current_node == &first || iterator.current_node == &last)
                return iterator;
            node *to_remove = iterator.current_node;
            ++iterator;
            to_remove->prev->next = to_remove->next;
            to_remove->next->prev = to_remove->prev;
            delete to_remove;
            return iterator;
        }
    
        iterator begin() {
            return iterator(first.next);
        }
    
        iterator end() {
            return iterator(&last);
        }
    
        const_iterator cbegin() const {
            return const_iterator(first.next);
        }
    
        const_iterator cend() const {
            return const_iterator(&last);
        }
    
        iterator find(const T &data) {
            for (iterator it = begin(); it != end(); ++it) {
                T &current_item = *it;
                if (current_item == data) {
                    return it;
                }
            }
            return end();
        }
    
        bool operator==(const linkedlist<T> &other) {
            const_iterator other_it = other.cbegin();
            const_iterator self_it = cbegin();
    
            while (other_it != other.cend() || other_it != other.cend()) {
                if (*self_it != *other_it) return false;
                ++self_it;
                ++other_it;
            }
    
            return other_it == other.cend() && other_it == other.cend();
        }
    
        linkedlist<T>&operator=(const linkedlist<T> &other) {
            this->first.data = other.first.data;
            this->last.data = other.last.data;
            for (linkedlist<T>::const_iterator it = other.cbegin(); it != other.cend(); it++) {
                append(*it);
            }
            return *this;
        }
    
        ~linkedlist() {
            while (begin() != end()) {
                erase(begin());
            }
        }
    };
    
    struct student {
        std::string name;
        linkedlist<std::string> courses;
    
        student() : name(""), courses("") {
        }
    
        student(const student &student) : name(student.name), courses(student.courses) {
        }
    
        student(const std::string &name) : name(name), courses("") {
        }
    
        bool operator==(const student &other) {
            return name == other.name && courses == other.courses;
        }
    };
    
    linkedlist<student> database;
    
    void add_course(student &std) {
        std::cout << "Main > Student \"" << std.name << "\"" << " > Add course" << std::endl;
        while (true) {
            std::cout << "Enter course to add (or \"back\" to go back): ";
            std::string query;
            std::cin >> query;
    
            if (query == "back")
                return;
    
            linkedlist<std::string>::iterator found = std.courses.find(query);
            if (found == std.courses.end()) {
                std.courses.append(query);
                std::cout << "Course " << "\"" << query << "\"" << " successfully created" << std::endl;
                return;
            }
            else {
                std::cout << "Course " << "\"" << query << "\"" << " already exists" << std::endl;
            }
        }
    }
    
    void remove_course(student &std) {
        std::cout << "Main > Student \"" << std.name << "\"" << std::endl;
        while (true) {
            std::cout << "Enter course to remove (or \"back\" to go back): ";
            std::string query;
            std::cin >> query;
    
            if (query == "back")
                return;
    
            linkedlist<std::string>::iterator found = std.courses.find(query);
            if (found == std.courses.end()) {
                std::cout << "Course " << "\"" << query << "\"" << " Not found" << std::endl;
            }
            else {
                std.courses.erase(found);
                std::cout << "Course " << "\"" << query << "\"" << " successfully removed" << std::endl;
                return;
            }
        }
    
    }
    
    void remove_student(student &std) {
        linkedlist<student>::iterator iterator = database.find(std);
        database.erase(iterator);
        std::cout << "Student \"" << std.name << "\" removed successfully" << std::endl;
    }
    
    void show_courses(student &std) {
        std::cout << "Main > Student \"" << std.name << "\" > Course List" << std::endl;
        int i = 0;
        for (linkedlist<std::string>::const_iterator it = std.courses.cbegin(); it != std.courses.cend(); it++) {
            std::string course_name = *it;
            std::cout << std::left << std::setw(5) << ++i << course_name << std::endl;
        }
    }
    
    void student_menu(student &std) {
        while (true) {
            std::cout << "Main > Student \"" << std.name << "\"" << std::endl
            << "\t1) Add course" << std::endl
            << "\t2) Remove course" << std::endl
            << "\t3) Show courses" << std::endl
            << "\t4) Remove student" << std::endl
            << "\t5) Go back" << std::endl
            << "Enter your option: ";
    
            int option = 0;
            std::cin >> option;
            switch (option) {
                case 1:
                    std::cout << std::endl;
                    add_course(std);
                    std::cout << std::endl;
                    break;
                case 2:
                    std::cout << std::endl;
                    remove_course(std);
                    std::cout << std::endl;
                    break;
                case 3:
                    std::cout << std::endl;
                    show_courses(std);
                    std::cout << std::endl;
                    break;
                case 4:
                    std::cout << std::endl;
                    remove_student(std);
                    return;
                case 5:
                    return;
                default:
                    std::cout << "Invalid option: " << option << std::endl;
            }
        }
    }
    
    void add_student() {
        std::cout << "Main > Create new student " << std::endl;
        std::string student_name;
    
        while (true) {
            std::cout << "Enter student name (or \"back\" to go back): ";
            std::cin >> student_name;
    
            if (student_name == "back")
                return;
    
            if (student_name.empty()) {
                std::cout << "Error: Student name can not be empty!" << std::endl;
                continue;
            }
            for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++) {
                const student &older_student = *it;
                if (older_student.name == student_name) {
                    std::cout << "Error: Student already exists" << std::endl;
                    continue;
                }
            }
            std::cout << "Student \"" << student_name << "\" created successfully" << std::endl;
            break;
        }
    
        student student(student_name);
    
        std::cout << std::endl;
        student_menu(database.append(student));
    }
    
    void list_students() {
        std::cout << "Main > List students" << std::endl;
        int i = 0;
        for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++) {
            const student &std = *it;
            std::cout << std::setw(5) << std::left << ++i << std.name << std::endl;
        }
    }
    
    
    void find_student() {
        std::cout << "Main > Find student" << std::endl;
        while (true) {
            std::cout << "Enter student name (or enter \"back\" to go back): ";
            std::string query;
            std::cin >> query;
            if (query == "back") return;
            for (linkedlist<student>::iterator it = database.begin(); it != database.end(); it++) {
                student &std = *it;
                if (std.name == query) {
                    student_menu(std);
                    break;
                }
            }
            std::cout << "Error: Student not found!" << std::endl;
        }
    }
    
    void main_menu() {
        while (true) {
            std::cout << "Main" << std::endl
            << "\t1) Add student" << std::endl
            << "\t2) List students" << std::endl
            << "\t3) Find a student" << std::endl
            << "\t4) Exit" << std::endl
            << "Enter your option: ";
    
    
            int option = 0;
            std::cin >> option;
    
            std::cout << std::endl;
    
            switch (option) {
                case 1:
                    add_student();
                    break;
                case 2:
                    list_students();
                    break;
                case 3:
                    find_student();
                    break;
                case 4:
                    std::cout << "Goodbye";
                    return;
                default:
                    std::cout << "Invalid option: " << option << std::endl;
            }
    
            std::cout << std::endl;
        }
    }
    
    int main() {
        main_menu();
    
        return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    May 2016
    Posts
    9

    need help with this code ( student information manager)

    this code stores information about students (with linked list) , i need to add these abilities to it :

    1.add and remove course and get the grade of the course
    2.calculating the GPA of students based on their grades
    3.search for a specific student


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    
    struct node {                                                               /* node structure definition */
       int id;
       char name[20];
       float gpa;
       struct node *next;
    };
    
    typedef struct node * nodeptr;                                  /* data type for node ptrs */
    
    nodeptr addnode(nodeptr list);                                /* allocate memory for new node */
    nodeptr insert(nodeptr list, nodeptr p);                   /* insert a node */
    int deletenode(nodeptr *,int);                                   /* locate & delete a node */
    void printlist(nodeptr);                                               /* print list contents */
    
    int main(void)
    {
    
       nodeptr list = NULL;                                               /* pointer to list */
       int del;                                                                      /* student to be deleted from list */
       int sel;                                                                      /* menu selection */
    
       while(true)
       {
                            printf("\nMenu:\n");
                            printf("1 - Add a Student\n");
                            printf("2 - Delete a Student\n");
                            printf("3 - Print Students\n");
                            printf("4 - Exit\n");
                            printf("Make your selection ->");
                            fflush(stdin);
                            scanf("%d", &sel);
                            printf("\n");
                            switch(sel)
                            {
                                        case 1:
                                                    list=addnode(list);
                                                    break;
    
                                        case 2:
                                                    printf("Enter Student ID to delete a student -> ");
                                                    scanf("%d",&del);
    
                                                    if (!deletenode(&list,del))      /* try to delete node, return flag */
                                                    {
                                                                printf("Student ID %d was not on the list\n", del);
                                                    }
                                                    else
                                                    {
                                                                printf("%d has been deleted\n",del);
                                                    }
                                                    break;
    
                                        case 3:
                                                    printlist(list);
                                                    break;
    
                                        case 4:
                                                    printf("Bye!\n");
                                                    exit(1);
    
                                        default:
                                                    printf ("Invalid selection.  Please try again.\n\n\n");
                                                    break;
                            }
       }
       return 0;
    }
    
    
    nodeptr addnode(nodeptr list)
    {
       /* This function allocates memory for new node*/
       nodeptr p;
    
       if ((p = (nodeptr)malloc(sizeof(struct node))) != NULL)
                {
                 p->next = NULL;                   /* initialize next to NULL */
                 printf("Please enter student ID: ");
                 fflush(stdin);
                 scanf("%d", &p->id);
                 printf("Please enter student name: ");
                 fflush(stdin);
                 gets(p->name);
                 printf("Please enter student GPA: ");
                 fflush(stdin);
                 scanf("%f", &p->gpa);
    
                 list = insert(list,p);                                         /* save p into list */
        }
        else
                {                                                                       /* else insufficient memory */
                 printf("Insufficient memory to build entire list.\n");
                }
                return(list);
    }
    
    nodeptr insert(nodeptr list, nodeptr p)
    {
    
       nodeptr q;
    
       if ((list == NULL) || (list->id >= p->id)) {                 /* does p go first? */
          p->next = list;                                                        /* put p on front */
          list = p;
       } else {                                                                                  /* else search for spot */
          q = list;                                                                              /* use q to locate spot */
          while ((q->next != NULL) && (q->next->id < p->id))
                 q = q->next;
          p->next = q->next;                                                           /* attach back of list to p */
          q->next = p;                                                          /* attach front of list to p */
       }
       return(list);
    }
    
    int deletenode(nodeptr *list,int target)
    {
      
    
       nodeptr p,q;                                                            /* p - target, q - predecessor */
       int found = FALSE;                                                /* flag if target found */
    
       if ((*list)->id == target)
       {                                                                                /* if target is at head of list */
          p = *list;                                                                /* p points to target node */
          *list = (*list)->next;                                               /* update external pointer */
          found = TRUE;                                                     /* set flag - target found */
       }
       else
       {                                                                                /* else use loop to search list */
          q = *list;
          while ((q->next != NULL) && (q->next->id < target))
                  {
                              q = q->next;                                      /* stop if found or target not there*/
                  }
          if (q->next != NULL)
                  {                                                                     /* will be true if target not on list*/
             if (q->next->id == target)
                             {                                                          /* is this the target? */
                                        found = TRUE;                       /* set flag - target found */
                                        p = q->next;                            /* p points to target node */
                                        q->next = p->next;                 /* reattach remainder of list to q */
             }
          }
       }
       if (found)
          free(p);                                                                  /* free memory space if necessary */
       return(found);                                                          /* return flag to main */
    }
    
    
    void printlist(nodeptr list)
    {
       /* Print all info about all students */
       while (list != NULL) {
          printf("ID: %d \n",list->id);
                  printf("Name: %s \n",list->name);
                  printf("GPA: %f \n\n",list->gpa);
          list = list->next;
       }
    }

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: need help with this code ( student information manager)

    Quote Originally Posted by omidelf View Post
    this code stores information about students (with linked list) , i need to add these abilities to it :

    1.add and remove course and get the grade of the course
    2.calculating the GPA of students based on their grades
    3.search for a specific student
    And what prevents you to add these abilities?
    Victor Nijegorodov

  4. #4
    Join Date
    May 2016
    Posts
    9

    Re: need help with this code ( student information manager)

    Quote Originally Posted by VictorN View Post
    And what prevents you to add these abilities?
    i tried everthing but it gives me error everytime , maybe i should rewrite the code ?
    how about you help me with the first code and tell me how to add GPA calculator for students that would be nice
    Last edited by omidelf; May 2nd, 2016 at 11:21 PM.

  5. #5
    Join Date
    May 2016
    Posts
    9

    Re: need help with this code ( student information manager)

    i tried this : but it has a bug
    after i add a new student it stores grades with value of 0

    #include <iostream>
    #include <iomanip>




    int i = 0;


    template<typename T>
    class linkedlist {
    private:
    struct node {
    node *prev;
    node *next;
    T data;

    node() {
    }

    node(node *prev, node *next, const T &data) :
    prev(prev), next(next), data(data) {
    }
    };

    node first;
    node last;

    public:
    linkedlist() :
    first(nullptr, &last, T()),
    last(&first, &last, T()) {
    }

    linkedlist(const T & nil) :
    first(nullptr, &last, nil),
    last(&first, &last, nil) {
    }

    linkedlist(const linkedlist<T> &other) :
    first(nullptr, &last, T()),
    last(&first, &last, T()) {
    (*this) = other;
    }

    class iterator {
    friend class linkedlist;

    private:
    node *current_node;

    iterator(node *current_node) : current_node(current_node) {
    }

    public:
    iterator(const iterator &other_iterator) : current_node(other_iterator.current_node) {
    }

    T &operator*() {
    return current_node->data;
    }

    const T &operator*() const {
    return current_node->data;
    }

    iterator &operator++() {
    current_node = current_node->next;
    return *this;
    }

    iterator operator++(int) {
    iterator tmp = iterator(*this);
    ++(*this);
    return tmp;
    }

    bool operator!=(const iterator &other) const {
    return current_node != other.current_node;
    }

    bool operator==(const iterator &other) const {
    return current_node == other.current_node;
    }
    };

    class const_iterator {
    friend class linkedlist;

    private:
    const node *current_node;

    const_iterator(const node *current_node) : current_node(current_node) {
    }

    public:
    const_iterator(const const_iterator &other_iterator) : current_node(other_iterator.current_node) {
    }

    const T &operator*() const {
    return current_node->data;
    }

    const_iterator &operator++() {
    current_node = current_node->next;
    return *this;
    }

    const_iterator operator++(int) {
    const_iterator tmp = const_iterator(*this);
    ++(*this);
    return tmp;
    }

    bool operator!=(const const_iterator &other) const {
    return current_node != other.current_node;
    }

    bool operator==(const const_iterator &other) const {
    return current_node == other.current_node;
    }
    };

    T &append(const T &data) {
    node *new_node = new node(last.prev, &last, data);
    new_node->prev->next = new_node;
    new_node->next->prev = new_node;
    return new_node->data;
    }


    iterator &erase(iterator &&iterator) {
    if (iterator.current_node == &first || iterator.current_node == &last)
    return iterator;
    node *to_remove = iterator.current_node;
    ++iterator;
    to_remove->prev->next = to_remove->next;
    to_remove->next->prev = to_remove->prev;
    delete to_remove;
    return iterator;
    }

    iterator erase(iterator &iterator) {
    if (iterator.current_node == &first || iterator.current_node == &last)
    return iterator;
    node *to_remove = iterator.current_node;
    ++iterator;
    to_remove->prev->next = to_remove->next;
    to_remove->next->prev = to_remove->prev;
    delete to_remove;
    return iterator;
    }

    iterator begin() {
    return iterator(first.next);
    }

    iterator end() {
    return iterator(&last);
    }

    const_iterator cbegin() const {
    return const_iterator(first.next);
    }

    const_iterator cend() const {
    return const_iterator(&last);
    }

    iterator find(const T &data) {
    for (iterator it = begin(); it != end(); ++it) {
    T &current_item = *it;
    if (current_item == data) {
    return it;
    }
    }
    return end();
    }

    bool operator==(const linkedlist<T> &other) {
    const_iterator other_it = other.cbegin();
    const_iterator self_it = cbegin();

    while (other_it != other.cend() || other_it != other.cend()) {
    if (*self_it != *other_it) return false;
    ++self_it;
    ++other_it;
    }

    return other_it == other.cend() && other_it == other.cend();
    }

    linkedlist<T>&operator=(const linkedlist<T> &other) {
    this->first.data = other.first.data;
    this->last.data = other.last.data;
    for (linkedlist<T>::const_iterator it = other.cbegin(); it != other.cend(); it++) {
    append(*it);
    }
    return *this;
    }

    ~linkedlist() {
    while (begin() != end()) {
    erase(begin());
    }
    }
    };

    struct student {
    double grade[100];
    std::string name;
    linkedlist<std::string> courses;


    student() : name(""), courses("") ,grade() {
    }

    student(const student &student) : name(student.name), courses(student.courses) {
    }

    student(const std::string &name) : name(name), courses("") , grade() {
    }

    student(const double &grade) : name(name), courses("") {
    }

    bool operator==(const student &other) {
    return name == other.name && courses == other.courses;
    }
    };

    linkedlist<student> database;
    linkedlist<student> grade;


    void add_course(student &std) {

    std::cout << "Main > Student \"" << std.name << "\"" << " > Add course" << std::endl;
    while (true) {
    std::cout << "Enter course to add (or \"back\" to go back): ";
    std::string query;
    std::cin >> query;
    double grades;
    std::cout <<"Enter Grade :";
    std::cin >> grades;
    std.grade[i] = grades;
    i++;

    if (query == "back")
    return;


    linkedlist<std::string>::iterator found = std.courses.find(query);
    if (found == std.courses.end()) {
    std.courses.append(query);



    for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++ ) {
    const student &older_student = *it;

    }

    std::cout << "Course " << "\"" << query << "\"" << " successfully created" << std::endl;
    return;
    }
    else {
    std::cout << "Course " << "\"" << query << "\"" << " already exists" << std::endl;
    }
    }
    }

    void remove_course(student &std) {
    std::cout << "Main > Student \"" << std.name << "\"" << std::endl;
    while (true) {
    std::cout << "Enter course to remove (or \"back\" to go back): ";
    std::string query;
    std::cin >> query;

    if (query == "back")
    return;

    linkedlist<std::string>::iterator found = std.courses.find(query);
    if (found == std.courses.end()) {
    std::cout << "Course " << "\"" << query << "\"" << " Not found" << std::endl;
    }
    else {
    std.courses.erase(found);
    std::cout << "Course " << "\"" << query << "\"" << " successfully removed" << std::endl;
    return;
    }
    }

    }

    void remove_student(student &std) {
    linkedlist<student>::iterator iterator = database.find(std);
    database.erase(iterator);
    std::cout << "Student \"" << std.name << "\" removed successfully" << std::endl;
    }

    void show_courses(student &std) {
    int j=0;
    std::cout << "Main > Student \"" << std.name << "\" > Course List" << std::endl;
    int i = 0;
    for (linkedlist<std::string>::const_iterator it = std.courses.cbegin(); it != std.courses.cend(); it++) {
    std::string course_name = *it;

    std::cout << " Course Name : " << course_name << " / Grade : " << std.grade[j];
    j++;
    }
    }





    void student_menu(student &std) {
    while (true) {
    std::cout << "Main > Student \"" << std.name << "\"" << std::endl
    << "\t1) Add course" << std::endl
    << "\t2) Remove course" << std::endl
    << "\t3) Show courses" << std::endl
    << "\t4) Remove student" << std::endl
    << "\t5) Go back" << std::endl
    << "Enter your option: ";

    int option = 0;
    std::cin >> option;
    switch (option) {
    case 1:
    std::cout << std::endl;
    add_course(std);
    std::cout << std::endl;
    break;
    case 2:
    std::cout << std::endl;
    remove_course(std);
    std::cout << std::endl;
    break;
    case 3:
    std::cout << std::endl;
    show_courses(std);
    std::cout << std::endl;
    break;
    case 4:
    std::cout << std::endl;
    remove_student(std);
    return;
    case 5:
    return;
    default:
    std::cout << "Invalid option: " << option << std::endl;
    }
    }
    }

    void add_student() {
    std::cout << "Main > Create new student " << std::endl;
    std::string student_name;

    while (true) {
    std::cout << "Enter student name (or \"back\" to go back): ";
    std::cin >> student_name;


    if (student_name == "back")
    return;

    if (student_name.empty()) {
    std::cout << "Error: Student name can not be empty!" << std::endl;
    continue;
    }
    for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++) {
    const student &older_student = *it;
    if (older_student.name == student_name) {
    std::cout << "Error: Student already exists" << std::endl;
    continue;
    }
    }
    std::cout << "Student \"" << student_name << "\" created successfully" << std::endl;
    break;
    }

    student student(student_name);

    std::cout << std::endl;
    student_menu(database.append(student));
    }

    void list_students() {
    std::cout << "Main > List students" << std::endl;
    int i = 0;
    for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++) {
    const student &std = *it;
    std::cout << std::setw(5) << std::left << ++i << std.name << std::endl;
    }
    }


    void find_student() {
    std::cout << "Main > Find student" << std::endl;
    while (true) {
    std::cout << "Enter student name (or enter \"back\" to go back): ";
    std::string query;
    std::cin >> query;
    if (query == "back") return;
    for (linkedlist<student>::iterator it = database.begin(); it != database.end(); it++) {
    student &std = *it;
    if (std.name == query) {
    student_menu(std);
    break;
    }
    }
    std::cout << "Error: Student not found!" << std::endl;
    }
    }

    void main_menu() {
    while (true) {
    std::cout << "Main" << std::endl
    << "\t1) Add student" << std::endl
    << "\t2) List students" << std::endl
    << "\t3) Find a student" << std::endl
    << "\t4) Exit" << std::endl
    << "Enter your option: ";


    int option = 0;
    std::cin >> option;

    std::cout << std::endl;

    switch (option) {
    case 1:
    add_student();
    break;
    case 2:
    list_students();
    break;
    case 3:
    find_student();
    break;
    case 4:
    std::cout << "Goodbye";
    return;
    default:
    std::cout << "Invalid option: " << option << std::endl;
    }

    std::cout << std::endl;
    }
    }

    int main() {
    main_menu();

    return EXIT_SUCCESS;
    }

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: how to add the ability to calculate GPA of every stored student in this code

    PLease, use CODE tags, not the QUOTE ones while posting the code snippets!
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to add the ability to calculate GPA of every stored student in this code

    but it has a bug after i add a new student it stores grades with value of 0
    and using the debugger shows the problem to be where? When confronted with a program that compiles but doesn't work as expected, the debugger is your friend to determine where program execution deviates from that expected from the design.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to add the ability to calculate GPA of every stored student in this code

    Code:
    struct student {
    	double grade[100];
    	std::string name;
    	linkedlist<std::string> courses;
    ...
    This is not a particularly good data structure as courses are a linked list but the related grades are an array. It would be better to have a structure that holds a course and a grade and then have a linked list of this structure. Something like
    Code:
    struct Course {
        string cname;
        int cgrade;
    };
    
    struct student {
        string sname;
        linkedlist<Course> courses;
    ...
    };
    PS It is also not good practice to use global variables - especially one called i !! if the value of a variable is required to be known between function calls, then make it static within the function.
    Last edited by 2kaud; May 3rd, 2016 at 04:29 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Jun 2022
    Posts
    3

    Re: how to add the ability to calculate GPA of every stored student in this code

    was it fixed? what was the problem in it?

  10. #10
    Join Date
    Jun 2022
    Posts
    3

    Re: how to add the ability to calculate GPA of every stored student in this code

    Quote Originally Posted by omidelf View Post
    this program gets and stores information about students , you can add , remove and also add and remove course for every student , also prints the information

    but how can i add the ability to calculate GPA for every student ? like after asking the course name , can someone change it so that it would ask the grade of the course , then after that changes the GPA of the student and stores it , also prints it


    also if you can correct any part of the code and make it simpler and easier to understand it would be great

    Maybe I should use some online services for that? I heard that there are some kind of websites, where you can get help. My friend told me that he has recently found some useful essay examples on https://studyhippo.com/essay-examples/the-great-gatsby/ about the great gatsby, and it really helped him, as a student. I was wondering maybe I should try something similar?
    Code:
    #include <iostream>
    #include <iomanip>
    
    template<typename T>
    class linkedlist {
    private:
        struct node {
            node *prev;
            node *next;
            T data;
    
            node() {
            }
    
            node(node *prev, node *next, const T &data) :
                    prev(prev), next(next), data(data) {
            }
        };
    
        node first;
        node last;
    
    public:
        linkedlist() :
                first(nullptr, &last, T()),
                last(&first, &last, T()) {
        }
    
        linkedlist(const T & nil) :
                first(nullptr, &last, nil),
                last(&first, &last, nil) {
        }
    
        linkedlist(const linkedlist<T> &other) :
                first(nullptr, &last, T()),
                last(&first, &last, T()) {
            (*this) = other;
        }
    
        class iterator {
            friend class linkedlist;
    
        private:
            node *current_node;
    
            iterator(node *current_node) : current_node(current_node) {
            }
    
        public:
            iterator(const iterator &other_iterator) : current_node(other_iterator.current_node) {
            }
    
            T &operator*() {
                return current_node->data;
            }
    
            const T &operator*() const {
                return current_node->data;
            }
    
            iterator &operator++() {
                current_node = current_node->next;
                return *this;
            }
    
            iterator operator++(int) {
                iterator tmp = iterator(*this);
                ++(*this);
                return tmp;
            }
    
            bool operator!=(const iterator &other) const {
                return current_node != other.current_node;
            }
    
            bool operator==(const iterator &other) const {
                return current_node == other.current_node;
            }
        };
    
        class const_iterator {
            friend class linkedlist;
    
        private:
            const node *current_node;
    
            const_iterator(const node *current_node) : current_node(current_node) {
            }
    
        public:
            const_iterator(const const_iterator &other_iterator) : current_node(other_iterator.current_node) {
            }
    
            const T &operator*() const {
                return current_node->data;
            }
    
            const_iterator &operator++() {
                current_node = current_node->next;
                return *this;
            }
    
            const_iterator operator++(int) {
                const_iterator tmp = const_iterator(*this);
                ++(*this);
                return tmp;
            }
    
            bool operator!=(const const_iterator &other) const {
                return current_node != other.current_node;
            }
    
            bool operator==(const const_iterator &other) const {
                return current_node == other.current_node;
            }
        };
    
        T &append(const T &data) {
            node *new_node = new node(last.prev, &last, data);
            new_node->prev->next = new_node;
            new_node->next->prev = new_node;
            return new_node->data;
        }
    
        iterator &erase(iterator &&iterator) {
            if (iterator.current_node == &first || iterator.current_node == &last)
                return iterator;
            node *to_remove = iterator.current_node;
            ++iterator;
            to_remove->prev->next = to_remove->next;
            to_remove->next->prev = to_remove->prev;
            delete to_remove;
            return iterator;
        }
    
        iterator erase(iterator &iterator) {
            if (iterator.current_node == &first || iterator.current_node == &last)
                return iterator;
            node *to_remove = iterator.current_node;
            ++iterator;
            to_remove->prev->next = to_remove->next;
            to_remove->next->prev = to_remove->prev;
            delete to_remove;
            return iterator;
        }
    
        iterator begin() {
            return iterator(first.next);
        }
    
        iterator end() {
            return iterator(&last);
        }
    
        const_iterator cbegin() const {
            return const_iterator(first.next);
        }
    
        const_iterator cend() const {
            return const_iterator(&last);
        }
    
        iterator find(const T &data) {
            for (iterator it = begin(); it != end(); ++it) {
                T ¤t_item = *it;
                if (current_item == data) {
                    return it;
                }
            }
            return end();
        }
    
        bool operator==(const linkedlist<T> &other) {
            const_iterator other_it = other.cbegin();
            const_iterator self_it = cbegin();
    
            while (other_it != other.cend() || other_it != other.cend()) {
                if (*self_it != *other_it) return false;
                ++self_it;
                ++other_it;
            }
    
            return other_it == other.cend() && other_it == other.cend();
        }
    
        linkedlist<T>&operator=(const linkedlist<T> &other) {
            this->first.data = other.first.data;
            this->last.data = other.last.data;
            for (linkedlist<T>::const_iterator it = other.cbegin(); it != other.cend(); it++) {
                append(*it);
            }
            return *this;
        }
    
        ~linkedlist() {
            while (begin() != end()) {
                erase(begin());
            }
        }
    };
    
    struct student {
        std::string name;
        linkedlist<std::string> courses;
    
        student() : name(""), courses("") {
        }
    
        student(const student &student) : name(student.name), courses(student.courses) {
        }
    
        student(const std::string &name) : name(name), courses("") {
        }
    
        bool operator==(const student &other) {
            return name == other.name && courses == other.courses;
        }
    };
    
    linkedlist<student> database;
    
    void add_course(student &std) {
        std::cout << "Main > Student \"" << std.name << "\"" << " > Add course" << std::endl;
        while (true) {
            std::cout << "Enter course to add (or \"back\" to go back): ";
            std::string query;
            std::cin >> query;
    
            if (query == "back")
                return;
    
            linkedlist<std::string>::iterator found = std.courses.find(query);
            if (found == std.courses.end()) {
                std.courses.append(query);
                std::cout << "Course " << "\"" << query << "\"" << " successfully created" << std::endl;
                return;
            }
            else {
                std::cout << "Course " << "\"" << query << "\"" << " already exists" << std::endl;
            }
        }
    }
    
    void remove_course(student &std) {
        std::cout << "Main > Student \"" << std.name << "\"" << std::endl;
        while (true) {
            std::cout << "Enter course to remove (or \"back\" to go back): ";
            std::string query;
            std::cin >> query;
    
            if (query == "back")
                return;
    
            linkedlist<std::string>::iterator found = std.courses.find(query);
            if (found == std.courses.end()) {
                std::cout << "Course " << "\"" << query << "\"" << " Not found" << std::endl;
            }
            else {
                std.courses.erase(found);
                std::cout << "Course " << "\"" << query << "\"" << " successfully removed" << std::endl;
                return;
            }
        }
    
    }
    
    void remove_student(student &std) {
        linkedlist<student>::iterator iterator = database.find(std);
        database.erase(iterator);
        std::cout << "Student \"" << std.name << "\" removed successfully" << std::endl;
    }
    
    void show_courses(student &std) {
        std::cout << "Main > Student \"" << std.name << "\" > Course List" << std::endl;
        int i = 0;
        for (linkedlist<std::string>::const_iterator it = std.courses.cbegin(); it != std.courses.cend(); it++) {
            std::string course_name = *it;
            std::cout << std::left << std::setw(5) << ++i << course_name << std::endl;
        }
    }
    
    void student_menu(student &std) {
        while (true) {
            std::cout << "Main > Student \"" << std.name << "\"" << std::endl
            << "\t1) Add course" << std::endl
            << "\t2) Remove course" << std::endl
            << "\t3) Show courses" << std::endl
            << "\t4) Remove student" << std::endl
            << "\t5) Go back" << std::endl
            << "Enter your option: ";
    
            int option = 0;
            std::cin >> option;
            switch (option) {
                case 1:
                    std::cout << std::endl;
                    add_course(std);
                    std::cout << std::endl;
                    break;
                case 2:
                    std::cout << std::endl;
                    remove_course(std);
                    std::cout << std::endl;
                    break;
                case 3:
                    std::cout << std::endl;
                    show_courses(std);
                    std::cout << std::endl;
                    break;
                case 4:
                    std::cout << std::endl;
                    remove_student(std);
                    return;
                case 5:
                    return;
                default:
                    std::cout << "Invalid option: " << option << std::endl;
            }
        }
    }
    
    void add_student() {
        std::cout << "Main > Create new student " << std::endl;
        std::string student_name;
    
        while (true) {
            std::cout << "Enter student name (or \"back\" to go back): ";
            std::cin >> student_name;
    
            if (student_name == "back")
                return;
    
            if (student_name.empty()) {
                std::cout << "Error: Student name can not be empty!" << std::endl;
                continue;
            }
            for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++) {
                const student &older_student = *it;
                if (older_student.name == student_name) {
                    std::cout << "Error: Student already exists" << std::endl;
                    continue;
                }
            }
            std::cout << "Student \"" << student_name << "\" created successfully" << std::endl;
            break;
        }
    
        student student(student_name);
    
        std::cout << std::endl;
        student_menu(database.append(student));
    }
    
    void list_students() {
        std::cout << "Main > List students" << std::endl;
        int i = 0;
        for (linkedlist<student>::const_iterator it = database.cbegin(); it != database.cend(); it++) {
            const student &std = *it;
            std::cout << std::setw(5) << std::left << ++i << std.name << std::endl;
        }
    }
    
    
    void find_student() {
        std::cout << "Main > Find student" << std::endl;
        while (true) {
            std::cout << "Enter student name (or enter \"back\" to go back): ";
            std::string query;
            std::cin >> query;
            if (query == "back") return;
            for (linkedlist<student>::iterator it = database.begin(); it != database.end(); it++) {
                student &std = *it;
                if (std.name == query) {
                    student_menu(std);
                    break;
                }
            }
            std::cout << "Error: Student not found!" << std::endl;
        }
    }
    
    void main_menu() {
        while (true) {
            std::cout << "Main" << std::endl
            << "\t1) Add student" << std::endl
            << "\t2) List students" << std::endl
            << "\t3) Find a student" << std::endl
            << "\t4) Exit" << std::endl
            << "Enter your option: ";
    
    
            int option = 0;
            std::cin >> option;
    
            std::cout << std::endl;
    
            switch (option) {
                case 1:
                    add_student();
                    break;
                case 2:
                    list_students();
                    break;
                case 3:
                    find_student();
                    break;
                case 4:
                    std::cout << "Goodbye";
                    return;
                default:
                    std::cout << "Invalid option: " << option << std::endl;
            }
    
            std::cout << std::endl;
        }
    }
    
    int main() {
        main_menu();
    
        return EXIT_SUCCESS;
    }
    I think I know how to fix it.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: how to add the ability to calculate GPA of every stored student in this code

    If you guess that it is still actual for the OP after six years ... then why just to show the fix?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured