Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[C++] I am trying to implement a ToDo list where each node contains a task and d

ID: 3598749 • Letter: #

Question

[C++] I am trying to implement a ToDo list where each node contains a task and desc of task. My set and get function worked until I moved the char taskName and taskDesc into struct Node. Before I create another class for Queue link list, I would like to have a working class Item. Also, although I don't have a Queue link list ready, If I wanted to make a pointer to type Item (class Item), how would I do so?

HEADER FILE
#ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED
class Item{
private:
    struct node{
        char taskName[10];
        char taskDesc[80];
        node* next;
    };
node* head;
node* curr;
node* temp;
public:
    Item();
    void addNode(char addTask[],char addDesc[]);
    void deleteNode(char deleteTask[],char deleteDesc[]);
    void printList();
    void getTask();
void getDesc();
void setTask(char text[]);
void setDesc(char text[]);
    char taskName[10];
    char taskDesc[80];
};
#endif // ITEM_H_INCLUDED

.CPP FILE
#include "Item.h"
#include
#include
using namespace std;
Item::Item(){
    head = NULL;
    curr = NULL;
    temp = NULL;
}
void Item::addNode(char addTask[],char addDesc[]){
    node* n = new node;
    n->next = NULL;
    for (int i =0; i<10; i++){
      n->taskName[i]=addTask[i];
      n->taskDesc[i]=addDesc[i];
    }
    if (head != NULL){
        curr = head;
        while(curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = n;
    } else {
        head = n;
    }
}
void Item::deleteNode(char deleteTask[], char deleteDesc[]){
    node* d = NULL;
    temp = head;
    curr = head;
    while ((curr != NULL) ){//&&
          // ((curr->taskName) != d) &&
          // ((curr->&taskDesc) != d)) {
        temp = curr;
        curr = curr->next;
        }
    if (curr == NULL){
        cout << d << "was not in the list ";
        delete d;
    } else {
        d = curr;
        curr= curr->next;
        temp->next = curr;
        delete d;
        cout << d << "was deleted. ";
    }
}
void Item::printList(){
    curr = head;
    while (curr != NULL){
            for(int i = 0; i<10;i++){
        cout << "Subject: " << curr->taskName[i] <<" Description: "<< curr->taskDesc[i] <         curr = curr->next;
            }
    }
}
/*
void Item::getTask()//gets the name of the task from the user
{
cout << " Subject:";
cin.getline(taskName, 10, ' ');
cout << taskName << endl;
}
void Item::getDesc() //gets the description
{
cout << "Description: ";
cin.getline(taskDesc, 80, ' ');
cout << taskDesc << endl;
}
void Item::setTask(char* text){
    for(int i = 0; i < 10; i++){
        text[i];
    }
}
void Item::setDesc(char* text){
    for(int i = 0; i<80; i++){
        text[i];
    }
}
*/
I wanted to use this http://www.includehelp.com/code-snippets/implementation-of-queue-using-linked-list-in-cpp.aspx for queue implementation.

Explanation / Answer

//I modified ur program ,, just make sure u give subject and description not exceeds your array size for taskName and taskDesc ,, otherwise results are unpridicted .. you can find my changes with keyword CheggEA

//Item.h , remains same

//Items.cpp,search my changes with CheggEA

#include "Item.h"

#include<iostream>

//Added string.h for strcpy,CheggEA

#include<string.h>

using namespace std;

Item::Item(){

head = NULL;

curr = NULL;

temp = NULL;

}

void Item::addNode(char addTask[], char addDesc[]){

node* n = new node;

n->next = NULL;

//CheggEA , use strlen instead of just numbers or strcpy

/*for (int i = 0; i<10; i++){

n->taskName[i] = addTask[i];

n->taskDesc[i] = addDesc[i];

}*/

strcpy( n->taskName,addTask);

strcpy(n->taskDesc, addDesc);

//End of change ChegEA

if (head != NULL){

curr = head;

while (curr->next != NULL) {

curr = curr->next;

}

curr->next = n;

}

else {

head = n;

}

}

void Item::deleteNode(char deleteTask[], char deleteDesc[]){

node* d = NULL;

temp = head;

curr = head;

while ((curr != NULL)){//&&

// ((curr->taskName) != d) &&

// ((curr->&taskDesc) != d)) {

temp = curr;

curr = curr->next;

}

if (curr == NULL){

cout << d << "was not in the list ";

delete d;

}

else {

d = curr;

curr = curr->next;

temp->next = curr;

delete d;

cout << d << "was deleted. ";

}

}

void Item::printList(){

curr = head;

while (curr != NULL){

//for (int i = 0; i<10; i++){

//CheggEA here compilation error ,, rectified compilation error

//cout << "Subject: " << curr->taskName[i] << " Description: " << curr->taskDesc[i] << endl;,, printing whole string instead of each char

cout << "Subject: " << curr->taskName << " Description: " << curr->taskDesc << endl;

curr = curr->next;

}

}

//CheggEA

void Item::getTask()//gets the name of the task from the user

{

cout << " Subject:";

cin.getline(taskName, 10, ' ');

cout << taskName << endl;

}

void Item::getDesc() //gets the description

{

cout << "Description: ";

cin.getline(taskDesc, 80, ' ');

cout << taskDesc << endl;

}

void Item::setTask(char* text){

//instead of copying char by char using for , you can use strcpy lib function,or use strlen of text

/*for(int i = 0; i < 10; i++){

text[i];

}*/

//use either for or strcpy ,CheggEA

for (int i = 0; i < strlen(text); i++)

taskName[i] = text[i];

//strcpy(taskName, text);

}

void Item::setDesc(char* text){

//instead of copying char by char using for , you can use strcpy lib function or use strlen in for loop ,cheggEA

/*for(int i = 0; i<80; i++){

text[i];

}*/

//use either for or strcpy ,CheggEA

for (int i = 0; i < strlen(text); i++)

taskDesc[i] = text[i];

//strcpy(taskDesc, text);

}

--------------------------------------

//main driver file to test,,here I have given how u can declare pointer to Item class ,, search with keyword ChegEA

#include"Item.h"

int main()

{

Item itm;

itm.getTask();

itm.getDesc();

itm.addNode(itm.taskName, itm.taskDesc);

itm.getTask();

itm.getDesc();

itm.addNode(itm.taskName, itm.taskDesc);

itm.getTask();

itm.getDesc();

itm.addNode(itm.taskName, itm.taskDesc);

itm.getTask();

itm.getDesc();

itm.addNode(itm.taskName, itm.taskDesc);

itm.printList();

//declare pointer to Item class,CheggEA

Item *itemPtr;

}

--------------------------------------------------------------------------

//output

Subject:Maths
Maths
Description: test Maths
test Maths


Subject:Science
Science
Description: Test science
Test science


Subject:Moral
Moral
Description: test moral
test moral


Subject:English
English
Description: test English
test English
Subject: Maths
Description: test Maths
Subject: Science
Description: Test science
Subject: Moral
Description: test moral
Subject: English
Description: test English

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote