0

I am new to programming, taking my first C++ class. I have an assignment and trying to write a C++ code for creating a linked list of Students with operation selections "INSERT, UPDATE, DELETE, SHOW, WRITE". I thought I finished writing the code, because when I'm done I had no errors. However, when I tried to run, "Build Failed" and got errors seen on the image. This is the first time I see this type of error. I will be more than happy if someone help me to figure out why is this happening, and how to solve it. Thank you very much.

My code is following;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

struct person {

    int id;
    char name [20];
    float grade;
    struct person *next ;

}Student;

typedef struct person *StudentPtr;

StudentPtr slist = NULL;

int ItemFound = FALSE;
int ItemExists = FALSE;


void GetStudent (StudentPtr *TemPtr)
{
    *TemPtr = (StudentPtr)malloc(sizeof(person));
}


void FindStudent (StudentPtr *PredPtr, StudentPtr *LocPtr, int id)
{
    *PredPtr = NULL;
    *LocPtr = slist;
    ItemFound = FALSE;
    ItemExists = FALSE;

    while(ItemFound == FALSE && (*LocPtr!=NULL))
    {
        if((*LocPtr)->id < id)
        {
            *PredPtr = *LocPtr;
            *LocPtr = ((*LocPtr)->next);
        }
        else
        {
            ItemFound = TRUE;

            if(((*PredPtr)->next)->id == id)

                ItemExists = TRUE;

        }
    }
}

void AddStudent(int id, char name)
{
    StudentPtr Something, PredPtr, TemPtr;

    FindStudent(&PredPtr, &Something, id);

    GetStudent(&TemPtr);

    TemPtr->id = id;
    TemPtr->grade = 0.0;
    strcat(TemPtr->name, &name);

    if(ItemExists == TRUE)
        printf("Student is already in the list.");
    else
    {
        if(PredPtr == NULL)
    {
        TemPtr->next = slist;
        slist = TemPtr;
        printf("[%d, %s] Adding operation is SUCCESSFUL to the linked list. Student order: ", id, &name);

    }
    else
        TemPtr->next = PredPtr->next;
        PredPtr->next = TemPtr;
        printf("[%d, %s] Adding operation is SUCCESSFUL to the linked list. Student order: ", id, &name);

    }

}

void UpdateStudent(int id, float grade)
{
    StudentPtr Something, PredPtr;

    FindStudent(&PredPtr, &Something, id);

    if(ItemExists == FALSE)
        printf("[%f] Student does not found! Grade is not changed.]", grade);
    else
        (PredPtr->next)->grade = grade;
}

void DeleteStudent(int id)
{
    StudentPtr LocPtr, PredPtr;

    FindStudent(&PredPtr, &LocPtr, id);

    if(ItemExists == FALSE)
        printf("[%d] Student does not found! Delete operation is not completed.", id);
    else
    {   if(PredPtr == NULL)
        {   slist = LocPtr->next;
            free(LocPtr);
            printf("[%d] Delete operation is SUCCESSFUL.", id);
        }
        else
        {
            PredPtr->next = LocPtr->next;
            free(LocPtr);
            printf("[%d] Delete operation is SUCCESSFUL.", id);
        }
    }
}

int main()
{
    int ENDED=FALSE;

    while(ENDED==FALSE)
    {
        printf("Welcome to Student Registration Program!\n\n");
        printf("[1] Insert, [2] Change, [3] Remove, [4] Show, [5] Write, [9] to exit\n\n");
        int selection;
        scanf("%d", &selection);
        printf("\nSelect: %d\n\n", selection);

        if(selection==1 || selection==2 || selection==3 || selection==4 || selection==5 || selection==9)
        {
            int id;

            switch(selection)
            {
                case 1:
                {   char name[40];
                    char surname[40];

                    printf("\nPlease input id, name, surname: ");

                    scanf("%d %s %s", &id, name, surname);

                    strcat(name, " ");
                    strcat(name, surname);

                    void AddStudent(int, char[40]);

                    AddStudent(id, name);

                    break;
                }

                case 2:
                {   float grade;
                    printf("\nPlease input id, grade: ");
                    scanf("%d %f", &id, &grade);

                    UpdateStudent(id, grade);

                    break;
                }

                case 3:
                {   printf("\nPlease input id: ");
                    scanf("%d", &id);

                    DeleteStudent(id);

                    break;
                }

                case 4:
                {   StudentPtr LocPtr = slist;

                    printf("\n        Id             Name   Grade");
                    printf("\n----------   --------------   -----");

                    while(LocPtr != NULL)
                    {
                        printf("\n%10d %16s %7.2f", LocPtr->id, LocPtr->name, LocPtr->grade);
                        LocPtr = LocPtr->next;
                    }

                    printf("\nPlease press a button to continue...");
                    char button;
                    scanf("%c", &button);

                    break;
                }

                case 5:
                {   FILE *fout = fopen("Students.txt","w");
                    StudentPtr temp;
                    temp = slist;

                    fprintf(fout, "\n        Id             Name   Grade");
                    fprintf(fout, "\n----------   --------------   -----");

                    while(temp != NULL)
                    {
                        printf("\n%10d %16s %7.2f", temp->id, temp->name, temp->grade);
                        temp = temp->next;
                    }fclose(fout);

                    printf("\nAll records have been written to the file.");
                    printf("\nPlease press a button to continue...");
                }
                case 9:
                {    printf("Thanks for using this program. See you later.");
                    ENDED = TRUE;
                }
            }
        }
        else
            printf("Please enter a valid selection.");


    }
}
Basak Oguz
  • 17
  • 7
  • You should include a minimal example that reproduces your error, including the expected output. Also, do not include relevant information in external links. Even better, if it is text, you don't even need a picture. – iled Dec 19 '15 at 15:39
  • I suggest you should use `new` and `delete` instead of `malloc()` and `free()` because you are using C++. – MikeCAT Dec 19 '15 at 15:50
  • C++ has built in `bool` type. Don't use TRUE FALSE macro. `new` and `delete` is preferable over `malloc` family – Danh Dec 19 '15 at 16:07
  • Possible duplicate: [What is an unresolved error and how do I fix it.](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Thomas Matthews Dec 19 '15 at 19:04

2 Answers2

0

You didn't defined AddStudent(int id, char* name). Your define is AddStudent(int id, char name). You should change it to AddStudent(int id, char* name).

Codigo5
  • 19
  • 5
0

Short Answer: You haven't defined a function with name AddStudent that has arguments of the type: (int, char*). Instead you have defined one with arguments of type: (int, char). Note that char* is a string, while char is just a single symbol/byte.

Long answer: You understand that you haven't really wrote a C++ class. In fact you've wrote a structure that contains a person data, created a single instance of that structure under the variable name Student and you have a bunch of functions that work with that structure. That is definitely NOT A C++ CLASS!

Instead you should write something like:

class Person {
public:
    int id;
    char* name;
    float grade;
    Person *p_next;
}

class Students {
public:
    void    addStudent(int id, char* name);
    Person* getStudent(int id);

private:
    Person *p_init;
}

In this example, Person is the underlying structure and Students represent the linked-list handler. If you would like to update a student's grade you could do:

Students students;
students.addStudent(...);
Person *student = students.getStudent(id);
student->grade = 22;
Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
  • 1
    Thank you! The short answer seems to simply solve my problem. I am required to use 'struct' instead of class for this assignment. However, long answer of yours have been very helpful for acknowledge the idea of class. Thank you very much for that one too. – Basak Oguz Dec 19 '15 at 22:28
  • Actually now I'm having another error (EXC BAD ACCESS) in the AddStudent function, for the line, `PredPtr->next = TemPtr`. When I build, no errors occur. Only selection INSERT gives that error. I could not understand why. – Basak Oguz Dec 19 '15 at 23:05
  • I forgot to mention to notify. @ItayGrudev – Basak Oguz Dec 19 '15 at 23:12
  • That is because you are essentially trying to modify unallocated memory. You are running the `FindStudent(&PredPtr, &Something, id);` essentialy on `slist` which is just a pointers to NULL meaning no actual memory. Initialise it like this: `slist = new person;` instead of `NULL`; – Itay Grudev Dec 20 '15 at 03:20
  • Thank you very much, I really appreciate your help. I solved that problem too. However,I don't know why but when I insert new element to the list and I print it on screen/output file, there seem to be a first element with id 0 that I did not add, and two other elements at the end again that I did not add. Between these, I am able to see the elements I actually aded to list. @ItayGrudev – Basak Oguz Dec 20 '15 at 16:03
  • The first element with id zero is the empty element you initialised in the beginning. It's the root element that has it's next pointer to the first element. With other words in `slist = new person;` the `new person` is the first element. If you do `slist->id=1` then the ID will be 1 to that zero/initial/root element. – Itay Grudev Dec 20 '15 at 20:28
  • I understand now. I took your time too much, thank you for all your help @ItayGrudev – Basak Oguz Dec 20 '15 at 21:09