[C++] The program works but not in the way I want it to. I am trying to get my p
ID: 3600699 • Letter: #
Question
[C++] The program works but not in the way I want it to. I am trying to get my program output to look like the picture I posted. In my program, the menu keeps repeating 3x and I don't know how I can add family member separately while at the same time, give them toDo tasks later on...
[Queue.cpp]
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
using namespace std;
class Queue{
private:
struct Node {
char *name;
Node* next;
};
public:
Node *front, *rear;
Queue();
~Queue();
void enqueue(char*);
void dequeue(char*);
void display();
char name[10];
char menu();
};
#endif // QUEUE_H_INCLUDED
[Queue.cpp]
#include "Queue.h"
#include "Item.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
Queue::Queue(){
front = NULL;
rear = NULL;
}
Queue::~Queue(){
while(front!=NULL){
Node *temp=front;
front=front->next;
delete temp;
}
rear = NULL;
}
void Queue::enqueue(char *n){
Node* temp= new Node;
if (temp == NULL){
cout <<"List is full"<<endl;
return;
}
cout << "Enter family member's name: ";
cin.getline(name, 10, ' ');
temp->name = n;
temp->next = NULL;
/*
if (cin >> '3'){
Item itemA;
itemA.getTask();
itemA.getDesc();
itemA.setTask(itemA.taskName);
itemA.setDesc(itemA.taskDesc);
itemA.addNode(itemA.taskName,itemA.taskDesc);
}
*/
if (front== NULL){
front = rear;
rear = temp;
} else {
rear-> next = temp;
rear = temp;
}
cout << n << endl;
}
void Queue::dequeue(char *d){
if (front == NULL){
cout <<"There is nothing to remove." << endl;
return;
}
cout << front->name<<"was removed from the list" << endl;
if (front == rear){
front = rear;
rear = NULL;
} else {
front = front->next;
}
}
void Queue::display(){
if(front ==NULL){
cout << "Your To-Do list is empty." << endl;
return;
}
Node *temp=front;
while(temp){
cout << temp -> name << " ";
temp = temp-> next;
Item itemB;
itemB.printList();
}
cout << endl;
}
char Queue::menu(){
char choice;
cout << "Make your selection below: ";
cout << "1. Add family members by name. ";
cout << "2. View all TODO lists. ";
cout << "3. Add TODO item. ";
cout << "4. Complete a TODO item. ";
cout << "5. Exit ";
cin >> choice;
return choice;
}
[Item.h]
#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
[Item.cpp]
#include "Item.h"
#include <stdlib.h>
#include <iostream>
#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;
strcpy(n->taskName,addTask);
strcpy(n->taskDesc,addDesc);
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) ){
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){
cout << "Subject: " << curr->taskName <<" Description: "<< curr->taskDesc <<endl;
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 < strlen(text); i++){
text[i];
}
}
void Item::setDesc(char* text){
for(int i = 0; i< strlen(text); i++){
text[i];
}
}
[main.cpp]
#include <iostream>
#include "Item.h"
#include "Queue.h"
using namespace std;
int main()
{
char choice;
do {
Queue A;
Item B;
choice = A.menu();
switch(choice){
case '1':
A.enqueue(A.name);
break;
case '2': A.display();
break;
case '3': B.addNode(B.taskName,B.taskDesc);
break;
case '4': B.deleteNode(B.taskName,B.taskDesc);
break;
default: cout << "System exit ";
}
}while(choice != '5');
/*
Item itemA;
itemA.getTask();
itemA.getDesc();
itemA.setTask(itemA.taskName);
itemA.setDesc(itemA.taskDesc);
itemA.addNode(itemA.taskName,itemA.taskDesc);
itemA.deleteNode(itemA.taskName,itemA.taskDesc);
itemA.printList();
Item *itemPtr;*/
//Queue fam;
//fam.menu();
//fam.enqueue(fam.name);
return 0;
}
Explanation / Answer
-Your program is correct just the thing is, in this program you have taken do-while loop. I think this is making trouble for you so please replace the do-while loop by a while loop.
-So First of all it will display you the menu to select then after that you can choose your option out of the 5.
-When you select to add a family member then it will ask for the name, after that if you will show you the member is added. After this it will again show you the menu that time **its not the the new list it will operate on the same list. So dont misunderstand it that the output is for a new execution.
-**If now you add list using option '3' then its for the member you have entered by option '2'.
[main.cpp]
#include <iostream>
#include "Item.h"
#include "Queue.h"
using namespace std;
int main()
{
cout<<"Welcome to TODO List Manager! ";
char choice= A.menu();
while(choice != '5')
{
Queue A;
Item B;
switch(choice)
{
case '1':
A.enqueue(A.name);
break;
case '2': A.display();
break;
case '3': B.addNode(B.taskName,B.taskDesc);
break;
case '4': B.deleteNode(B.taskName,B.taskDesc);
break;
default: cout << "System exit ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.