1

My homework is about making a schedule with doubly-linked list. We can create a dynamic array for keeping days. But every day has to have a doubly-linked list which contains time slots. Vectors and arrays are forbidden from use, instead of linked lists. I have difficulty about functions.

This is my header file:

#ifndef _SCHEDULE_H
#define _SCHEDULE_H


#include <string>
using namespace std;

struct Node
{
    string courseName;
    int time;
    Node *next;    //forward direction
    Node *prev;    //backward direction

    Node::Node() {}

    Node::Node(const string &cName,const int&time, Node * pRight, Node * pLeft)
        : courseName(cName),time(time),next(pRight), prev(pLeft)
    {}
};

class Schedule
{
public:
    Schedule(); //Constructor

    //adding new course depend on time
    void addCourse(string courseName, char day, int time,Node *Days[6]);

    // delete course depend on time
    void deleteCourse(char day, int time,Node *Days[6]);

    // display a particular course's      time
    void displayCourse(string courseName,Node *Days);

    //prints schedule
    void print(Node *Days);

private:

    Node *head;     //Head node, start of a linked list based on Day
    Node *tail;     //Tail node, end of a linked list based on Day
};

#endif

Here's my implementation file:

#include <iostream>
#include "Schedule.h"

using namespace std;

Schedule::Schedule()
{
    head=new Node(" ",0,NULL,NULL);
    tail = NULL;
}

void Schedule::addCourse(string courseName, char day, int time,Node *Days[6])
{
    int i;

    if (day=='M')
    {i=0;}
    else if(day=='T')
    {i=1;}
    else if(day=='W')
    {i=2;}
    else if(day=='R')
    {i=3;}
    else if(day=='F')
    {i=4;}
    else if(day=='S')
    {i=5;}

    Node*cur=Days[i]->next=head;

    if(Days[i]->next==NULL)
    {
        Days[i]=new Node;
        Days[i]->next->courseName=courseName;
        Days[i]->time=time;
        Days[i]->next=NULL;
        Days[i]->prev=NULL;

        cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
    }

    else if(time<Days[i]->next->time && time!=Days[i]->next->time)
    {
        Node*newcourse=new Node;
        //Days[i]=new Node;
        Days[i]->next->courseName=courseName;
        Days[i]->next->time=time;
        Days[i]->next=head;
        Days[i]->prev=NULL;
        Days[i]->next=newcourse;

        cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
    }

    else if(time>Days[i]->next->time)
    {

        while(Days[i]->next!=NULL && Days[i]->next->time<time && Days[i]->next->time!=time)
        {
            Days[i]->next=Days[i]->next->next;
        }

        if(Days[i]->next->time==time)
        {
            cout<<"Time conflict"<<endl;
        }
        else
        {
            Node*newcourse=new Node;
            Days[i]->next->courseName=courseName;
            Days[i]->next->time=time;
            Days[i]->next=Days[i]->next->next;
            Days[i]->prev=Days[i]->next;
            Days[i]->next->next=newcourse;

            cout<<"The course "<<courseName<<" is added on "<<day<<" "<<time<<endl;
        }
    }
}

void Schedule::deleteCourse(char day, int time,Node *Days[6])
{
    int d;

    if (day=='M')
    {d=1;}
    else if(day=='T')
    {d=1;}
    else if(day=='W')
    {d=2;}
    else if(day=='R')
    {d=3;}
    else if(day=='F')
    {d=4;}
    else if(day=='S')
    {d=5;}

    Node*cur=Days[d]->next=head;

    if(Days[d]->next==NULL)
    {
        cout<<"Schedule is empty for this day"<<endl;
    }
    else
    {
    }
}

void Schedule::displayCourse(string courseName,Node *Days)
{
}

void Schedule::print(Node *Days)
{
}

Here is my main:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Schedule.h"

using namespace std;

Node *Days = new Node[6];

void CoutSelection()
{
    cout<<endl<<endl;

    cout<<"Welcome to Schedule Maker. Please select an option:"<<endl;
    cout<<" 1) Load the course schedule from a known file"<<endl;
    cout<<" 2) Add a time slot manually"<<endl;
    cout<<" 3) Remove a time slot manually"<<endl;
    cout<<" 4) Print a particular course's time slot"<<endl;
    cout<<" 5) Print all schedule"<<endl;
    cout<<" 6) Exit" <<endl;
    cout<<endl;
    cout<<" Please enter your selection as 1-2-3-4-5-6"<<endl;
    cout<<endl;
}

int main()
{
    int selection;
    CoutSelection();
    cin>>selection;
    Schedule list;

    while (selection!=6)
    {
        if (selection==1)
        {   string fileName;
            cout<<"Please enter the filename that you want to load"<<endl;
            cin>>fileName;

            ifstream input;
            input.open(fileName);//open file

            if(!input.is_open())//control if correctly open
            {
                cout<<"Couldn't open input file: "<<fileName<<endl;
            }
            else
            {
                string cname,line; //course name and day identifier
                char day;
                int time; //time
                while(!input.eof())
                {getline(input, line);
                    stringstream ss(line);
                    int num;
                    ss>>cname>>day>>num;

                    list.addCourse(cname,day,time,*Days[6]);
                }
            }
        }

        else if (selection==2)
        {
            int timeAdded;
            string cnameAdded;
            char dayAdded;

            cout<<"Please enter course name,day and it's time that you want to add like   : coursename dayidentifier time"<<endl;
            cout<<"Enter the day as M/T/W/R/F/S. (MONDAY:M, TUESDAY:T, WEDNESDAY:W, THURSDAY:R, FRIDAY:F, SATURDAY:S)"<<endl;
            cin>>cnameAdded>>dayAdded>>timeAdded;

            list.addCourse(cnameAdded,dayAdded,timeAdded,*Days[6]);
        }
        else if(selection==3)
        {
            char dayDeleted;
            int timeDeleted;
            cout<<"Please enter the day and time that you want to delete like : dayidentifider time"<<endl;
            cout<<"Enter the day as M/T/W/R/F/S. (MONDAY:M, TUESDAY:T, WEDNESDAY:W, THURSDAY:R, FRIDAY:F, SATURDAY:S)"<<endl;
            cin>>dayDeleted>>timeDeleted;
            list.deleteCourse(dayDeleted,timeDeleted,*Days[6]);
        }
        else if(selection==4)
        {
            string coursedisplayed;
            cout<<"Please enter course name that you want to display"<<endl;
            cin>>coursedisplayed;

            list.displayCourse(coursedisplayed,*Days);
        }
        else if(selection==5)

        {
            list.print(*Days);
        }

        CoutSelection();
        cin>>selection;

    }
    return 0;

}

What is wrong with my code? If I handle one of the functions, I'm sure I can do other functions.

Errors :

error C2664: 'Schedule::addCourse' : cannot convert parameter 4 from 'Node' to 'Node *[]'

IntelliSense: no operator "*" matches these operands operand types are: * Node

5
  • What are you asking what is wrong with it? Does it not compile? Or does it compile, but fail to run to completion? Or does it run, but produce the wrong output? Please add specifics and any error messages you are seeing. Commented Mar 18, 2014 at 20:39
  • But you are using arrays so apparently you have violated the rules of the assignment. Commented Mar 18, 2014 at 20:45
  • This: if(Days[i]->next->time==time) is not going to end well if this: Days[i]->next!=NULL was the reason for the prior while loop exiting. Your code can, and probably will, invoke undefined behavior. Commented Mar 18, 2014 at 20:55
  • I think problem is passing array to functions IntelliSense: no operator "*" matches these operands operand types are: * Node Commented Mar 18, 2014 at 22:08
  • You have considerably more problems than that. Your Days array is global, dynamically allocated and invoke the default Node() constructor for each new node when created. But your constructor never initializes any of its members. And your sentinel nodes aren't helping any, and in fact are ultimately useless. I honestly have a hard time believing this much code would/could be written without testing at least some of it isolation before integration into your project. Commented Mar 18, 2014 at 22:33

1 Answer 1

1

Aside from all the problems presented by @WhozCraig, which I think you should tackle for your own good. Your compiler is talking to you, and it is telling you that your addCourse method receives a pointer to a Node Array.

But in your main you called it with the following list.addCourse(cname,day,time,*Days[6]);. By doing *Days[6] you are telling the method you want to send what is pointed by Days[6]. Thus your compiler is receiving a Node object and not a pointer to a node array.

Try it with the following list.addCourse(cname,day,time,Days);, this will send the pointer to the first element in days.

One pointer to keep in mind, which you'll teacher will likely notice:

  1. You have memory leaks, which is another VERY important subject.
Sign up to request clarification or add additional context in comments.

3 Comments

He specified in the question that vectors were forbidden from use in the project (probably because the focus of the assignment is to work with linked lists, and thus, pointers).
Oh, sorry about that, yeah I had assignments like these once
Yeah, I've made comments in other threads about assignments like these. I can understand where the instructor is trying to force you to learn certain concepts, but often times it just leads to bad programming habits rather than anything else. And I agree with you, I've had plenty of assignments like this as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.