Feb 26, 2020 at 8:47pm UTC
Hello, I am looking for help on my program. I want to have one iteration in my program for "Courses for Jacob: " at the top of my output and I was hoping to get feedback on what changes I could make to get this outcome. Below is my code.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct node
{
private:
string fullName;
int courseNumber;
string courseName;
string courseRoom;
node *next;
public:
string getFullName(){
return fullName;
}
void setFullName(string Full){
fullName = Full;
}
string getCourseName(){
return courseName;
}
void setCourseName(string name){
courseName = name;
}
int getCourseNumber(){
return courseNumber;
}
void setCourseNumber(int number){
courseNumber = number;
}
string getCourseRoom(){
return courseRoom;
}
void setCourseRoom(string room){
courseRoom = room;
}
void setNext(struct node * nextNode){
next = nextNode;
}
struct node * getNext(){
return next;
}
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(struct node newNode)
{
struct node *tmp = new struct node;
tmp->setFullName(newNode.getFullName());
tmp->setCourseName(newNode.getCourseName());
tmp->setCourseNumber(newNode.getCourseNumber());
tmp->setCourseRoom(newNode.getCourseRoom());
tmp->setNext(NULL);
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->setNext(tmp);
tail = tail->getNext();
}
}
struct node *getHead(){
return head;
}
struct node *getTail(){
return tail;
}
};
int main()
{
struct node record;
record.setFullName("Jacob Hallum");
record.setCourseName("C++ Programming");
record.setCourseNumber(31613);
record.setCourseRoom("SHIM 237");
linked_list a;
a.add_node(record);
struct node record2;
record2.setFullName("Jacob Hallum");
record2.setCourseName("Calculus 2");
record2.setCourseNumber(31525);
record2.setCourseRoom("SCMA 271");
a.add_node(record2);
struct node record3;
record3.setFullName("Jacob Hallum");
record3.setCourseName("Princ Physics: Mechanics");
record3.setCourseNumber(31296);
record3.setCourseRoom("SCMA 217");
a.add_node(record3);
struct node record4;
record4.setFullName("Jacob Hallum");
record4.setCourseName("Intro to Psychology");
record4.setCourseNumber(30297);
record4.setCourseRoom("Online");
a.add_node(record4);
struct node * head = a.getHead();
while(head !=NULL){
cout<<"Course for: "<<head->getFullName()<<"\n";
cout<<"Course Number: "<<head->getCourseNumber()<<"\n";
cout<<"Course Name: "<<head->getCourseName()<<"\n";
cout<<"Course Room: "<<head->getCourseRoom()<<"\n\n";
head=head->getNext();
}
string name = head->getCourseName();
cout<<name<<endl;
return 0;
}
The output I have for my program is:
Course for: Jacob Hallum
Course Number: 31613
Course Name: C++ Programming
Course Room: SHIM 237
Course for: Jacob Hallum
Course Number: 31525
Course Name: Calculus 2
Course Room: SCMA 271
Course for: Jacob Hallum
Course Number: 31296
Course Name: Princ Physics: Mechanics
Course Room: SCMA 217
Course for: Jacob Hallum
Course Number: 30297
Course Name: Intro to Psychology
Course Room: Online