I'm working on this linked list project where I'm supposed to save the employee data in a linked list and apply some operations.
and I am new with c++ so I'll show you my code and please help me with it.
my questions are:
1- whenever I enter a full name the program keeps going in an infinite loop! i used getline() method and still its not working. but if I enterd just the first name it works fine.
2- I don't know how I can display or find the employee information if I used the methods I create it gives me random data.
my code:
main:
#include<iostream>
#include<string>
#include <fstream>
#include "EmployeeList.cpp"
using namespace std;
int main(){
string name;
int choice, id;
double Salary;
EmployeeList* employee = new EmployeeList();
cout<<"***********************************************"<<endl;
cout<<"**********EMPLOYEE MANAGEMENT SYSTEM***********"<<endl;
cout<<"***********************************************\n\n"<<endl;
do{
cout<<" 1--->Press 1 to INSERT EMPLOYMENT:"<<endl;
cout<<" 2--->Press 2 to FIND EMPLOYMENT INFO ID:"<<endl;
cout<<" 3--->Press 3 to DELETE EMPLOYMENT ID:"<<endl;
cout<<" 4--->Press 4 to SEARCH HIGHEST SALARY:"<<endl;
cout<<" 5--->Press 5 to DISPLAY ALL EMOLYMENTS:"<<endl;
cout<<" 6--->Press 6 to REVERSE DISPLAY ALL EMPLOYMENTS:"<<endl;
cout<<" 7--->Press 7 to EXIT:"<<endl;
cout<<"\n Enter Your Choice: ";
cin>>choice;
switch(choice){
case 1:
cout<<endl;
cout<<"Enter name : ";
getline(cin,name);
cout<<"Enter ID : ";
cin>>id;
cout<<"Enter Salary : ";
cin>>Salary;
cout<<endl;
employee ->INSERTEMPLOYMENT(name,id,Salary);
break;
case 2:
cout<<endl;
cout<<"Enter ID : ";
cin>>id;
cout<<endl;
employee->FINDEMPLOYMENTINFOID(id);
break;
case 3:
cout<<endl;
cout<<"Employee ID : ";
cin>>id;
cout<<endl;
employee-> DELETEEMPLOYMENTID(id);
break;
case 4:
cout<<endl;
employee->SEARCHHIGHESTSALARY();
break;
case 5:
cout<<endl;
employee->DISPLAYALLEMPLOYMENTS();
break;
case 6:
cout<<endl;
employee->REVERSEDISPLAYALLEMPLOYMENTS(employee->getHead());
break;
case 7:
exit(0);
default:
cout<<"Invalid choice."<<endl;
break;
}
} while (true);
return 0;
}
Employee:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Employee{
private:
string name;
int ID;
double Salary;
Employee* next;
public:
//Constructor
Employee (string n, int id, double s){
n = name;
id = ID;
s = Salary;
this->next = NULL;
}
//Setters
void setName(string n){
n = name;
}
void setID(int id){
id = ID;
}
void setSalary(double s){
s = Salary;
}
void setNext(Employee* next){
this->next = next;
}
//Getters
string getName(){
return name;
}
int getID(){
return ID;
}
double getSalary(){
return Salary;
}
Employee* getNext(){
return this->next;
}
};
EmployeeList:
#include "Employee.cpp"
using namespace std;
class EmployeeList{
private:
Employee* head;
public:
//Constructor
EmployeeList(){
head = NULL;
}
//getter
Employee* getHead(){
return head;
}
//insert method
void INSERTEMPLOYMENT (string name, int id, double Salary){
Employee* newnode = new Employee(name, id, Salary);
if(head == NULL){
head = newnode;
}
else {
Employee* temp = head;
while(temp->getNext() != NULL){
temp = temp->getNext();
}
temp->setNext(newnode);
}
cout<<"The employee record was added successfully."<<endl;
cout<<"------------------------------------------"<<endl;
}
//info
void FINDEMPLOYMENTINFOID (int id){
Employee *temp = head;
bool status = false;
while(temp != NULL){
if(temp->getID() == id){
status = true;
cout<<"Employee Name = "<<temp->getName()<<endl;
cout<<"Employee ID = "<<temp->getID()<<endl;
cout<<"Employee Salary = "<<temp->getSalary()<<endl;
cout<<"---------------------------------------"<<endl;
}
temp = temp->getNext();
}
if(status == false) {
cout<<"Employee is not found in the list."<<endl;
cout<<"--------------------------------------------"<<endl;
}
}
//display
void DISPLAYALLEMPLOYMENTS (){
Employee* temp = head;
while(temp != NULL){
cout<<"Employee Name = "<<temp->getName()<<endl;
cout<<"Employee ID = "<<temp->getID()<<endl;
cout<<"Employee Salary = "<<temp->getSalary()<<endl;
cout<<"---------------------------------------"<<endl;
temp = temp->getNext();
}
}
};
There are 2 major problems here.
Assignment are not reversible...
In Employee class most assignment are reversed. The constructor is:
//Constructor
Employee (string n, int id, double s){
n = name; // replace the local copy of n with name!!!
id = ID;
s = Salary;
this->next = NULL;
}
It should of course be:
//Constructor
Employee(string n, int id, double s) {
name = n;
ID = id;
Salary= s;
this->next = NULL;
}
This is the reason why your Employee instances only contain garbage values...
Mixing getline and stream extractor is dangerous
When you use a stream extractor to read an integer value, the stream pointer is positioned immediately after the last number, so just before the end of line. If you use getline immediately after that, you will read that newline character only and will get an empty string. A simple trick is to consistently skip over empty lines:
case 1:
cout << endl;
cout << "Enter name : ";
for (;;) {
getline(cin, name);
if (name != "") break;
}
...
That should be enough to fix the immediate problems. But others are still around:
you never test the state of the input streams: if the users types an incorrect number you will not detect it and your program will have a wrong behaviour
EmployeeList being a linked list should behave as a container: it should automatically destroys its elements in its destructor. And because of that and of the rule of 5, it should also have a custom (or deleted) copy constructor and copy assignment operator.
constructor have a special syntax to directly initialize their members instead of first default initialize them and then assign to them. Not only it is slightly more efficient, but it is the only way to initialize const members. For that reason the idiomatic way should be:
//Constructor
Employee (string n, int id, double s)
: name(n), ID(id), Salary(s), next(nullptr) {}
Related
im trying to implement both queue and stack in my mini project. Customer will enter nodes into stack and the seller will sort the nodes from stack and put them into queue. I want to sort nodes from a stack based on college name, where all nodes with college = "kdoj" will get into the queue first then followed by all nodes with college = "kdse". The problem is i have tried doing the sorting with 2 (for loop) and i dont know why only 2 nodes get into the queue when i have more than 2 nodes in the stack. i am not sure where is the problem and been stuck here for a while :(.
This is the input example:
name : david
ic num: 123
phone num: 123
college: kdse
quantity: 1
size: m
name : jojo
ic num: 123
phone num: 123
college: kdse
quantity: 1
size: m
name : zoro
ic num: 123
phone num: 123
college: kdoj
quantity: 1
size: m
This is the runnable code.
The problem will occur when you sort the list in seller menu after entering the data.
#include <iostream>
#include <cstring>
#include<iomanip>
#include <conio.h>
#include<string>
using namespace std;
//nodes
struct customerNode {
string name, id, college;
int phoneNum,quantity;
char size;
public:
customerNode* next;
customerNode(string name, string id, int phoneNum,string college, int quantity, char size)
{
name = name;
id = id;
phoneNum = phoneNum;
college = college;
quantity = quantity;
size = size;
next = NULL;
}
};
//class customer
class Customer{
public:
customerNode *head = NULL;
bool isEmpty(){
return(head == NULL);
}
void pushStack(string name, string id, int phoneNum, string college, int quantity, char size){
customerNode *newnode = new customerNode(name,id,phoneNum,college,quantity,size);
newnode->name = name;
newnode->id= id;
newnode->phoneNum = phoneNum;
newnode->college = college;
newnode->quantity = quantity;
newnode->size = size;
if(isEmpty()){
newnode->next = head;
head = newnode;
cout<<endl<<"created new stack."<<endl;
}
else{
newnode->next = head;
head = newnode;
cout<<endl<<"added node into a stack."<<endl;
}
}
//pop out node from stack
void popStack(){
customerNode *temp;
temp = head; //temp variable
head = temp->next;
temp->next = NULL;
cout<<endl<<"Order has been deleted!";
}
//push node into stack
void enterOrder(){
int phoneNum, quantity;
char size;
string name, id, college;
cout<<endl<<"\tADDING YOUR ORDER.\n";
cout<<"\tName: ";
cin.ignore();
getline(cin,name);
cout<<"\tIC Num: ";
cin>>id;
cout<<"\tPhone Number: ";
cin>>phoneNum;
cout<<"\tCollege name: ";
cin>>college;
cout<<"\tQuantity: ";
cin>>quantity;
cout<<"\tSize (S/M/L/XL): ";
cin>>size;
pushStack(name, id, phoneNum,college, quantity, size);
cin.get();
}
//see order and trigger popout funct
void deleteorder(){
char choice;
cout<<"YOUR ODER."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
stackTop();
cout<<endl;
cout<<endl<<"Confirm to delete?: ";
cin>>choice;
if(choice == 'y' || choice == 'Y'){
popStack();
}
}
//display node at the top
void stackTop(){
customerNode *check;
if(!isEmpty()){
check = head;
cout<<endl<<
setw(10)<<left<<check->id
<<setw(20)<<left<<check->name
<<setw(10)<<left<<check->phoneNum
<<setw(20)<<left<<check->college
<<setw(10)<<left<<check->quantity
<<setw(5)<<left<<check->size;
}
else{
cout<<endl<<"Stack is an underflow.";
getch();
}
}
int getNumberNodes(){
customerNode *check;
check = head;
int counter = 0;
while(check!= NULL){
counter++;
check = check->next;
}
return counter;
}
};
//Customer main menu
void customerMenu(Customer &c){
int choice;
do{cout
<<endl<<"CUSTOMER MENU"
<<endl<<"Please choose your task."
<<endl<<"1. Insert Order"
<<endl<<"2. Delete Order"
<<endl<<"3. Check Order"
<<endl<<"4. Exit"
<<endl<<"Your Choice: ";
cin>>choice;
//customer task
switch(choice){
case 1: c.enterOrder();
break;
case 2: c.deleteorder();
break;
case 3:
cout<<"\nYOUR ODER."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
c.stackTop();
cout<<endl;
break;
}
}while(choice != 4);
};
//SELLER CLASS
class Seller{
string id;
string password;
public:
customerNode *backPtr, *frontPtr;
//constructor seller
Seller(){
id = "12345";
password = "12345";
backPtr = NULL;
frontPtr = NULL;
}
//check queue empty
bool queueEmpty(){
return (backPtr == NULL && frontPtr == NULL);
}
//display all nodes in stack
void getStack(Customer &c){
customerNode *check;
if(!c.isEmpty()){
check = c.head;
while(check!=NULL){
cout<<endl<<
setw(10)<<left<<check->id
<<setw(20)<<left<<check->name
<<setw(10)<<left<<check->phoneNum
<<setw(20)<<left<<check->college
<<setw(10)<<left<<check->quantity
<<setw(5)<<left<<check->size;
check=check->next;
}
}
else{
cout<<endl<<"Stack is an underflow.";
getch();
}
}
//will trigger getStack()
void displayStack(Customer &c){
cout<<"\nCUSTOMER FULL ORDERLIST."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
getStack(c);
cout<<endl;
}
//display all nodes in queue
void getQueue(){
customerNode *check;
if(!queueEmpty()){
check = frontPtr;
while(check!=NULL){
cout<<endl<<
setw(10)<<left<<check->id
<<setw(20)<<left<<check->name
<<setw(10)<<left<<check->phoneNum
<<setw(20)<<left<<check->college
<<setw(10)<<left<<check->quantity
<<setw(5)<<left<<check->size;
check=check->next;
}
}
else{
cout<<endl<<"Stack is an underflow.";
getch();
}
}
//display node in queue
void displayQueue(){
cout<<"\nSORTED CUSTOMER FULL ORDERLIST."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
getQueue();
cout<<endl;
}
//insert queuee
void insertQueue(customerNode *newNode){
if(!queueEmpty()){
newNode->next = NULL;
backPtr->next = newNode;
backPtr = newNode;
cout<<endl<<"inserted node into queue."<<endl;
}
else{
frontPtr = newNode;
backPtr = newNode;
}
}
void sortOrder(Customer &c){
for(customerNode *node = c.head; node!=NULL; node = node->next){
if(node->college == "kdoj"){
cout<<endl<<node->college;
insertQueue(node);
}
}
for(customerNode *node = c.head; node!=NULL; node = node->next){
if(node->college == "kdse"){
cout<<endl<<node->college;
insertQueue(node);
}
}
displayQueue();
}
};
//seller main menu
void sellerMenu(Customer &c, Seller &s){
int choice;
do{cout
<<endl<<"SELLER MENU"
<<endl<<"Please choose your task."
<<endl<<"1. Display OrderList"
<<endl<<"2. Sort OrderList"
<<endl<<"4. Exit"
<<endl<<"Your Choice: ";
cin>>choice;
//choose task
switch(choice){
case 1: s.displayStack(c);
break;
case 2: s.sortOrder(c);
break;
case 3:
break;
}
}while(choice != 4);
}
void menu(Customer &c, Seller &s){ //main menu function
int choice;
do{
cout
<<endl<<"Welcome to HotspotPrinting!"
<<endl<<"Please choose your role."
<<endl<<"1. Customer"
<<endl<<"2. Seller"
<<endl<<"3. Exit"
<<endl<<"Your Choice: ";
cin>>choice;
switch(choice){
case 1: customerMenu(c);
break;
case 2: sellerMenu(c,s);
break;
}
}while(choice != 3);
}
int main()
{
Customer c;
Seller s;
menu(c,s);
cout<<endl<<"Thank You For Using Our Service :)!";
return 0;
}
your problem is that customer node records exist in two containers, your queue and a list. But you only have one next pointer, when you insert a customer into the queue you set its next pointer to null, this destroys the linkage in the customer list.
If you need to have the same object in 2 containers then use shared_ptr in standard containers, like std::list and std::queue
When I am running this program having error in reading file compiler show error of access violation please help me out in this matter I have tried << and >> operators for writing and reading but I am using next pointer due to which I cannot use these operator
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class employee
{ private:
int id;
static int count;
public:
string name;
employee *next;
employee()
{ cout<<"enter name:";
cin>>name;
count++;
id=count;
next=NULL;
}
employee(int x)
{}
void setcount(int x)
{count=x;}
void show()
{cout<<"Name: "<<name<<endl;
cout<<"id: "<<id<<endl;
}
void setname(string s)
{name=s;}
string getname()
{return name;}
int getid()
{return id;}
};
int employee::count=0;
class db
{ employee *list;
public:
db ()
{ list=NULL; }
void hire()
{employee *e=new employee;
employee *temp=list;
if(list==NULL)
{list=e;}
else{
while(temp->next!=NULL)
{ temp=temp->next; }
temp->next=e;
}//else end
cout<<"hired..."<<endl;
}
void displayall()
{ if(list==NULL)
cout<<"NO record..";
else
{ employee *temp=list;
while(temp!=NULL)
{temp->show();
temp=temp->next; }
}//else end
}
void remove()
{ int id;
int found=0;
cout<<"Enter id to be removed...";
cin>>id;
if(list==NULL)
cout<<"no record..";
else
{ employee *temp=list;
employee *pre=list;
while(temp!=NULL)
{ if( temp->getid()==id)
{ found=1;
employee *e=temp;
e->show();
if(temp==list) //first item to b removed
list=list->next;
else
pre->next=temp->next;
delete e; cout<<"record deleted"<<endl;
break;
}
pre=temp;
temp=temp->next;
}//while end
if(found==0)
cout<<"record not found.."<<endl;
}//else end
}
void searchbyname()
{ string name;
int found=0;
cout<<"enter name..";
cin>>name;
if(list==NULL)
cout<<"no record..";
else
{ employee *temp=list;
while(temp!=NULL)
{ if( temp->getname()==name)
{ found=1;
temp->show();}
temp=temp->next;
}//while end
if(found==0)
cout<<"record not found.."<<endl;
}//else end
}
void save()
{ ofstream out;
out.open("eb",ios::out | ios::binary);
if(!out)
cout<<"cannot save..";
else
{ employee *temp=list;
while(temp!=NULL)
{ out.write((char*) temp,sizeof(employee));
temp=temp->next;
}
out.close();
}
}
void retrieve()
{ ifstream in;
in.open("eb",ios::in | ios::binary);
if(!in)
cout<<"cannot retrieve..";
else
{ employee *temp=NULL;
employee e(0);
while(1)
{ in.read((char*) &e,sizeof(employee));
employee *p=new employee(e); //dynamically creating an employee that will have the same value as 'e' and saving its address in p. Every node in link list has different location so p will be different. &e is always same. So actually read node from file in e than create a node dynamically with same contents and address will be save in p.
if(list==NULL)
{list=p;
temp=list;
p->show();}
else{
temp->next=p;
temp=temp->next;
e.show();
if(e.next==NULL)
{ e.setcount(e.getid()); break;}
}
}
in.close();
}
}
};
void main()
{
db obj;
try{ obj.retrieve();
int op;
while(1)
{ cout<<endl<<"Enter 1 to hire ,2 for remove ,3 for displayall, 4 for search by name.. 5 for exit";
cin>>op;
switch(op)
{case 1:
obj.hire(); break;
case 2:
obj.remove(); break;
case 3:
obj.displayall(); break;
case 4:
obj.searchbyname(); break;
}
if(op==5)
break;
}
obj.save();
}
catch(...)
{cout<<"error..";}
getch();
}
You can not read your class data from the file using this code: in.read((char*) &e,sizeof(employee)); Your class contains std::string and pointers, they will be initialized by the values, that point nowhere. Any attempt to use them is an undefined behavior, leading to the AV in your case.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I wrote a program using single CLL in order to have the following output:
Main Menu
Create Books CLL
Add book
Search (by Book Name)
Delete Author
Enter your Choice: 1
Enter Book ID: 11223344
Enter Book Name: Programming C++
Enter Author’s Name: Jhone
Node Created
But when i enter the first choice i will enter the book id, book name and then it will start looping without stop! What's wrong with my program?
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int bid;
char book,author;
struct node *next;
}*last;
class circular_llist
{
public:
void create_node(int id,char bname,char aname);
void add_begin(int id,char bname,char aname);
void delete_element(char author);
void search_element(char name);
void display_list();
circular_llist()
{
last = NULL;
}
};
int main()
{
int choice, bid, position;
char name,author;
circular_llist cl;
while (1)
{
cout<<endl<<"Main Menu:"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at beginning"<<endl;
cout<<"3.Delete"<<endl;
cout<<"4.Search"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the Book ID: ";
cin>>bid;
cout<<"Enter the Book Name: ";
cin>>name;
cout<<"Enter the Book Author: ";
cin>>author;
cl.create_node(bid,name,author);
cout<<endl;
break;
case 2:
cout<<"Enter the Book ID: ";
cin>>bid;
cout<<"Enter the Book Name: ";
cin>>name;
cout<<"Enter the Book Author: ";
cin>>author;
cl.add_begin(bid,name,author);
cout<<endl;
break;
case 3:
if (last == NULL)
{
cout<<"List is empty, nothing to delete"<<endl;
break;
}
cout<<"Enter the Author name for deletion: ";
cin>>author;
cl.delete_element(author);
cout<<endl;
break;
case 4:
if (last == NULL)
{
cout<<"List Empty!! Can't search"<<endl;
break;
}
cout<<"Enter Book Name: ";
cin>>name;
cl.search_element(name);
cout<<endl;
break;
case 5:
cl.display_list();
break;
case 6:
exit(1);
}
}
return 0;
}
void circular_llist::create_node(int id,char bname,char aname)
{
struct node *temp;
temp = new(struct node);
temp->bid = id;
temp->book = bname;
temp->author = aname;
if (last == NULL)
{
last = temp;
temp->next = last;
}
else
{
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void circular_llist::add_begin(int id,char bname,char aname)
{
if (last == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->bid = id;
temp->book = bname;
temp->author = aname;
temp->next = last->next;
last->next = temp;
}
void circular_llist::delete_element(char author)
{
struct node *temp, *s;
s = last->next;
if (last->next == last && last->author == author)
{
temp = last;
last = NULL;
free(temp);
return;
}
if (s->author == author) /*First Element Deletion*/
{
temp = s;
last->next = s->next;
free(temp);
return;
}
while (s->next != last)
{
/*Deletion of Element in between*/
if (s->next->author == author)
{
temp = s->next;
s->next = temp->next;
free(temp);
cout<<"Element "<<author;
cout<<" deleted from the list"<<endl;
return;
}
s = s->next;
}
if (s->next->author == author)
{
temp = s->next;
s->next = last->next;
free(temp);
last = s;
return;
}
cout<<"Element "<<author<<" not found in the list"<<endl;
}
void circular_llist::search_element(char name)
{
struct node *s;
int counter = 0;
s = last->next;
while (s != last)
{
counter++;
if (s->book == name)
{
cout<<"Element "<<name;
cout<<" found at position "<<counter<<endl;
return;
}
s = s->next;
}
if (s->book == name)
{
counter++;
cout<<"Element "<<name;
cout<<" found at position "<<counter<<endl;
return;
}
cout<<"Element "<<name<<" not found in the list"<<endl;
}
void circular_llist::display_list()
{
struct node *s;
if (last == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return;
}
s = last->next;
cout<<"Books List: "<<endl;
while (s != last)
{
cout<<s->bid<<"\n"<<s->book<<"\n"<<s->author<<"\n"<<endl;
s = s->next;
}
cout<<s->bid<<"\n"<<s->book<<"\n"<<s->author<<"\n"<<endl;
}
When you read single characters (like you do with name and author) you really read only single character. And you enter multiple characters for those fields, meaning that the first input (to name) will read the fist character of the name, and the second input (to author) will read the second character of the name. That will leave quite a few characters in the input buffer, and attempting to read anything but characters or strings will fail, but you don't check for these failures so you end up with just looping over and over again trying to read e.g. a number and failing.
Simplest solution? Begin with using std::string for strings, and continue with reading multiple-word entries using std::getline.
The problem is how you input author and book name, e.g., cin>>name. This reads only the first word from the input and the rest of the name (if it has spaces) is left in the input buffer. Next you try to read the menu choice number and it will fail because the next character in the buffer is the first character of the second word of the name of the book, or such. I suggest that you replace your cin >> name with std::getline.
UPDATE
Oh! I have missed that you actually input only a single character...
I am getting this error main.cpp:23:5: error: ‘Department’ does not name a type
/*
* File: main.cpp
* Author: anonymous
*
* Created on May 11, 2015, 9:44 PM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class College{
public :
string name; //for name of the college
int numOfColleges;
int numDepartments; //number of departments in this college
Department* dep; //this will point to the department in this college
College* next; //the next pointer to point to the next college
College(){
name =" ";
numDepartments=0 ;
dep = NULL;
next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){
cout<<name<<"\n";
}
};
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
void AddCollege(College *head)
{
string n;
int numD;
cout<<"Enter the name of the College : ";
cin>>n;
cout<<"Enter the number of Departments : ";
cin>>numD;
College* tmp = new College(n,numD);
if(!head)
{
head=tmp;
return;
}
College * t=head;
while(t->next) t=t->next;
t->next=tmp;
cout<<"college added";
}
void DeleteCollege(College*&head)
{
string name;
cout<<"enter name of college:";
cin>>name;
if((!head)||(!head->next && head->name!=name))
{
cout<<"could not find "<<name<<" in the list\n"; return;
}
if(head->next && head->name==name)
{
College *tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
return;
}
College* tmp = head;
College* t;
while(tmp->next)
{
if(tmp->name==name)
{
College* tmp = head;
head = head->next;
delete tmp;
tmp->next=NULL;
}
if(tmp->next->name == name)
{
t = tmp->next;
tmp->next = tmp->next->next;
delete t;
return;
}
tmp=tmp->next;
}
cout<<"could not find "<<name<<" in the list\n";
};
/*Print the list of colleges int the college head*/
void printColleges(College *head)
{
cout<<"ALL Colleges in Database : \n";
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
}
void AddDepartment(College *head)
{
if(!head)
{
cout<<"NO COLLEGES ADDED!!";
return;
}
string n,Dname;
int numS;
College* tmp = head;
College*tmp2 = tmp;
;
while(tmp)
{
cout<<"->"<<tmp->name;
tmp=tmp->next;
cout<<endl;
}
cout<<"Type a College name to Add a Department inside\n";
cin>>n;
cout<<"Enter Department name:";
cin>>Dname;
cout<<"Enter the number of students :";
cin>>numS;
while(n!=tmp->name)
{
tmp=tmp->next;
}
if(!tmp-> dep)
{
College * tmpD = new Department(Dname,numS);
Department*tmpDD = tmpD;
head=tmp;
return;
}
/*if(tmp->dep)
{
Department *tmp3 = tmp->dep->next;
t=tmp->dep;
dep->next=tmp3;
}*/
};
//void DeleteDepartment(College* head)
//{
//
//
// ;}
void PrintAll(College*head)
{
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
};
int main(int argc, char** argv) {
int choice;
College*h = NULL;
while(choice!=11){
cout<<"\n\nMenu\n";
cout<<"1: Add a new college in the list \n";
cout<<"2: Delete a college from the list \n";
cout<<"3: Add a department in a college\n";
cout<<"4: Delete a department from a college.\n";
cout<<"5: Print all the colleges along with their departments\n ";
cout<<"6: Delete all the departments of a particular college \n";
cout<<"7: Delete all the colleges from the list.\n";
cout<<"8: Print the total number of students in a college.\n";
cout<<"9: Find and print the college that has the highest number of students. \n";
cout<<"10: Find and print the department that has the highest number of students.\n";
cout<<"EXIT\n";
cin>>choice;
switch(choice)
{
case 1: AddCollege(*&h);
break;
case 2: DeleteCollege(*&h);
break;
case 3: printColleges(*&h);
break;
case 4:
AddDepartment(*&h);
break;
/* case 4:
DeleteDepartment(*&h);
break;
case 5:
PrintAll(*&h);
break;
case 6:
DeleteDepartment(*&h);
break;
case 7:
DeleteAllColleges(*&h);
break;
case 8:
NumOfStudentsInCollege(*&h);
break;
case 9:
HighestNumOfStudentsInCollege(*&h);
break;
case 10:
HighestNumOfStudentsInDep(*&h);
break;
case 11:
cout<<"bye";
break;*/
default:
cout<<"\nInvalid menu choice";
}
}
}
Declare your Department class before it's called in College. (Move your Department declaration above your College declaration):
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
class College{
public :
string name; //for name of the college
int numOfColleges;
int numDepartments; //number of departments in this college
Department * dep; //this will point to the department in this college
College * next; //the next pointer to point to the next college
College(){
name =" ";
numDepartments=0 ;
dep = NULL;
next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){
cout<<name<<"\n";
}
};
good day. i have a problem with my program. this is designed for small clinics to basically
record the patients information for an appointment. so here is my problem, at first it was
working but whenever im going to input infos for the 3rd patient(3rd node), after inputting
the name it will go on an error. this is a queue. `
#include <iostream>
using namespace std;
struct clnc_hrs
{
std:: string name;
std:: string symptms;
int age;
int cont_num;
clnc_hrs *next;
};
class List{
private:
clnc_hrs *rear;
clnc_hrs *front;
public:
List();
void BookAppointment();
void DeletePrevious();
void DisplaySchedule();
};
List::List(){
rear = NULL;
front = NULL;
}
void List::BookAppointment ()
{
std:: string N;
std:: string S;
int A;
int CN;
clnc_hrs *newnode = (clnc_hrs*)malloc(sizeof(clnc_hrs));
fflush(stdin);
cout<<"enter name of the patient:";
std::getline(cin, N);
newnode->name = N;
fflush(stdin);
cout<<"enter age of the patient:";
cin>>A;
newnode->age = A;
fflush(stdin);
cout<<"enter contact number of the patient:";
cin>>CN;
newnode->cont_num = CN;
fflush(stdin);
cout<<"enter the complaints or symptoms of the patient:";
std::getline(cin, S);
newnode->symptms = S;
newnode->next = NULL;
if(front == NULL)
{
fflush(stdin);
front = newnode;
fflush(stdin);
}
else
{
fflush(stdin);
rear->next = newnode;
fflush(stdin);
}
rear = newnode;
}
void List::DisplaySchedule ()
{
clnc_hrs *disp = (clnc_hrs*)malloc(sizeof(clnc_hrs));
disp = front;
if(front == NULL)
{
cout<<"there's no appointment for today"<<endl;
}
else
{
while(disp != NULL)
{
cout<<endl;
cout<<"name:"<<disp->name<<endl;
cout<<"age:"<<disp->age<<endl;
cout<<"contact number:"<<disp->cont_num<<endl;
cout<<"symptoms/complaints:"<<disp->symptms<<endl;
cout<<endl;
disp = disp->next;
}
}
}
void List::DeletePrevious()
{
clnc_hrs *newnode = (clnc_hrs*)malloc(sizeof(clnc_hrs));
if(front == NULL)
{
cout<<"no appointments today"<<endl;
}
else
{
newnode = front;
front = front->next;
cout<<"The previous patient is: "<<endl;
cout<<newnode->name<<endl;
cout<<newnode->age<<endl;
cout<<newnode->cont_num<<endl;
cout<<newnode->symptms<<endl;
delete newnode;
}
}
int main ()
{
List list;
int ans;
while(true)
{
cout<<"press 1 for booking an appointment\npress 2 to delete previous patients info \npress 3 for display"<<endl;
cin>>ans;
if(ans == 1)
{
list.BookAppointment();
}
else if(ans == 2)
{
list.DeletePrevious();
}
else if(ans == 3)
{
list.DisplaySchedule();
}
}
system("pause");
}
`
i hope someone here can help me. i am using dev c++ 4.9.9.2
As you say this in not homework then just use STL - i.e. http://www.cplusplus.com/reference/queue/queue/
All the bits'n'bobs are done for you
Don't use malloc in a C++ program, it doesn't construct C++ objects correctly, use new instead.
clnc_hrs *newnode = new clnc_hrs();
You might have other errors but this is the first one.
cout<<"enter name of the patient:";
std::getline(cin, N);
newnode->name = N;
is not wrong, but just a bit wasteful. This is simpler
cout<<"enter name of the patient:";
std::getline(cin, newnode->name);
same for all you other inputs.
fflush(stdin);
is implementation defined behaviour. fflush only has defined behaviour on output streams not input streams. You seem to be using fllush(stdin); as some sort of magic incantation that you hope will solve your problems. Avoid that kind of thinking. Learn what the code you write really does. In this case you should remove all calls to fflush(stdin);.