Home > Net >  So im trying to make a c program with a menu that adds/subtracts classes from a linked list... why d
So im trying to make a c program with a menu that adds/subtracts classes from a linked list... why d

Time:04-27

problem is occuring "current = head;", "error: cannot convert 'course*' to 'main()::node*' in assignment"

example run should look like: What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 3 You aren't currently taking any courses.

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 1 What course name would you like to add? Intro_to_C What course number would you like to add? COP3223C Added!

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 1 What course name would you like to add? Computer_Science_1 What course number would you like to add? COP3502C Added!

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 3 Course Schedule:
  5. COP3223C - Intro_to_C
  6. COP3502C - Computer Science_1

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 1 What course name would you like to add? Concepts_in_Computer_Science What course number would you like to add? COP2500C Added!

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 3 Course Schedule:
  5. COP2500C - Concepts_in_Computer_Science
  6. COP3223C - Intro_to_C
  7. COP3502C - Computer Science_1

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 2 What course code would you like to drop? COP2100C This course is not in your schedule.

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 2 What course code would you like to drop? COP3223C Course has been removed.

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 3 Course Schedule:
  5. COP2500C - Concepts_in_Computer_Science
  6. COP3502C - Computer Science_1

What courses would you like to do?

  1. Add Course
  2. Drop Course
  3. Print Schedule
  4. Exit 4

Done!

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//linked list of structs 


struct course {
    char name[31], number[11];
    struct course *next;
};
//prints the list


void printList(struct course *h) {
    struct course *temp = h;
    int num = 1;
    while (temp != NULL) {
        printf("%d. %s - %s\n", num, temp->number, temp->name);
        temp = temp->next;
    }
}

int contains(struct course *h, char code[11]) {
    struct course *temp = h;
    while (temp != NULL) {
        if (strcmp(temp->number, code)) {
            return 1;
        }
        temp = temp->next;
    }
    return 0;
}

int Menu() {
    printf("What courses would you like to do?\n1. Add Course\n2. Drop Course\n3. Print Schedule\n4. Exit\n");
    int value;
    scanf("%d", &value);
    return value;
}

int main() {

    struct course *head = NULL;
    struct node *current = NULL;
    int option = -1;
    while (option != 4) {
        option = Menu();
        if (option == 1) {
            //Add new course
            struct course *new_course = (struct course *) malloc(sizeof(struct course));
            printf("What course name would you like to add?\n");
            scanf("%s", new_course->name);
            printf("What course number would ypou like to add?\n");
            scanf("%s", new_course->number);
            new_course->next = NULL;
            if (head == NULL) {
                head = new_course;
            } else if (contains(head, new_course->number) == 1) {
                printf("Course has already been added.\n");
            } else {
                int flag = 0;
                if (strcmp(head->number, new_course->number) > 0) {
                    new_course->next = head;
                    head = new_course;
                    flag = 1;
                }
                current = head;
                while (current->next != NULL && flag == 0) {
                    if (strcmp(current->next->number, new_course->number) > 0) {
                        new_course->next = current->next;
                        current->next = new_course;
                        flag = 1;
                        break;
                    }
                    current = current->next;
                }
                // inserts at the end;
                if (flag == 0) {
                    current->next = new_course;
                }
            }
        } else if (option == 2) {
            //Remove course
            char remove[11];
            int flag = 0;
            printf("What element would you like to delete?\n");
            scanf("%s", remove);
            while (head != NULL && strcmp(head->number, remove) == 0) {
                head = head->next;
                flag = 1;
            }
            current = head;
            while (current != NULL && current->next != NULL) {
                if (strcmp(current->next->number, remove) == 0) {
                    current->next = current->next->next;
                    flag = 1;
                } else {
                    current = current->next;
                }
            }
            if (flag == 0) {
                printf("This course is not on the schedule.\n");
            } else {
                printf("The course has been deleted.");
            }
        } else if (option == 3) {
            //Print out schedule
            printList(head);

        }
        return 0;

    }

CodePudding user response:

The variable head is of type struct course *, whereas the variable current is of type struct node *. Since they are of different type, you cannot assign one to the other.

You probably want to make the pointer current of type struct course * instead. So you should change the line

struct node *current = NULL;

to:

struct course *current = NULL;
  • Related