Can someone create a C++ program that does the following with a main and header
ID: 3693340 • 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.
Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
void printMenu();
int main()
{
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
string inputInfo;
string courseNumber, courseTitle;
int index;
string line;
bool success = false;
// instantiate a Linked List object
LinkedList * list1 = new LinkedList();
printMenu();
do // will ask for user input
{
cout << "What action would you like to perform? ";
cin.get(input1);
input1 = toupper(input1);
cin.ignore(20, ' '); //flush the buffer
// matches one of the case statement
switch (input1)
{
case 'A': //Add Course
cout << "Please enter some course information: ";
cout << "Enter a course number: ";
getline(cin, courseNumber);
cout << "Enter a course title: ";
getline(cin, courseTitle);
cout << "Enter an index to add: ";
cin >> index;
cin.ignore(20, ' '); //flush the buffer
success = list1->addElement(courseNumber, courseTitle, index);
if (success == true)
cout << "The course " << courseNumber << " with title " << courseTitle << " added ";
else
cout << "The index is out of bounds ";
break;
case 'D': //Display Courses
list1->printList();
break;
case 'Q': //Quit
delete list1;
break;
case 'R': //Remove Course
cout << "Please enter a course to remove: ";
cout << "Enter a course number: ";
getline(cin, courseNumber);
cout << "Enter a course title: ";
getline(cin, courseTitle);
success = list1->removeElement(courseNumber, courseTitle);
if (success == true)
cout << "The course " << courseNumber << " with title " << courseTitle << " removed ";
else
cout << "The course " << courseNumber << " with title " << courseTitle << " does not exist ";
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action ";
break;
}
} while (input1 != 'Q');
return 0;
}
/** The method printMenu displays the menu to a user**/
void printMenu()
{
cout << "Choice Action ";
cout << "------ ------ ";
cout << "A Add Course ";
cout << "D Display Courses ";
cout << "Q Quit ";
cout << "R Remove Course ";
cout << "? Display Help ";
}
LinkedList.h
#include <iostream> //to use cout
#include <iomanip> //to format output
#include <string> //to use strings
using namespace std;
//struct Course represents some course information
struct Course
{
string number;
string title;
struct Course * next;
};
//class LinkedList will contains a linked list of courses
class LinkedList
{
private:
struct Course * head;
int size = 0;
public:
LinkedList();
~LinkedList();
bool addElement(string number, string title, int index);
bool removeElement(string number, string title);
void printList();
};
//Constructor to initialize the linked list
LinkedList::LinkedList()
{
head = NULL;
}
//Destructor
//Description: ...... to be completed
LinkedList::~LinkedList()
{
Course *curr, *temp;
curr = head;
temp = head;
int count=0;
while (curr != NULL)
{
curr = curr->next;
delete temp;
temp = curr;
count++;
}
cout << "The number of deleted courses is: " << count << " ";
}
//Description: Adds a node to the linked list.
bool LinkedList::addElement(string newNumber, string newTitle, int index)
{
Course *temp = new Course;
struct Course *temp2;
temp->number = newNumber;
temp->title = newTitle;
temp->next = NULL;
if (index >= (size + 1) || index < 0)
{
return false;
}
if (head == NULL)
{
head = temp;
}
else
{
temp2 = head;
if (index == 0)
{
temp->next = head;
head = temp;
}
else
{
for (int i = 1; i < index; i++)
{
temp2 = temp2->next;
}
temp->next = temp2->next;
temp2->next = temp;
}
}
size++;
return true;
}
//Description: Removes node from the linked list.
bool LinkedList::removeElement(string someNumber, string someTitle)
{
struct Course *temp, *temp2;
temp = head;
if (head == NULL)
{
return false;
}
if (head->number.compare(someNumber) == 0 && head->title.compare(someTitle) == 0)
{
head = head->next;
delete temp;
size--;
return true;
}
else if (head->next == NULL)
{
return false;
}
temp = head->next;
temp2 = head;
while (temp->next != NULL)
{
if (temp->number.compare(someNumber) == 0 && temp->title.compare(someTitle) == 0)
{
temp2->next = temp->next;
delete temp;
size--;
return true;
}
else
{
temp2 = temp2->next;
temp = temp->next;
}
}
if (temp->number.compare(someNumber) == 0 && temp->title.compare(someTitle) == 0)
{
temp2->next = NULL;
delete temp;
size--;
return true;
}
else
return false;
cout << "Whoops";
}
//Description: Prints the linked list for the user to see.
void LinkedList::printList()
{
struct Course *temp;
temp = head;
while (temp != NULL)
{
cout << "Course Number: " << temp->number << ", Course Title: " << temp->title << " ";
temp = temp->next;
}
}
sample output
Choice Action
------ ------
A Add Course
D Display Courses
Q Quit
R Remove Course
What action would you like to perform?
A
Please enter some course information:
Enter a course number:
123
Enter a course title:
java
Enter an index to add:
12
The index is out of bounds
What action would you like to perform?
Q
The number of deleted courses is: 0