Can someone create a C++ program that does the following with a main and header
ID: 3693403 • Letter: C
Question
Can someone create a C++ program that does the following with a main and header file that does the 4 simple things below and no more than that, Please be in C++ language so basically it display the 4 thing if I press 1 it will prompt me to add a course when i press 2 it will display what i have added if i press 3 it will delete the couse i chose and 4 exits the program. Thats all i need it to do. The program should not end unit the user press quit
Create a linkable class ‘course’, courses have titles. Then implement the following menu to manage it:
1 Add a course
2 Display all courses
3 Delete a course
4 Quit
The user should be allowed to use the menu as many times as they want before quitting. Below is what I have so far
**************************************************************************************'
#include
#include
using namespace std;
#include "Q4.h"
int main()
{
course * Cptr;
int A,a,B,b,C,c;
int i, num;
char enterCourse,deleteCourse,selection;
cout<<"Course"< cout< cout<<"[A] Add a course"< cout<<"[B} Delete a course"< cout<<"[C] Display all course"< cout<<"[D] Quit"<
cin>>selection;
while(selection == 'A'|| selection =='a'||selection=='B'||selection=='b'||selection=='C'||selection=='c')
switch(selection)
{
case 'A'|'a':
cout<<"Enter a course: ";
cin>>enterCourse;
break;
case 'B'|'b':
cout<<"Enter course to delete: ";
cin>>deleteCourse;
break;
case 'C'|'c':
courselist->display();
}
cout<<"GOODBYE"<
return 0;
}
*******************************************************************
class course
{
public:
course(){};
private:
char ADDcourse;
char DELETEcourse;
};
Explanation / Answer
#include<iostream.h>
#include<iomanip.h>
using namespace std;
class course_node
{
unsigned int course_id;
public:
course_node *next;
course_node()
{
course_id = INT_MIN;
next = NULL;
}
void setid(int id)
{
course_id = id;
}
int getid()
{
return course_id;
}
};
class course
{
private:
course_node *head;
public:
course()
{
head = NULL;
}
void Add(int id)
{
course_node *obj = new course_node();
obj->setid(id);
if(head == NULL)
{
head = obj;
}
else
{
obj->next = head;
head = obj;
}
}
int Delete(int id)
{
course_node *temp;
if(head == NULL)
{
cout<<"Empty List";
return 0;
}
if(head->getid() == id)
{
temp = head;
head = head->next;
free(temp);
}
else
{
course_node *prev = head;
while(prev->next->getid() != id && prev->next!=NULL)
prev = prev->next;
// Check if node really exists in Linked List
if(prev->next == NULL)
{
printf(" Given node is not present in Linked List");
return 0;
}
// Remove node from Linked List
prev->next = prev->next->next;
}
return 1;
}
void dispaly()
{
if(head == NULL){
cout <<"List is empty";
return;
}
course_node *temp = head;
while(temp != NULL)
{
cout<<temp->getid()<<" ";
temp = temp->next;
}
}
};
int main()
{
int Courseid;
char selection = ' ';
course *object1 = new course();
cout<<" A. Add a course "<<"B. Delete a course "<<"C. Display all course "<<"D. Quit ";
cin>>selection;
while(toupper(selection) == 'A' || toupper(selection) == 'B' || toupper(selection) == 'C' || toupper(selection) == 'D' )
{
if(toupper(selection) == 'D' )
{
break;
}
switch(selection)
{
case 'A'|'a':
cout<<"Enter a course Id: ";
cin>>Courseid;
object1->Add(Courseid);
break;
case 'B'|'b':
cout<<"Enter course id to delete: ";
cin>>Courseid;
object1->Delete(Courseid);
break;
case 'C'|'c':
object1->dispaly();
break;
}
cout<<" A. Add a course "<<"B. Delete a course "<<"C. Display all course "<<"D. Quit ";
cin>>selection;
}
if(toupper(selection) != 'D' )
{
cout<<"Wrong option selected";
}
system("Pause");
cout<<"GOODBYE";
return 0;
}