Suggestions with creating a Menu for Patient Program in C++ - c++

So I've basically have most of the programming almost complete. I wanted to add a menu that displays an output. For example, a list of records is created. After it is all entered, I would like for it to display only the patients that smoke or have high blood pressure or high fat diet when selecting a number from the menu. Now I am stuck trying to figure that out but can't seem to wrap my head around it. Any suggestions of how I can get it work? Right now it is only set to display all of the records at once.
//Program Description: An interface program that displays patients records.
#include <iostream>
#include <string>
using namespace std;
struct Node
{
char name[20];
int age;
string smoker;
string hbp;
string hfd;
int points;
Node *ptr;
};
Node *startPtr = NULL;
void getInfo()
{
Node *p, *p2;
p = new Node;
int agePoints;
int smkPoints;
int hbpPoints;
int hfdPoints;
cout << " Please enter the name of the patient : ";
cin >> p->name;
cout << " Please enter the age of the patient : ";
cin >> p->age;
cout << " Is he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " Does he/she have high blood pressure? (y/n) : ";
cin >> p->hbp;
cout << " Does he/she have a high fat diet? (y/n) : ";
cin >> p->hfd;
cout << endl;
p->ptr = NULL;
// Age point system
if (p-> age > 30) {
agePoints = 3;
}
else if (p->age > 20)
{
agePoints = 2;
}
else
{
agePoints = 1;
}
// Habits Points System
if (p->smoker == "Y" || p->smoker == "y")
{
p->smoker = "Yes";
smkPoints = 4;
}
else
{
p->smoker = "non smoker";
smkPoints = 0;
}
if (p->hbp == "Y" || p->hbp == "y")
{
p->hbp = "High blood pressure";
hbpPoints = 2;
}
else
{
p->hbp = "Normal blood pressure";
hbpPoints = 0;
}
if (p->hfd == "Y" || p->hfd == "y")
{
p->hfd = "High fat diet";
hfdPoints = 1;
}
else
{
p->hfd = "Low fat diet";
hfdPoints = 0;
}
p->points = agePoints + smkPoints + hbpPoints + hfdPoints;
// Set up link to this node
if (startPtr == NULL) {
startPtr = p;
}
else
{
p2 = startPtr;
while (p2->ptr != NULL)
p2 = p2->ptr;
p2->ptr = p;
}
}
void delete_start_node()
{
Node *p;
p = startPtr;
startPtr = startPtr->ptr;
delete p;
}
void delete_end_node()
{
Node *p1, *p2;
if (startPtr == NULL)
cout << "The List is empty!\n";
else
{
p1 = startPtr;
if (p1->ptr == NULL)
{
delete p1;
startPtr = NULL;
}
else
{
while (p1->ptr != NULL)
{
p2 = p1;
p1 = p1->ptr;
}
delete p1;
p1->ptr = NULL;
}
}
}
void disp()
{
Node *p;
p = startPtr;
if (p == NULL)
cout << "Empty List!\n";
while (p != NULL)
{
if (p == NULL)
cout << "Empty List!\n";
cout << " Name : " << p->name << endl;
cout << " Age : " << p->age << endl;
cout << " Smoker : " << p->smoker << endl;
cout << " Blood Pressure : " << p->hbp << endl;
cout << " Fat Diet : " << p->hfd << endl;
cout << " Points : " << p->points << endl;
cout << endl;
p = p->ptr;
}
}
Node* removeptr(Node* prev)
{
if (prev)
{
if (prev->ptr)
{
Node* p = prev->ptr;
prev->ptr = p->ptr;
return p;
}
}
else if (startPtr)
{
Node* p = startPtr;
startPtr = startPtr->ptr;
return p;
}
return NULL;
}
// sort by age in ascending order
void sortAge()
{
Node* startPtr2 = NULL;
while (startPtr)
{
Node* prev = NULL;
Node* curr = startPtr;
Node* prevMax = NULL;
int max = startPtr->age;
while (curr)
{
if (curr->age > max)
{
max = curr->age;
prevMax = prev;
}
prev = curr;
curr = curr->ptr;
}
// Node with the highest age found pass throug the list.
Node* xferNode = removeptr(prevMax);
if (xferNode)
{
xferNode->ptr = startPtr2;
startPtr2 = xferNode;
}
}
startPtr = startPtr2;
}
int main()
{
Node *p;
p = startPtr;
char selection;
do
{
cout << " Patient Menu\n";
cout << " =============================================\n";
cout << " 1. Insert a new record\n";
cout << " 2. List smoker patients\n"; // smoker patient
cout << " 3. List HBP patients\n"; // high blood pressure patient
cout << " 4. List HFD patients\n"; // high fat diet patient
cout << " 5. Delete a patient record by given name\n";
cout << " 6. Exit the program\n";
cout << " =============================================\n";
cout << " Enter your selection: ";
cin >> selection;
cout << endl;
switch (selection)
{
case '1':
getInfo();
break;
case '2':
disp();
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
cout << " Goodbye!\n";
return 0;
break;
default:
break;
}
}while (selection != 6);
/*
disp();
cout << "________________________________________________\n";
sortAge();
disp();*/
system("pause");
return 0;
}

Node *smokers, *hbp, *hfd = NULL;
You could keep a separate linked list for each type - smoker, hbp, hfd and change
void disp()
// to
void disp(Node* linkedList)
and print list given to it
case '2':
disp(smokers);
break;
case '3':
disp(hbp);
break;
case '4':
disp(hfd);
break;
btw, smells like a homework :)

Related

problem with the Queue and int main program in c++?

#include<iostream>
#include<string>
using namespace std;
class Product
{
public:
int static BarCode;
int Quantity;
double Price;
friend class ProudectQueue;
friend class ProductNode;
friend class Customerr;
friend class customerQueue;
//public:
product() {
BarCode= 0, Quantity = 0, Price = 0;
}
product(int barCode, int Quantity, double price)
{
BarCode=barCode ,Quantity=Quantity , Price = price;
}
void DisplayList();
void setBarCode(int bc)
{BarCode=bc;}
int getBarCode()
{return BarCode;}
void setQuantity(int Q)
{Quantity=Q;}
int getQuantity()
{return Quantity;}
void setPrice(int r)
{Price=r;}
int getPrice()
{return Price;}
void printProductDetails()
{ cout<<getBarCode()<<"\n";
cout<<getQuantity()<<"\n";
cout<<getPrice()<<endl;
}
};
class ProductNode //class node
{
public:
Product data;
ProductNode* next;
ProductNode()
{
data.BarCode = 0, data.Quantity = 0, data.Price = 0;
}
ProductNode(int BarCode, int Quantity, double price)
{
data.BarCode = BarCode, data.Quantity=Quantity, data.Price = price;
}
};
class ProductQueue {
ProductNode* rear, * front;
int counter;
public:
ProductQueue() { rear = front = NULL; counter = 0; }
~ProductQueue() { Product Productdata; while (!isempty()) dequeue(Productdata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Product ProductData) {
ProductNode* newnode = new ProductNode;
newnode->data = ProductData;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Product& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
ProductNode* nextnode;
nextnode = front->next;
data = front->data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(char ProductName)
{
ProductNode* currNode = front;
int position = 1;
while (currNode->data.BarCode!= ProductName)
{
if (currNode != NULL)
currNode = currNode->next;
position++;
}
if (currNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
ProductNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << "Product BarCode: " << currNode->data.BarCode << "\t"
<< "Product Quantity: " << currNode->data.Quantity<< "\t"
<< "Product price : " << currNode->data.Price;
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->next;
}
}
bool modify()
{
if(isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
int BarCode1;
double Price1;
int Quantity1;
cout << "\nEnter BarCode : "; cin >> BarCode1;
cout << "\nEnter price : "; cin >> Price1;
cout << "\nEnter Quantity : "; cin >> Quantity1;
rear->data.BarCode = BarCode1;
rear->data.Price = Price1;
rear->data.Quantity = Quantity1;
return true;
}
}
};
class Seller //seller class
{
private:
string Name;
int ID, contact;
float Reating;
friend class SellerQueue;
friend class SellerNode;
public:
Seller() { Name = "unknown", ID= 0, contact = 0,Reating=0; }
Seller(string name, int id, int con,float reating) { Name = name, ID=id , contact = con,Reating=reating; }
void setName(string name) { Name = name; }
void setID(int id) { ID = id; }
void setcontact(int con) { contact = con; }
void setReating(float reating){Reating=reating;}
string getName() { return Name; }
int getID() { return ID; }
int getcontact() { return contact; }
float getReating(){ return Reating;}
void printSellerInfo()
{
cout << "\tName : " << Name << "\t";
cout << "\tSeller ID : " << ID << "\t";
cout << "\tcontact: " << contact<< "\t\n";
cout << "\tSeller Reating : " << Reating << "\t";
}
};
class SellerNode
{
public:
Seller Data;
SellerNode* Next;
SellerNode() { Data.Name = "unknown", Data.ID = 0, Data.contact= 0,Data.Reating=0; }
SellerNode(string name, int id, int con,float reating)
{ Data.Name = name, Data.ID = id, Data.contact=con ,Data.Reating=reating;}
};
class SellerQueue {
SellerNode* rear, * front;
int counter;
public:
SellerQueue() { rear = front = NULL; counter = 0; }
~SellerQueue() { Seller Sellerdata; while (!isempty()) dequeue(Sellerdata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Seller SellerData) {
SellerNode* newnode = new SellerNode;
newnode->Data = SellerData;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->Next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Seller& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
SellerNode* nextnode;
nextnode = front->Next;
data = front->Data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(int SellerID)
{
SellerNode* currNode = front;
int position = 1;
while (currNode->Data.ID != SellerID)
{
if (currNode != NULL)
currNode = currNode->Next;
position++;
}
if (currNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
SellerNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << "Seller name : " << currNode->Data.Name << "\t" <<
"Seller ID:" << currNode->Data.ID << "\t"
<< "contact:" << currNode->Data.contact<<"\t"<<
"Seller Reating"<<currNode->Data.Reating;
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->Next;
}
}
bool modify()
{
if (isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
string name1;
int ID1;
int contact1;
float Reating1;
cout << "\nEnter seller name : "; cin >> name1;
cout << "\nEnter Seller ID : "; cin >> ID1;
cout << "\nEnter contact : "; cin >> contact1;
cout << "\nEnter seller Reating: "; cin >> Reating1;
rear->Data.Name = name1;
rear->Data.ID = ID1;
rear->Data.contact = contact1;
rear->Data.Reating = Reating1;
return true;
}
}
};
class Customer //custmer class
{
public:
int ID;
string Name;
float Point;
int Phone;
friend class Product;
friend class customerQueue;
friend class customerrNode;
Customer() {
ID = 0; Name = "Unknown"; Point = 0;Phone=0;
}
Customer(unsigned int id, string name, float point,int phone) {
ID = id; Name = name; Point = point; Phone=phone;
}
void setID(unsigned int id) { ID = id; }
void setName(string name) { Name = name; }
void setPoint(float point) { Point= point; }
void setPhone(int phone){ Phone= phone; }
void setProductforCustomer(int B, int q, double pr)
{
Product.getBarCode() = B;
Product.getQuantity() = q;
Product.getPrice() = pr; // erorr
}
int getBarCode() { return Product.BarCode; }
int Quantity() { return Product.Quantity; }
double getPrice() { return Product.Price; }
unsigned int getID() { return ID; }
string getName() { return Name; }
float getPoint() { return Point; }
int getPhone(){ return Phone; }
void printCustomerInfo()
{
cout << "\tID number : " << ID << "\t";
cout << "\tCustomer Name : " << Name << "\t";
cout << "\tPoint is : " << Point << "\t";
cout<<"\tphone Number is :"<< Phone<<"\t";
cout << "\tBarCode" << Product.BarCode;
cout << "\tQuantity"<<Product.Quantity;
cout << "\tprice : "<<Product.Price << endl;
}
};
class customerNode
{
public:
customerNode* Next;
Customer Data;
customerNode()
{
Data.ID = 0, Data.BarCode = 0, Data.Name = "unkown";
}
customerNode(int id, string Name, int BarCode)
{
Data.ID = id, Data.BarCode = BarCode, Data.Name = Name; //error
}
};
class customerQueue {
customerNode* rear, * front;
int counter;
public:
customerQueue() { rear = front = NULL; counter = 0; }
~customerQueue() { Customer customerdata; while (!isempty()) dequeue(customerdata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Customer customerdata) {
customerNode* newnode = new customerNode;
newnode->Data = customerdata;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->Next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Customer& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
customerrNode* nextnode; //error
nextnode = front->Next;
data = front->Data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(int id)
{
customerNode* CurrNode = front;
int position = 1;
while (CurrNode->Data.ID != id)
{
if (CurrNode != NULL)
CurrNode = CurrNode->Next;
position++;
}
if (CurrNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
customerNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t ";
cout << "Customer ID : " << currNode->Data.ID << " Custmer Name :"
<< currNode->Data.Name <<
" Custmer point: " << currNode->Data.Point<<
" Prouduct BarCode: " << currNode->Data.Product.BarCode <<
" Product Quantity :" << currNode->Data.Product.Quantity<<
" product price : " << currNode->Data.Product.Price;
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->Next;
}
}
bool modify()
{
if (isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
int BarCode1;
int Quantity1;
double Price1;
unsigned int ID1;
string Name1;
cout << "\nEnter customer ID : "; cin >> ID1;
cout << "\nEnter customer Name : "; cin >> Name1;
cout << "\nEnter BarCode : "; cin >> BarCode1;
cout << "\nEnter Quantity : "; cin >>Quantity1;
cout << "\nEnter price : "; cin >> Price1;
rear->Data.Product.BarCode = BarCode1;
rear->Data.Product.Quantity = Quantity;
rear->Data.Product.Price = Price1;
return true;
}
}
};
class Action
{
int product,Time;
string Seller, Customer;
double E_bidding;
friend class ActionQueue;
friend class ActionNode;
public:
Action() { Product = 0, Time = 0,Seller="unknown",custmer="unknown",E_bidding=0; }
Action(int Product, int Time,string Seller,string Custmer,double E_bidding)
{ Product=product, Time = time,Seller=seller,Custmer=custmer,E-binding=ebinding; }
void setProduct(int product) { Product=product; }
int getProduct() { return Product; }
void setTime (int time){ Time=time;}
int getTime () { return Time; }
void setSeller (string seller){ Seller = seller; }
string getSeller () { return Seller; }
void setCustomer (string custmor) { Customer = custmor;}
string getCustomer () { return Customer; }
void setE_bidding (double ebidding) { E_bidding = ebidding; }
double getE_bidding () { return E_bidding; }
void printActionDetails()
{
cout<<"Auction Details: "<<"\n"<<getE_bidding()<<"\n"<<getCustomer()<<"\n"<<getProduct()
<<"\n"<<getSeller()<<"\n"<<getTime()<<endl;
}
};
class ActionNode
{
public:
ActionNode Data;
ActionPosition Data;
ActionNode* next;
ActionNode()
{
Data.Product = 0, Data.Time = 0,Data.Seller="unknown",Data.Customer="unknown",Data.E_bidding=0 ;
}
ActionNode(int product, int time,string seller,string customer,double ebidding)
{
Data.Product = product, Data.Time = time,Data.Seller=seller,Data.Customer=customer,Data.E_bidding=ebidding;
}
};
class ActionQueue
{
ActionQueue newnode;
ActionNode* rear, * front;
int counter;
public:
ActionQueue() { rear = front = NULL; counter = 0; }
~ActionQueue() { Action Actiondata; while (!isempty()) dequeue(Actiondata); }
bool isempty() { if (counter == 0) return true; else return false; }
void enqueue(Action ActionData) {
stockNode* newnode = new stockNode; //error
newnode->Data = ActionData;
if (isempty())
{
rear = newnode;
front = newnode;
}
else {
rear->next = newnode;
rear = newnode;
}
counter++;
}
bool dequeue(Action& data)
{
if (isempty()) { cout << "Error:the queue is empty\n"; return false; }
else {
ActionNode* nextnode;
nextnode = front->next;
data = front->Data;
delete front;
front = nextnode;
counter--;
return true;
}
}
int find(int Product)
{
ActionNode* currNode = front;
int position = 1;
while (currNode->Data.Product != Product)
{
if (currNode != NULL)
currNode = currNode->next;
position++;
}
if (currNode) return position;
return -1;
}
void display()
{
cout << endl;
cout << "Front-->";
ActionNode* currNode = front;
for (int i = 0; i < counter; i++)
{
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << " Product : " << currNode->Data.Product << "\t" <<
"Time :" << currNode->Data.Time<<"\t"<<
"Seller:"<< currNode->Data.Seller<<"\t"<<
"Customer :" << currNode->Data.Custmoer<<"\t"<<
"E_bidding :" << currNode->Data.E_bidding<<"\t";
if (i != counter - 1) cout << endl;
else cout << "\t<-- Rear" << endl;
currNode = currNode->next;
}
}
bool modify()
{
if (isempty())
{
cout << "Error ,the queue is empty \n"; return false;
}
else
{
int product1,Time1;
string Seller1, Customer1;
double E_bidding1;
cout << "\nEnter Product : "; cin>>product1;
cout<< "\nEnter Time :" ;cin>>Time1;
cout<< "\nEnter Seller:";cin>>Seller1;
cout<< "\nEnter Customer :" ;cin>>Customer1;
cout<< "\nEnter E_bidding :" ;cin>>E_bidding1;
rear->Data.product = product1;
rear->Data.Time = Time1;
rear->Data.Seller = Seller1;
rear->Data.Customer = Customer1;
rear->Data.E_bidding = E_bidding1;
return true;
}
}
};
int main()
{
Seller sellerObject, valseller;
SellerQueue SellerQueue1;
Product productObject ,valPruduct;
ProductQueue productQueue1;
Customer customerObject, valCostumer;
customerQueue customerQueue1;
Action ActionObject, Action,valAction;
ActionQueue Actionqueue1;
string string1, string2, string3;
int int1, int2, int3,index, intChoice, customerloop, customerTotal;
double double1,double2;
char charChoice;
bool bool1;
float float1,float2,float3;
for (int i = 0;; i++)
{
cout << "Main menu:\n";
cout << "1.Product\n";
cout << "2.Seller\n";
cout << "3.Customer\n";
cout << "4.ACtion\n";
cin >> intChoice;
switch (intChoice)
{
case 1:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
ProductQueue.display();
break;
case 'b':
cout << "Enter product BarCode: "; cin >> int1;
cout << "Enter Quantity : "; cin >> int1;
cout << "Enter product price : "; cin >> double1;
productObject.setBarCode(int1);
productObject.setQuantity(int1);
productObject.setPrice(double1);
ProductQueue.enqueue(ProductObject);
break;
case'c':
cout << ProductQueue.dequeue(valPruduct) << endl;
valPruduct.printProductDetails();
break;
case 'd':
cout << "modify rear element : ";
ProductQueue.modify();
break;
case'e':
cout << "Enter Product BarCode to find it "; cin >> int1;
cout << "\nthe element index : "<<ProductQueue.find(int1) << endl;
break;
case'f':
break;
}
break;
case 2:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
SellerQueue1.display();
break;
case 'b':
cout << "Enter Seller name : "; cin >> string1;
cout << "Enter Seller ID : "; cin >> int2;
cout << "Enter contact : "; cin >> int1;
cout << "Enter Reating : "; cin >> float1;
sellerObject.setName(string1);
sellerObject.setID(string2); //error
sellerObject.setcontact(int1);
sellerObject.setReating(float1);
SellerQueue.enqueue(sellerObject);
break;
case'c':
SellerQueue.dequeue(valseller);
valseller.printSellerInfo();
break;
case 'd' :
cout << "modify rear element : ";
SellerQueue.modify();
break;
case'e':
cout << "Enter Seller ID to find him : "; cin >> int1;
cout << "\nthe element index : " << SellerQueue.find(int1)<<endl;
break;
case'f':
break;
}
break;
case 3:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
customerQueue1.display();
break;
case 'b':
// Customer = 0;
cout << "Enter customer Name : "; cin >> string3;
cout << "Enter customer id : "; cin >> int1;
cout << "Enter customer point : "; cin >> float2;
cout << "Enter customer number : "; cin >> int2;
cout << "Enter BarCode : "; cin >> int2;
cout << "Enter Quantity : "; cin >> double1;
cout << "\tprice : " ;cin >>double2 ;
customerObject.setID(int1);
customerObject.setName(string3);
customerObject.setPoint(flaot3);//??error
customerObject.setProductforCustomer(int1, int2, double1);
customerQueue1.enqueue(customerObject);
for (int j = 1;; i++)
{
cout << "Do you want add a new Prodect to this customer ? \n 1 ""YES"" \n 2 ""NO"" ";
cin >> customerloop;
if (customerloop == 1)
{
cout << "Enter Product BarCode : "; cin >> int1;
cout << "Enter Product Quantity : "; cin >> int2;
cout << "Enter Product price : "; cin >> double1;
// customerObject.setBarCode();
customerObject.setProductforCustomer(int1, int2, double1);
customerQueue1.enqueue(customerObject);
}
else break;
}
break;
case'c':
cout << customerQueue1.dequeue(valCostumer);
valCostumer.printCustomerInfo();
break;
case 'd':
cout << "modify rear element : ";
customerQueue1.modify();
break;
case'e':
cout << "Enter customer id to find him : "; cin >> int1;
cout << "\nthe element index : " << customerQueue1.find(int1)<<endl;
break;
case'f':
break;
}
break;
case 4:
cout << "a.Display\n";
cout << "b.Insert\n";
cout << "c.Delete\n";
cout << "d.Modify\n";
cout << "e.Find\n";
cout << "f.Exit\n";
cin >> charChoice;
switch (charChoice)
{
case'a':
ActionQueue.display();
break;
case 'b':
cout << "\nEnter Product : "; cin>>int1;
cout<< "\nEnter Time :" ;cin>>int2;
cout<< "\nEnter Seller:";cin>>string1;
cout<< "\nEnter Customer :" ;cin>>string2;
cout<< "\nEnter E_bidding :" ;cin>>double1;
ActionObject.setProduct(int1);
ActionObject.setTime(int2);
ActionObject.setSeller(string1);
ActionObject.setCustomer(string2);
ActionObject.setE_bidding(double1);
ActionQueue.enqueue(ActionObject);
break;
case'c':
cout << ActionQueue.dequeue(valAction);
valAction.printActionDetails();
break;
case 'd':
cout << "modify rear element : ";
ActionQueue.modify();
break;
case'e':
cout << "Enter the Prodect to find it :"; cin >> int1;
cout << "\nthe element index : " << ActionQueue.find(int1)<<endl;
break;
case'f':
break;
}
break;
}
}
}
we study a small part about queue so we don't have full knowledge about it. we want to know why in call error, the class customer and class Action have most of the problem also the call in the int main .
most error like this [Error] expected unqualified-id before '.' token.
the error in name or wasn't declared in the scope it's ok we can fix it
pleas help

Switch options are random and abruptly stops

I'm a coding newbie and I've been lost to what my program is doing. Basically, my add() method abruptly stops when I call it by case 1, 3 times, and then it terminates the program when I want to add more student ID to my node. I'm at a loss and I have been trying to find out what's wrong. Any help would be appreciated!
#include <iostream>
using namespace std;
struct Node
{
int ID;
Node *next;
}; Node *head = NULL;
void Menu();
void Add();
void Delete();
void Search();
void Display();
int main()
{
int Options = 0;
cout << "Welcome to Student Records. Choose an option:\n"
<< "1.) Add student\n"
<< "2.) Delete student\n"
<< "3.) Search student\n"
<< "4.) Display list of students\n"
<< "5.) Exit\n\n";
cout << "Input: ";
cin >> Options;
do{
switch(Options)
{
case 1:
Add();
break;
case 2:
Delete();
break;
case 3:
Search();
break;
case 4:
Display();
break;
default:
cout << "Operation terminated.";
}
cout << "\nWhat's next?\n";
Menu();
cin >> Options;
}while(Options == 1 || Options == 2 || Options == 3 || Options == 4);
cout << "Exit program?";
return 0;
}//end main
void Menu()
{
cout << "1.) Add student\n"
<< "2.) Delete student\n"
<< "3.) Search student\n"
<< "4.) Display list of students\n"
<< "5.) Exit\n\n";
}
void Add()
{
int id;
Node *node_new;
Node *pointer;
Node *lastNode = NULL;
node_new = new Node;
cout << "Enter 3-digit Student ID#: ";
cin >> id;
node_new -> ID = id;
if (head == NULL) //if list is empty
{
head = node_new;
node_new -> next = NULL;
}
else //if head is not NULL
{
pointer = head;
lastNode = NULL;
while(pointer -> ID < id && pointer != NULL)
{
lastNode = pointer;
pointer = pointer -> next;
}
if (lastNode == NULL)
{
head = node_new;
node_new -> next = pointer;
}
else
{
lastNode -> next = node_new;
node_new -> next = pointer;
}
}
}
void Delete()
{
int id;
Node *pointer;
Node *lastNode;
cout << "Enter Student ID# you wish to delete: ";
cin >> id;
if (head == NULL)
{
cout << "The list is already empty.";
}
else if (head -> ID == id)
{
pointer = head;
head = head -> next;
delete pointer;
}
else
{
pointer = head;
while(pointer -> ID != id && pointer != NULL)
{
lastNode = pointer;
pointer = pointer -> next;
}
if(pointer == NULL)
cout << "Student does not exist.";
else
lastNode -> next = pointer -> next;
delete pointer;
}
}
void Display()
{
Node *pointer;
pointer = head;
do{
if(pointer == NULL)
cout << "The list is already empty!";
else
{
cout << "Student ID# " << pointer -> ID << endl;
pointer = pointer -> next;
}
}while(pointer != NULL);
}
void Search()
{
int id;
Node *pointer;
pointer = head;
cout << "Who do you want to search? Enter ID#: ";
cin >> id;
do{
if(pointer == NULL)
cout << "Empty";
else
{
cout << "ID: " << pointer -> ID << endl;
pointer = pointer -> next;
}
}while(pointer != NULL);
}

separating C++ code in to multiple files

I hope you are all doing well.
I have the below code which manages a printing queue. I need to separate it into the below files and I dont know how to this. Oddly i've never been asked to do this. Any help you can provide would be greatly appreciated. I am not sure what code goes in which file.
Files I need to separate my code into:
1. heap.h - declaration file for heap.
2. heap.cpp - implementation file for heap.
3. pqtype.h - declaration file for priority queue.
4. pqtype.cpp - implementation file for priority queue.
5. test.cpp - driver file.
My current Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct node
{
int priority;
int jNum;
string jName;
struct node *next;
};
class PriorityQueue
{
private:
node *front;
public:
PriorityQueue()
{
front = NULL;
}
void addJob(string item, int priority, int number)
{
node *temp, *q;
temp = new node;
temp->jName = item;
temp->priority = priority;
temp->jNum = number;
//whether queue is empty
if (front == NULL || priority < front->priority)
{
temp->next = front;
front = temp;
}
else
{
q = front;
while (q->next != NULL && q->next->priority <= priority)
q = q->next;
temp->next = q->next;
q->next = temp;
}
}
void printJob()
{
node *temp;
if (front == NULL)
cout << "There Are No Print Requests In The Queue.\n";
else
{
temp = front;
cout << "\nNow printing Request # " << temp->jNum << " " <<"For "<< temp->jName <<"\n"<< endl;
front = front->next;
free(temp);
}
}
void viewJob()
{
node *ptr;
ptr = front;
if (front == NULL)
cout << "There Are No Print Requests In The Queue\n\n";
else
{
while (ptr != NULL)
{
cout << "Job #: " << ptr->jNum << " " <<"For "<< ptr->jName <<"\n"<< endl;
ptr = ptr->next;
}
}
}
};
int main()
{
int choice, priority;
PriorityQueue pq;
char ch;
string jName;
int number = 0;
cout << "Printing Queue\n" << endl;
do
{
cout << "==============" << endl;
cout << "1. Add Job\n";
cout << "2. Print Job\n";
cout << "3. View Job\n";
cout << "4. Exit\n";
cout << "\nEnter Your Option Now : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Are You A Student (s or S), TA (t or T), Or Instructor (i or I)? ";
cin >> ch;
if (ch == 'i' || ch == 'I')
{
priority = 1;
jName = "Instructor";
cout << "\nJob Successfully Entered.\n\n";
}
else if (ch == 't' || ch == 'T')
{
priority = 2;
jName = "TA";
cout << "\nJob Successfully Entered.\n\n";
}
else if (ch == 's' || ch == 'S')
{
priority = 3;
jName = "Student";
cout << "\nJob Successfully Entered.\n\n";
}
number++;
pq.addJob(jName, priority, number);
break;
case 2:
pq.printJob();
break;
case 3:
pq.viewJob();
break;
case 4:
cout << "\nThank You For Using Printint Queue\nProgram Now Closing...\n\n";
break;
default:
cout << "\nInvalid Choice Selected! \n";
}
}
while (choice != 4);
system("PAUSE");
return 0;
}

Error in my c++ code using linkedlist "Stop working" [closed]

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
my code stop working in that test case, i think that the error in function Checktables but i'm not sure and i can't fix the error please help me to tun this code correctly.
image of a test case and the error
this is a cpp file with main .cpp
#include"Header.h"
string Name;
string namess;
customer::customer()
{
name = "";
gsize = status = 0;
next = NULL;
}
customer::customer(string name1, int gsize1, int status1)
{
name = name1;
gsize = gsize1;
status = status1;
next = NULL;
}
waitinglist::waitinglist()
{
chairnum =50 ;
totalcustomers = tables = 0;
head = tail = NULL;
}
waitinglist::waitinglist(int val)
{
chairnum = 50;
totalcustomers = 0;
tables = 0;
head = tail = NULL;
}
void waitinglist::change()
{
customer*temp ;
temp = head;
cout << "enter the name: ";
cin >> namess;
while (temp != NULL)
{
if (namess == temp->name)
{
if (temp->status==2)
{
temp->status=1;
cout << "done! " << endl ;
break ;
}
}
else if (namess != temp->name)
{
temp = temp->next;
}
}
if (temp == NULL)
{
cout << "can't found! " << endl;
}
}
void waitinglist::newcustomer()
{
customer*tmp = new customer;
cout << "enter the name: "; cin >> tmp->name;
customer*tmpo=new customer;
tmpo=head ;
while (tmpo != NULL)
{
if (tmp->name != tmpo->name)
{
tmpo = tmpo->next;
}
else if (tmp->name == tmpo->name)
{
cout<<"The Name already exist! " << endl ;
cout << "enter the name: "; cin >> tmp->name;
tmpo=head;
}
}
cout << "enter the group number: "; cin >> tmp->gsize;
cout << "enter the status: "; cin >> tmp->status;
if (head == NULL) // linkedlist is empty
{
head = tail = tmp;
totalcustomers++;
}
else
{
tail->next = tmp;
tail=tail->next;
totalcustomers++;
}
}
void waitinglist::checktables()
{
float c=5.00;
customer*temp=head;
customer*found;
cout<<"enter number of tables: ";
cin >> tables ;
while (tables>=1 && temp!=NULL)
{
int x;
float y;
y=((temp->gsize)/c);
x=(temp->gsize)/c;
if (tables<y)
{
temp=temp->next;
}
else if (tables>=y)
{
if (x==y)
{
tables=tables-x ; // Correct Table!
cout<<temp->name<<endl;
}
else if (x!=y)
{
tables=tables-(x+1);
cout<<temp->name<<endl;
}
found=temp ;
delete found; // Discard
break ;
}
}
}
void waitinglist::displayall()
{
customer *tmp;
tmp = head;
if (tmp == NULL)
{
cout << "Empty!";
}
while (tmp != NULL)
{
cout << "Name: " << tmp->name <<endl;
cout << "group number: " << tmp->gsize << endl;
tmp = tmp->next;
}
cout << endl;
}
void waitinglist::diplaycustomer()
{
customer*tmp;
tmp = head;
cout << "enter the name: ";
cin >> Name;
while (tmp != NULL)
{
if (Name == tmp->name)
{
cout << "the name : " << tmp->name << endl;
cout << "the group size = " << tmp->gsize << endl;
cout << "the status = " << tmp->status << endl;
break;
}
else if (Name != tmp->name)
{
tmp = tmp->next;
}
}
if (tmp == NULL)
{
cout << "can't found!" << endl;
}
}
int main()
{
int choice;
string name1 = "";
int gsize1 = 0;
int status1 = 0;
waitinglist mylist;
cout << "Note: 1 in status means the customer not here and 2 means the customer is here.\n";
cout << "Select your option.\n\n";
cout << "(1) Add a new Customer.\n";
cout << "(2) Display information based on Name.\n";
cout << "(3) List all Names.\n";
cout << "(4) to change the status. \n" ;
cout << "(5) Check tables by name. \n";
cout << "(6) quit. \n";
do
{
cout << "\n";
cout << "Enter your choice: --> ";
cin >> choice;
if (1 <= choice && choice <= 5)
{
switch (choice)
{
case 1:
mylist.newcustomer();
break;
case 2:
mylist.diplaycustomer();
break;
case 3:
mylist.displayall();
break;
case 4:
mylist.change() ;
break;
case 5 :
mylist.checktables();
break;
default:
cout << "Invalid choice. Enter again.\n\n";
break;
}
}
else if (choice>6)
{
cout << "Invalid choice. Enter again.\n\n";
break;
}
} while (choice != 6);
return 0;
}
and this is the header file .h
#include<iostream>
#include<string>
using namespace std;
class customer
{
public:
string name;
int gsize;
int status;
customer* next;
customer();
customer(string,int,int);
};
class waitinglist
{
public:
int tables; //number of occupied tables
int chairnum;
int totalcustomers;
customer*head,*tail;
waitinglist();
waitinglist(int);
void newcustomer();
void diplaycustomer();
void displayall();
void change () ;
void checktables();
};
One error is that your checktables function corrupts your linked list structure by calling delete on one of the nodes:
found = temp;
delete found; // Discard
What you've just done in those lines above is to have a linked list with a broken (invalid) link in it. Any functions that now traverses the list (like displaytables) will now hit the broken link, and things start to go haywire.
To delete a node from a linked list, you have to not just call delete, but adjust the link in waitinglist that used to point to that deleted node and have it point to the next node after the deleted one.
Think of it like a real chain -- if one of the links in the chain needs to be removed, you have to physically remove it, and hook the link before it to the next good link. You didn't do this step.
I won't write the code for that, but this is what you should have seen much earlier in the development of your program. Better yet would have been to write a singly-linked list class that adds and removes nodes correctly first. Test it, and then once it can add and remove nodes successfully without error, then use it in your larger program.

Reading input from a text file, omits the first and adds a nonsense value to the end?

When I input locations from a txt file I am getting a peculiar error where it seems to miss off the first entry, yet add a garbage entry to the end of the link list (it is designed to take the name, latitude and longitude for each location you will notice). I imagine this to be an issue with where it starts collecting the inputs and where it stops but I cant find the error!! It reads the first line correctly but then skips to the next before adding it because during testing for the bug it had no record of the first location Lisbon though whilst stepping into the method call it was reading it. Very bizarre but hopefully someone knows the issue. Here is firstly my header file:
#include <string>
struct locationNode
{
char nodeCityName [35];
double nodeLati;
double nodeLongi;
locationNode* Next;
void CorrectCase() // Correct upper and lower case letters of input
{
int MAX_SIZE = 35;
int firstLetVal = this->nodeCityName[0], letVal;
int n = 1; // variable for name index from second letter onwards
if((this->nodeCityName[0] >90) && (this->nodeCityName[0] < 123)) // First letter is lower case
{
firstLetVal = firstLetVal - 32; // Capitalise first letter
this->nodeCityName[0] = firstLetVal;
}
while(n <= MAX_SIZE - 1)
{
if((this->nodeCityName[n] >= 65) && (this->nodeCityName[n] <= 90))
{
letVal = this->nodeCityName[n] + 32;
this->nodeCityName[n] = letVal;
}
n++;
}
//cityNameInput = this->nodeCityName;
}
};
class Locations
{
private:
int size;
public:
Locations(){
}; // constructor for the class
locationNode* Head;
//int Add(locationNode* Item);
};
And here is the file containing main:
// U08221.cpp : main project file.
#include "stdafx.h"
#include "Locations.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int n = 0,x, locationCount = 0, MAX_SIZE = 35;
string cityNameInput;
char targetCity[35];
bool acceptedInput = false, userInputReq = true, match = false, nodeExists = false;// note: addLocation(), set to true to enable user input as opposed to txt file
locationNode *start_ptr = NULL; // pointer to first entry in the list
locationNode *temp, *temp2; // Part is a pointer to a new locationNode we can assign changing value followed by a call to Add
locationNode *seek, *bridge;
void setElementsNull(char cityParam[])
{
int y=0, count =0;
while(cityParam[y] != NULL)
{
y++;
}
while(y < MAX_SIZE)
{
cityParam[y] = NULL;
y++;
}
}
void addLocation()
{
temp = new locationNode; // declare the space for a pointer item and assign a temporary pointer to it
if(!userInputReq) // bool that determines whether user input is required in adding the node to the list
{
cout << endl << "Enter the name of the location: ";
cin >> temp->nodeCityName;
temp->CorrectCase();
setElementsNull(temp->nodeCityName);
cout << endl << "Please enter the latitude value for this location: ";
cin >> temp->nodeLati;
cout << endl << "Please enter the longitude value for this location: ";
cin >> temp->nodeLongi;
cout << endl;
}
temp->Next = NULL; //set to NULL as when one is added it is currently the last in the list and so can not point to the next
if(start_ptr == NULL){ // if list is currently empty, start_ptr will point to this node
start_ptr = temp;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->Next != NULL)
{
temp2 = temp2->Next; // Move to next link in chain until reach end of list
}
temp2->Next = temp;
}
++locationCount; // increment counter for number of records in list
if(!userInputReq){
cout << "Location sucessfully added to the database! There are " << locationCount << " location(s) stored" << endl;
}
}
void populateList(){
ifstream inputFile;
inputFile.open ("locations.txt", ios::in);
userInputReq = true;
temp = new locationNode; // declare the space for a pointer item and assign a temporary pointer to it
do
{
inputFile.get(temp->nodeCityName, 35, ' ');
setElementsNull(temp->nodeCityName);
inputFile >> temp->nodeLati;
inputFile >> temp->nodeLongi;
setElementsNull(temp->nodeCityName);
if(temp->nodeCityName[0] == 10) //remove linefeed from input
{
for(int i = 0; temp->nodeCityName[i] != NULL; i++)
{
temp->nodeCityName[i] = temp->nodeCityName[i + 1];
}
}
addLocation();
}
while(!inputFile.eof());
userInputReq = false;
cout << "Successful!" << endl << "List contains: " << locationCount << " entries" << endl;
cout << endl;
inputFile.close();
}
bool nodeExistTest(char targetCity[]) // see if entry is present in the database
{
match = false;
seek = start_ptr;
int letters = 0, letters2 = 0, x = 0, y = 0;
while(targetCity[y] != NULL)
{
letters2++;
y++;
}
while(x <= locationCount) // locationCount is number of entries currently in list
{
y=0, letters = 0;
while(seek->nodeCityName[y] != NULL) // count letters in the current name
{
letters++;
y++;
}
if(letters == letters2) // same amount of letters in the name
{
y = 0;
while(y <= letters) // compare each letter against one another
{
if(targetCity[y] == seek->nodeCityName[y])
{
match = true;
y++;
}
else
{
match = false;
y = letters + 1; // no match, terminate comparison
}
}
}
if(match)
{
x = locationCount + 1; //found match so terminate loop
}
else{
if(seek->Next != NULL)
{
bridge = seek;
seek = seek->Next;
x++;
}
else
{
x = locationCount + 1; // end of list so terminate loop
}
}
}
return match;
}
void deleteRecord() // complete this
{
int junction = 0;
locationNode *place;
cout << "Enter the name of the city you wish to remove" << endl;
cin >> targetCity;
setElementsNull(targetCity);
if(nodeExistTest(targetCity)) //if this node does exist
{
if(seek == start_ptr) // if it is the first in the list
{
junction = 1;
}
if(seek != start_ptr && seek->Next == NULL) // if it is last in the list
{
junction = 2;
}
switch(junction) // will alter list accordingly dependant on where the searched for link is
{
case 1:
start_ptr = start_ptr->Next;
delete seek;
--locationCount;
break;
case 2:
place = seek;
seek = bridge;
delete place;
--locationCount;
break;
default:
bridge->Next = seek->Next;
delete seek;
--locationCount;
break;
}
}
else
{ cout << targetCity << "That entry does not currently exist" << endl << endl << endl;
}
}
void searchDatabase()
{
char choice;
cout << "Enter search term..." << endl;
cin >> targetCity;
if(nodeExistTest(targetCity))
{
cout << "Entry: " << endl << endl;
}
else
{
cout << "Sorry, that city is not currently present in the list." << endl << "Would you like to add this city now Y/N?" << endl;
cin >> choice;
/*while(choice != ('Y' || 'N'))
{
cout << "Please enter a valid choice..." << endl;
cin >> choice;
}*/
switch(choice)
{
case 'Y':
addLocation();
break;
case 'N':
break;
default :
cout << "Invalid choice" << endl;
break;
}
}
}
void printDatabase()
{
temp = start_ptr; // set temp to the start of the list
do
{ if (temp == NULL)
{
cout << "You have reached the end of the database" << endl;
}
else
{ // Display details for what temp points to at that stage
cout << "Location : " << temp->nodeCityName << endl;
cout << "Latitude : " << temp->nodeLati << endl;
cout << "Longitude : " << temp->nodeLongi << endl;
cout << endl;
// Move on to next locationNode if one exists
temp = temp->Next;
}
}
while (temp != NULL);
}
void nameValidation(string name)
{
n = 0; // start from first letter
x = name.size();
while(!acceptedInput)
{
if((name[n] >= 65) && (name[n] <= 122)) // is in the range of letters
{
while(n <= x - 1)
{
while((name[n] >=91) && (name[n] <=97)) // ERROR!!
{
cout << "Please enter a valid city name" << endl;
cin >> name;
}
n++;
}
}
else {
cout << "Please enter a valid city name" << endl;
cin >> name;
}
if(n <= x - 1)
{
acceptedInput = true;
}
}
cityNameInput = name;
}
int main(array<System::String ^> ^args)
{
//main contains test calls to functions at present
cout << "Populating list...";
populateList();
printDatabase();
deleteRecord();
printDatabase();
cin >> cityNameInput;
}
The text file contains this (ignore the names, they are just for testing!!):
Lisbon 45 47
Fattah 45 47
Darius 42 49
Peter 45 27
Sarah 85 97
Michelle 45 47
John 25 67
Colin 35 87
Shiron 40 57
George 34 45
Sean 22 33
The output omits Lisbon, but adds on a garbage entry with nonsense values. Any ideas why? Thank you in advance.
The main function creates a new locationNode and stores it in the global variable temp, then reads the first dataset and stores it in that node.
Then you call addLocation() which starts by creating another new locationNode which replaces the existing one in temp. This new node is then inserted into the list.
The next iteration of the main loop then fills that temp value with values and addLocation() inserts another brand new locationNode into the list. So the first dataset isn't stored in the list and each iteration ends up by inserting an uninitialized new node.
As you see using global variables can lead to confusing situations and you code would surely become clearer if you were passing the nodes as parameters to your functions.