My program is about an employee management system, and I am supposed to use a linked list to store the employees (struct). Let's suppose I register 3 employees with distinct information. When printing the list I get the last employee printed 3 times.
void push(NODE** head, employee* new_data)
{
// 1. allocate node
NODE* new_node = new NODE();
// 2. put in the data
new_node->data = new_data;
// 3. Make next of new node as head
new_node->next = (*head);
// 4. Move the head to point to
// the new node
(*head) = new_node;
}
NODE* InsertAtHead(NODE *&head, employee* val) {
NODE* tmp;
//create a new node
tmp = new NODE;
if (tmp == NULL)
exit(1);
//Initialize the new node
tmp->data = val;
tmp->next = NULL;
//Insert by changing links
tmp->next = head;
head = tmp;
return head;
}
void PrintList(NODE* head) {
NODE* cur;
if (IsEmpty(head)) {
cout << "The list is empty" << endl;
return;
}
cout << "The list is:" << endl;
cur = head;
while (cur != NULL) {
cout << "Full Name: " << cur->data->empInfo.fname << "" << cur->data->empInfo.lname << endl;
cout << "Date of Birth: " << cur->data->dateOfBirth.day << "/" << cur->data->dateOfBirth.month << "/" << cur->data->dateOfBirth.day << endl;
cout << "Address: " << cur->data->adr.city << " - " << cur->data->adr.street << " - " << cur->data->adr.building << endl;
cout << "Date Employed: " << cur->data->dateEmployed.day << "/" << cur->data->dateEmployed.month << "/" << cur->data->dateEmployed.day << endl;
cout << "Position: " << cur->data->position << endl << endl;
cur = cur->next;
}
cout << endl;
}
I tried different insertion methods, and I tried saving on a CSV file. The CSV file saves the info of the 3 different employees.
fout.open("employees.csv", ios::out | ios::app);
cout << "Enter the Number of Employees: ";
std::cin >> n;
cout << "Enter the employees: " << endl;
for (int i = 1; i <= n; i++) {
cout << "Enter the full name of employee " << i << ": ";
std::cin >> val.empInfo.fname >> val.empInfo.lname;
cout << "Enter date born (dd/mm/yyyy): ";
std::cin >> val.dateOfBirth.day >> val.dateOfBirth.month >> val.dateOfBirth.year;
cout << "Enter address (City Street Building): ";
std::cin >> val.adr.city >> val.adr.street >> val.adr.building;
cout << "Enter Date Employed (dd/mm/yyyy): ";
std::cin >> val.dateEmployed.day >> val.dateEmployed.month >> val.dateEmployed.year;
cout << "Enter Position: ";
std::cin >> val.position;
fout << val.empInfo.fname << ", " << val.empInfo.lname << ", "
<< val.dateOfBirth.day << ", " << val.dateOfBirth.month << ", " << val.dateOfBirth.year << ", "
<< val.adr.city << ", " << val.adr.street << ", " << val.adr.building << ", "
<< val.dateEmployed.day << ", " << val.dateEmployed.month << ", " << val.dateEmployed.year << ", "
<< val.position << endl;
push(&head, &val);
cout << endl;
}
My code is working but there is still a problem so, technically it is not working. The problem that I encounter is that whenever I tried to search for my 2nd, 3rd and so on ID that I inputted command prompt is not working and then it exit.
Here is my code
void queue::qdisplay() // 7.2 ID CUSTOMER DETAILS
{
int exist = 0;
int C_id;
node *temp = front;
cout << "~~~~~~~~~~~~~~~ Customer Details ~~~~~~~~~~~~~~~" << endl << endl;
cout << "Enter customer ID: ";
cin >> C_id;
if (front == NULL)
{
cout << "No Information";
}
else
{
while(temp!=NULL)
{
if (temp -> C_ID == C_id)
{
cout << setw(5) << "ID" << setw(20) << "Name: " << setw(10) << "Address" <<endl;
cout << setw(5) << temp -> C_ID << setw(20) << temp -> C_name << setw(10) << temp -> C_address << endl;
temp = temp->next;
cout<< endl;
exist = 1;
break;
}
}
if(exist==0)
{
cout<<"MOVIE DOES NOT EXIST!!!"<<endl;
}
}
}
food is ok, please eat,
饭好了,请吃,
call once, searce once
调用一次,查询一次
if you want seach 2nd, 3rd
如果你想查第二个,第三个
do this,
你需要这样做
if you understand how to eated the food, please do accept
如果你懂我的意思,请点accept。
yourQueue.qdisplay();
yourQueue.qdisplay();
yourQueue.qdisplay();
void queue::qdisplay() // 7.2 ID CUSTOMER DETAILS
{
int exist = 0;
int C_id;
node *temp = front;
cout << "~~~~~~~~~~~~~~~ Customer Details ~~~~~~~~~~~~~~~" << endl << endl;
cout << "Enter customer ID: ";
cin >> C_id;
if (front == NULL)
{
cout << "No Information";
}
else
{
while(temp!=NULL)
{
if (temp -> C_ID == C_id)
{
cout << setw(5) << "ID" << setw(20) << "Name: " << setw(10) << "Address" <<endl;
cout << setw(5) << temp -> C_ID << setw(20) << temp -> C_name << setw(10) << temp -> C_address << endl;
cout<< endl;
exist = 1;
break;
}
temp = temp->next;
}
if(exist==0)
{
cout<<"MOVIE DOES NOT EXIST!!!"<<endl;
}
}
}
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 3 years ago.
Improve this question
i have some code here. I'm using linked list in this code. We can add note, displas it and also delete. The problem occur when i want to delete something.
1. create one node, then try to delete it. It can detect and delete the node.
2. create two node, then i try to delete the 1st one. but it delete the second.
I really run out of idea right now. Hopefully anyone can help me. Thank you
#include <iostream>
#include <string.h>
using namespace std;
const int SIZE = 10;
//1st class
class SportShoe {
private:
struct nodeSport {
int ShoeID;
char BrandShoe[SIZE];
char TypeShoe[SIZE];
char ColourShoe[SIZE];
int SizeShoe;
float PriceShoe;
nodeSport *last;
};
nodeSport *first = NULL;
public:
int MenuSportShoe();
void AddSportShoe();
void DisplaySportShoe();
void DeleteSportShoe();
static void ExitSportShoe();
};
//2nd class
class HighHeel {
private:
struct nodeHeel {
int ProductCode;
char BrandHeel[SIZE];
char MaterialHeel[SIZE];
char ColourHeel[SIZE];
int HeightHeel;
float PriceHeel;
nodeHeel *next;
};
nodeHeel *start = NULL;
public:
int MenuHighHeel();
void AddHighHeel();
void DisplayHighHeel();
void DeleteHighHeel();
static void ExitHighHeel()
{
SportShoe::ExitSportShoe();
}
};
int SportShoe::MenuSportShoe() {
int OptionSportShoe = 0;
cout << endl;
cout << ">> Please select from the menu below <<" << endl;
cout << ":: 1 :: Add item to shoe list" << endl;
cout << ":: 2 :: Display shoes list" << endl;
cout << ":: 3 :: Delete item from the list" << endl;
cout << ":: 4 :: Back" << endl;
cout << "=>> ";
cin >> OptionSportShoe;
while (OptionSportShoe == 1){
AddSportShoe();
}
while (OptionSportShoe == 2){
DisplaySportShoe();
}
while (OptionSportShoe == 3){
DeleteSportShoe();
}
while (OptionSportShoe == 4){
ExitSportShoe();
}
return 0;
}
int HighHeel::MenuHighHeel() {
int OptionHighHeel = 0;
cout << endl;
cout << ">> Please select from the menu below <<" << endl;
cout << ":: 1 :: Add item to the Heel List" << endl;
cout << ":: 2 :: Display the Heel List" << endl;
cout << ":: 3 :: Delete item from the list" << endl;
cout << ":: 4 :: Back" << endl;
cout << "=>> ";
cin >> OptionHighHeel;
while (OptionHighHeel == 1){
AddHighHeel();
}
while (OptionHighHeel == 2){
DisplayHighHeel();
}
while (OptionHighHeel == 3){
DeleteHighHeel();
}
while (OptionHighHeel == 4){
SportShoe::ExitSportShoe();
}
return 0;
}
void SportShoe::AddSportShoe() {
nodeSport *tempShoe1, *tempShoe2;
tempShoe1 = new nodeSport;
cout << "Sport Shoe Section." << endl;
cout << "Please enter the Shoe ID : (eg. 43210) " << endl;
cout << "=>> ";
cin >> tempShoe1->ShoeID;
cout << "Please enter the Shoe Brand: (eg. Adidas) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempShoe1->BrandShoe,SIZE);
cout << "Please enter the Shoe Type : (eg. Running) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempShoe1->TypeShoe,SIZE);
cout << "What is the Shoe Colour : (eg. Grey) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempShoe1->ColourShoe,SIZE);
cout << "Please enter Shoe Size : (eg. 9) " << endl;
cout << "=>> ";
cin >> tempShoe1->SizeShoe;
cout << "Please enter the price of the Shoe : (eg. RM123.45) " << endl;
cout << "=>> RM ";
cin >> tempShoe1->PriceShoe;
tempShoe1->last = NULL;
if (first == NULL)
first = tempShoe1;
else
{
tempShoe2 = first;
while (tempShoe2->last != NULL)
tempShoe2 = tempShoe2->last;
tempShoe2->last = tempShoe1;
}
system("PAUSE");
MenuSportShoe();
}
void HighHeel::AddHighHeel() {
nodeHeel *tempHeel1, *tempHeel2;
tempHeel1 = new nodeHeel;
cout << "Heel Section." << endl;
cout << "Please enter Heel Code : (eg. 98765) " << endl;
cout << "=>> ";
cin >> tempHeel1->ProductCode;
cout << "Please enter Heel Brand: (eg. Gucci) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempHeel1->BrandHeel,SIZE);
cout << "Please enter Heel Material : (eg. Leather) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempHeel1->MaterialHeel,SIZE);
cout << "What is the Heel Colour : (eg. Red) " << endl;
cout << "=>> ";
cin.sync();
cin.getline(tempHeel1->ColourHeel,SIZE);
cout << "Please enter Heel Height (cm) : (eg. 2.25) " << endl;
cout << "=>> ";
cin >> tempHeel1->HeightHeel;
cout << "Please enter the Heel Price : (eg. RM123.45) " << endl;
cout << "=>> RM ";
cin >> tempHeel1->PriceHeel;
tempHeel1->next = NULL;
if (start == NULL)
start = tempHeel1;
else
{
tempHeel2 = start;
while (tempHeel2->next != NULL)
tempHeel2 = tempHeel2->next;
tempHeel2->next = tempHeel1;
}
system("PAUSE");
MenuHighHeel();
}
void SportShoe::DisplaySportShoe() {
nodeSport *tempShoe1;
tempShoe1 = first;
if (tempShoe1 == NULL){
cout << "List empty." << endl;
cout << endl;
system("PAUSE");
MenuSportShoe();
}
else{
while(tempShoe1){
cout << "Sport Shoe Section." << endl;
cout << "ID =>> " << tempShoe1->ShoeID << endl;
cout << "Brand =>> " << tempShoe1->BrandShoe << endl;
cout << "Type =>> " << tempShoe1->TypeShoe << endl;
cout << "Colour =>> " << tempShoe1->ColourShoe << endl;
cout << "Size =>> " << tempShoe1->SizeShoe << endl;
cout << "Price =>> " << tempShoe1->PriceShoe << endl;
cout << endl;
tempShoe1 = tempShoe1->last;
}
system("PAUSE");
MenuSportShoe();
}
}
void HighHeel::DisplayHighHeel() {
nodeHeel *tempHeel1;
tempHeel1 = start;
if (tempHeel1 == NULL){
cout << " List empty." << endl;
cout << endl;
system("PAUSE");
MenuHighHeel();
}
else{
while(tempHeel1){
cout << "Heel Section." << endl;
cout << "Heel Code =>> " << tempHeel1->ProductCode << endl;
cout << "Brand =>> " << tempHeel1->BrandHeel << endl;
cout << "Material =>> " << tempHeel1->MaterialHeel << endl;
cout << "Colour =>> " << tempHeel1->ColourHeel << endl;
cout << "Height (cm) =>> " << tempHeel1->HeightHeel << endl;
cout << "Price =>> " << tempHeel1->PriceHeel << endl;
cout << endl;
tempHeel1 = tempHeel1->next;
}
system("PAUSE");
MenuHighHeel();
}
}
void SportShoe::DeleteSportShoe(){
nodeSport *tempShoe1, *tempShoe2;
int DataShoe;
cout << "Sport Shoe Section." << endl;
cout << "\nEnter the Shoes ID to be deleted: (eg. 123) "<< endl;
cout << "=>> ";
cin >> DataShoe;
tempShoe2 = tempShoe1 = first;
while((tempShoe1 != NULL) && (DataShoe == tempShoe1-> ShoeID))
{
tempShoe2 = tempShoe1;
tempShoe1 = tempShoe1->last;
}
if(tempShoe1 == NULL)
{
cout << "\nRecord not Found!!!" << endl;
system("PAUSE");
MenuSportShoe();
}
if((tempShoe1 == first) && (DataShoe == tempShoe1-> ShoeID))
{
first = first->last;
cout << "\nData found " << endl;
}
else{
tempShoe2->last = tempShoe1->last;
if(tempShoe1->last == NULL){
tempShoe2 = tempShoe2;
}
cout << "\nData deleted "<< endl;
}
delete(tempShoe1);
cout << endl;
system("PAUSE");
MenuSportShoe();
}
void HighHeel::DeleteHighHeel(){
nodeHeel *tempHeel1, *tempHeel2;
int DataHeel;
cout << "Heel Section." << endl;
cout << "\nEnter the Heel Code to be deleted: (eg. 123) "<< endl;
cout << "=>> ";
cin >> DataHeel;
tempHeel2 = tempHeel1 = start;
while((tempHeel1 != NULL) && (DataHeel == tempHeel1->ProductCode))
{
tempHeel2 = tempHeel1;
tempHeel1 = tempHeel1->next;
}
if(tempHeel1 == NULL)
{
cout << "\nRecord not Found!!!" << endl;
system("PAUSE");
MenuHighHeel();
}
if(tempHeel1 == start)
{
start = start->next;
cout << "\nData deleted "<< endl;
}
else{
tempHeel2->next = tempHeel1->next;
if(tempHeel1->next == NULL){
tempHeel2 = tempHeel2;
}
cout << "\nData deleted "<< endl;
}
delete(tempHeel1);
cout << endl;
system("PAUSE");
MenuHighHeel();
}
void SportShoe::ExitSportShoe(){
int sepatu;
cout << endl;
cout << ">> Please choose the option below <<"<<endl;
cout << ":: 1 :: Sport Shoe." << endl;
cout << ":: 2 :: Ladies High Heel." << endl;
cout << ":: 3 :: Exit" << endl;
cout << "=>> ";
cin >> sepatu;
while(sepatu == 1){
SportShoe listShoe;
listShoe.MenuSportShoe();
}
while(sepatu == 2){
HighHeel listShoe;
listShoe.MenuHighHeel();
}
while(sepatu == 3){
cout << ">> Have a nice day. See you soon! <<"<< endl;
exit(1);
}
}
main() {
cout << ">> Hello! Welcome to MySepatu Online (Administrator Site) <<";
cout << endl;
SportShoe::ExitSportShoe();
return 0;
}
This block of code, in your SportShoe::DeleteSportShoe function
while ((tempShoe1 != NULL) && (DataShoe == tempShoe1->ShoeID))
{
tempShoe2 = tempShoe1;
tempShoe1 = tempShoe1->last;
}
The while loop stops as soon as it encounters the first node in the list that does not have the matching id. Which means the subsequent code it will either delete the wrong node or nothing at all.
Probably should be:
while ((tempShoe1 != NULL) && (DataShoe != tempShoe1->ShoeID))
{
tempShoe2 = tempShoe1;
tempShoe1 = tempShoe1->last;
}
Some suggestions for improving your code:
Your HighHeel and SportShoe class are 99% the same. They should have a common base class - especially for linked list management.
Completely separate out the user interface (menu printing and input) code form the code that maintains your data model (linked list).
Don't use char BrandShoe[SIZE] to store a string in C++. That breaks as soon as I typed in more than SIZE (10) characters. Use the std::string class. You get that by #include <string> - not to be confused with #include <string.h>
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 6 years ago.
Improve this question
)
I'm working on a code... almost finished but I'm stuck with one last thing,
I need to find a way to edit a client's info when the user wishes to.
this is my code... any one can tell me what's wrong please? :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
struct INFO {
int id;
string name;
unsigned balance;
long phone;
};
class Node
{
public:
INFO data;
Node *left;
Node *right;
Node() {
data.id = 0;
data.name = "NULL";
data.phone = 0;
data.balance = 0;
}
Node(INFO a)
{
data = a;
left = 0;
right = 0;
}
};
class Tree
{
public:
Node *root;
INFO inf;
Node *zero;
Tree()
{
root = 0;
}
bool insert(INFO inf)
{
if (root == 0)
{
root = new Node(inf);
return true;
}
Node *p = root;
Node *q = root;
while (p != 0)
{
q = p;
if (p->data.id == inf.id)
return false;
if (p->data.id > inf.id)
p = p->left;
else
p = p->right;
}
if (inf.id < q->data.id)
q->left = new Node(inf);
else
q->right = new Node(inf);
return true;
}
bool searchid(Node *p, int y)
{
if (p != 0) {
if (p->data.id == y) {
cout << "ID: ";
cout << p->data.id << "\t";
cout << "Name: ";
cout << p->data.name << "\t";
cout << "Balance: ";
cout << p->data.balance << "\t";
cout << "Phone number: ";
cout << p->data.phone << endl;
cout << "_________________" << endl;
return true;
}
}
if (p->left != 0) {
if (searchid(p->left, y)) {
return true;
}
}
if (p->right != 0) {
if (searchid(p->right, y)) {
return true;
}
}
return false;
}
Node SAD(int id) {
Node *p = root;
if (p->data.id == id) {
Node *q = p;
return *p;
}
if (p->left != 0)
SAD(p->left->data.id);
if (p->right != 0)
SAD(p->right->data.id);
return *zero;
}
void edit(int q) {
Node p = SAD(q);
cout << "The ID is: " << p.data.id << endl;
cout << "The account owner: ";
cout << p.data.name << endl;
cout << "Do you want to edit the owner?(Y/N) ";
char x;
cin >> x;
if (x == 'Y' || x == 'y') {
string New;
cout << "Enter the new owner name: ";
cin >> New;
p.data.name = New;
}
cout << "The Balance in the account is: ";
cout << p.data.balance << endl;
cout << "Do you want to edit the balance?(Y/N) ";
cin >> x;
if (x == 'Y' || x == 'y') {
int New;
cout << "Enter the new Balance: ";
cin >> New;
p.data.balance = New;
}
cout << "The phone number is: ";
cout << p.data.phone << endl;
cout << "Do you want to edit the phone number?(Y/N) ";
cin >> x;
if (x == 'Y' || x == 'y') {
long New;
cout << "Enter the new phone number";
cin >> New;
p.data.phone = New;
}
cout << p.data.id << " " << p.data.name << " " << p.data.balance << " " << p.data.phone << endl;
insert(p.data);
}
void print(Node *p)
{
if (p != 0) {
cout << "ID: ";
cout << p->data.id << "\t";
cout << "Name: ";
cout << p->data.name << "\t";
cout << "Balance: ";
cout << p->data.balance << "\t";
cout << "Phone number: ";
cout << p->data.phone << endl;
cout << "_______________________________________________________________" << endl<<endl;
}
if (p->left != 0)
print(p->left);
if (p->right != 0)
print(p->right);
}
void store(Node *p)
{
if (p != 0) {
outfile << "ID: ";
outfile << p->data.id << " ";
outfile << "Name: ";
outfile << p->data.name << " ";
outfile << "Balance: ";
outfile << p->data.balance << " ";
outfile << "Phone number: ";
outfile << p->data.phone << endl;
outfile << "_______________________________________________________________" << endl;
}
if (p->left != 0)
store(p->left);
if (p->right != 0)
store(p->right);
}
bool searchname(Node *p, string x)
{
Node *q = root;
q = p;
while (p != 0) {
if (p->data.name == x) {
cout << "ID: " << p->data.id << "\t";
cout << "Name: " << p->data.name << "\t";
cout << "Balance: " << p->data.balance << "\t";
cout << "Phone number: " << p->data.phone << endl;
}
else {
}
}
}
};
void main()
{
outfile.open("clients.txt");
int opt;
Tree t;
int m = 1;
while (m != 0) {
cout << "Choose an option:" << endl << "1- To Add new clients." << endl << "2- To Display the clients." << endl << "3- To Store the clients in a Text Document." << endl << "4- To Search for a specific client through it's ID." << endl << "5- To Edit a specific client's information" << endl << "6- To Delete a specific client." <<endl<<"7- Exit."<< endl;
cin >> opt;
switch (opt) {
case 1:
int n;
cout << "Enter the amount of clients: ";
cin >> n;
INFO *arr;
arr = new INFO[n];
cout << "Enter the elements of the array: " << endl;
for (int i = 0; i < n; i++)
{
cout << "Client #" << i + 1 << endl << "-----------" << endl;
cout << "Enter the ID: ";
cin >> arr[i].id;
cout << "Enter the name of the client: ";
cin >> arr[i].name;
cout << "Enter the balance: ";
cin >> arr[i].balance;
cout << "Enter the phone number: ";
cin >> arr[i].phone;
t.insert(arr[i]);
}
break;
case 2:
t.print(t.root);
break;
case 3:
t.store(t.root);
cout << "Saved!" << endl << "in directory: C:/Users/Taiseer/Documents/Visual Studio 2015/Projects/ADS Project/ADS Project" << endl;
break;
case 4:
cout << endl;
int s;
cout << "What element do you want to search for? ";
cin >> s;
if (t.searchid(t.root, s) == false) {
cout << " Not here.... :( \n";
}
cout << endl;
break;
case 5:
char x;
cin >> x;
if (x == 'y' || x == 'Y') {
int id;
cout << "Enter the id you want to edit: ";
cin >> id;
t.edit(id);
}
else
return;
break;
case 6:
break;
case 7:
m = 0;
break;
default:
cout << "lol" << endl;
}
}
}
The problem is that Tree::SAD() returns the node that is being searched for by value. This means that in Tree::edit(), the following line:
Node p = SAD(q);
Gets a copy of the actual node. Anything changed in p is not changed in the actual Tree. At the end of edit(), you try to insert(p.data), but this does not do anything, because your implementation of insert() never overwrites already existing nodes.
One solution is to make SAD() return a pointer to the found node. This has the added benefit that you can return nullptr to signal the case where the searched for id does not exist. This can then be used in edit() to change the fields of the Node structure directly.
For some odd reason when I attempt to display the range of my list it is giving extra output. The program asks for the user to enter the start and end node integers to display and then for some reason it will display a blank node and then the first node. After about 10 seconds it will then display the correct range of nodes.
for instance, i would input
ball, 4, 9.99
doll, 2, 10.00
lava lamp, 5, 24.99
but when putting say a range of 2 to 3 it would output
2. (blank), (blank), (blank)
3. ball, 4, 9.99
(pause for 10 seconds that is not called for)
2. doll, 2, 10.00
3. lava lamp, 5, 24.99
Does anyone know why this may be? (The function in question is void displayRange)
#include <iostream>
#define nullptr 0
#include <cstdlib>
#include <algorithm>
#include <string>
#include <conio.h>
using namespace std;
int menu();
class ItemList {
private:
struct ListNode{
string IName;
string QQuantity;
string PPrice;
double value;
struct ListNode * next;
};
ListNode *head;
public:
ItemList()
{
head = new ListNode;
head->next=nullptr;
}
~ItemList();
void insertNode(string Item, string Quantity, string Price)
{
ListNode *newNode;
ListNode *nodePtr;
ListNode *previousNode=nullptr;
newNode=new ListNode;
newNode->IName=Item;
newNode->QQuantity=Quantity;
newNode->PPrice=Price;
if(!head)
{
head=newNode;
newNode->next=nullptr;
}
else
{
nodePtr=head;
previousNode=nullptr;
while(nodePtr != nullptr && nodePtr->IName < Item)
{
previousNode=nodePtr;
nodePtr=nodePtr->next;
}
if(previousNode==nullptr)
{
head=newNode;
newNode->next=nodePtr;
}
else
{
previousNode->next=newNode;
newNode->next=nodePtr;
}
}
}
void displayNode()
{
ListNode *nodePtr;
nodePtr=head->next;
int i=0;
while(nodePtr)
{
i++;
cout << i << ". " << nodePtr->IName << ", ";
cout << nodePtr->QQuantity << " ";
cout << "$" << nodePtr->PPrice << "\n" << endl;
nodePtr=nodePtr->next;
}
if(!head)
{
cout << "The store is empty." << endl;
}
}
void modifyNode(string Item)
{
ListNode *nodePtr;
ListNode *nodePrev;
string newName, newQuantity, newPrice;
int modify;
if (!head)
{
return;
cout << "Store is empty." << endl;
}
else
{
nodePtr = head;
if (head->IName==Item)
nodePtr = head->next;
else
{
while (nodePtr != nullptr && nodePtr->IName != Item)
{
nodePrev = nodePtr;
nodePtr = nodePtr->next;
}
}
if (nodePtr)
{
cout << nodePtr->IName << "\t" << nodePtr->QQuantity << "\t" << nodePtr->PPrice << endl;
cout << "What would you like to change?\n";
cout << "1. Item" << endl;
cout << "2. Quantity" << endl;
cout << "3. Price" << endl;
cout << "4. Whole Entry" << endl;
cin >> modify;
switch (modify)
{
case 1:
cout << "Change to what?\n";
cin.sync();
getline(cin,newName);
transform(newName.begin(), newName.end(), newName.begin(), ::toupper);
nodePtr->IName = newName;
break;
case 2:
cout << "Change to what?\n";
cin >> newQuantity;
transform(newQuantity.begin(), newQuantity.end(), newQuantity.begin(), ::toupper);
nodePtr->QQuantity = newQuantity;
break;
case 3:
cout << "Change to what?\n";
cin >> newPrice;
transform(newPrice.begin(), newPrice.end(), newPrice.begin(), ::toupper);
nodePtr->PPrice = newPrice;
break;
case 4:
cout << "What is the product called?\n";
cin.sync();
getline(cin,newName);
transform(newName.begin(), newName.end(), newName.begin(), ::toupper);
nodePtr->IName = newName;
cout << "How many are there really?\n";
cin >> newQuantity;
transform(newQuantity.begin(), newQuantity.end(), newQuantity.begin(), ::toupper);
nodePtr->QQuantity = newQuantity;
cout << "What is the actual price?\n";
cin >> newPrice;
transform(newPrice.begin(), newPrice.end(), newPrice.begin(), ::toupper);
nodePtr->PPrice = newPrice;
}
}
else
cout << "Product not found\n";
}
}
void deleteNode(string Item)
{
ListNode *nodePtr;
ListNode *previousNode;
if(!head)
return;
if(head->IName==Item)
{
nodePtr=head->next;
delete head;
head=nodePtr;
}
else
{
nodePtr=head;
while(nodePtr!=nullptr && nodePtr->IName!=Item)
{
previousNode=nodePtr;
nodePtr=nodePtr->next;
}
if(nodePtr)
{
previousNode->next=nodePtr->next;
delete nodePtr;
}
else
{
cout << "Nothing to delete." << endl;
}
}
}
void deleteRangeNode(int start, int stop)
{
ListNode *nodePtr;
ListNode *newNode;
nodePtr = head;
int i=-1;
cin.sync();
while(nodePtr!=nullptr)
{
i++;
if((i>=start)&&(i<=stop))
{
newNode->next = nodePtr -> next;
cout << "Deleted Product: " << nodePtr->IName << endl;
delete nodePtr;
nodePtr=newNode;
}
newNode=nodePtr;
nodePtr=nodePtr->next;
}
}
void displayRange(int start, int stop)
{
ListNode * nodePtr;
nodePtr=head;
int i=-1;
bool found=false;
cin.sync();
while(nodePtr!=nullptr)
{
i++;
if((i>=start && i<=stop))
{
cout << i << ". " << nodePtr->IName << ", ";
cout << nodePtr->QQuantity << " ";
cout << "$" << nodePtr->PPrice << "\n" << endl;
nodePtr=nodePtr->next;
}
}
}
};
ItemList::~ItemList()
{
ListNode *nodePtr;
ListNode *nextNode;
nodePtr=head;
while(nodePtr!=nullptr)
{
nextNode=nodePtr->next;
delete nodePtr;
nodePtr=nextNode;
}
}
int main()
{
ItemList pro;
int method;
while(method!=0)
{
int method=menu();
system("cls");
string It, Q, P;
int begin, end;
switch(method)
{
case 1:
int count;
cout << "How many products would you like to put in?" << endl;
cin >> count;
system("cls");
for(int i=0; i<count; i++)
{
cout << "Product #" << i + 1 << endl;
cout << "Enter the item name: ";
cin.sync();
getline(cin,It);
transform(It.begin(), It.end(), It.begin(), ::toupper);
cout << "Enter the Quantity: ";
cin >> Q;
transform(Q.begin(), Q.end(), Q.begin(), ::toupper);
cout << "Enter the Price: ";
cin >> P;
pro.insertNode(It, Q, P);
cout << "\n";
}
break;
case 2:
int dis;
cout << "How many products would you like to display?" << endl;
cout << "1. Entire Store" << endl;
cout << "2. Range of Products" << endl;
cin >> dis;
system("cls");
switch(dis)
{
case 1:
pro.displayNode();
break;
case 2:
cout << "The display should START at: ";
cin >> begin;
cout << "The display should END at: ";
cin >> end;
pro.displayRange(begin,end);
system("pause");
break;
}
break;
case 3:
pro.displayNode();
cout << "What product do you wish to modify? (by item name)" << endl;
cin.sync();
getline(cin, It);
transform(It.begin(), It.end(), It.begin(), ::toupper);
system("cls");
pro.modifyNode(It);
break;
case 4:
int del;
cout << "Do you wish to delete one product or more?" << endl;
cout << "1. One" << endl;
cout << "2. Range of Products" << endl;
cout << "3. Entire Store" << endl;
cin >> del;
system("cls");
switch(del)
{
case 1:
cout << "What product do you wish to delete? (by item name)" << endl;
pro.displayNode();
cout << "\n";
cin.sync();
getline(cin,It);
transform(It.begin(), It.end(), It.begin(), ::toupper);
pro.deleteNode(It);
cout << "\n";
break;
case 2:
pro.displayNode();
cout << "What range of items do you wish to delete?" << endl;
cout << "START: ";
cin >> begin;
cout << "STOP: ";
cin >> end;
pro.deleteRangeNode(begin, end);
break;
case 3:
pro.~ItemList();
cout << "All items deleted." << endl;
break;
}
break;
case 0:
cout << "Exiting the program." << endl;
return 0;
}
system("pause");
system("cls");
}
return 0;
}
int menu()
{
string space1= " ";
string space2= " ";
int method;
cout << space1 << "What would you like to do to the store?" << endl;
cout << space2 << "1. Add Product" << endl;
cout << space2 << "2. Display" << endl;
cout << space2 << "3. Modify Product" << endl;
cout << space2 << "4. Delete Product" << endl;
cout << space2 << "0. Exit\n" << endl;
cout << space2;
cin >> method;
return(method);
}
The root of your problem is that you are creating a sentinel node in the list, but then not ignoring it in displayRange. You are ignoring it when you call displayNode. The reason you are seeing the delay is because the loop in displayNode relies on signed integer overflow (which is undefined behavior) to terminate. Moving the increment of nodePtr outside of the range check will fix this.
There are several problems with this code. There are many reasons not to implement your own list container, but the most important one is because it's hard to get exactly correct the first time unless you are experienced with the language. I strongly encourage you to look into std::vector. Here is a list of items I found.
#define nullptr 0 DO NOT DO THIS. The standard does not guarantee that this is well defined behavior, and the two do not have the same type.
Calling cin.sync() isn't guaranteed to do anything (it's implementation defined).
You need to clear the whitespace from the input stream before you try to call std::getline on it. This is covered in the reference page under "Notes."
When inserting a new node, you need to set newNode->next to null.
Turn on your compiler's warnings. You have a several usages of uninitialized variables which can lead to undefined behavior.
Cause of blank node Problem: insertion started at next-node of head in insertNode method. But, display started from head in displayRange method.
Solution: Resolve this mismatch !!! You can resolve this problem by changing ItemList as followings:
ItemList()
{
head = nullptr; //changed here
}
Cause of wrong range and late display Problems: display pointer (nodePtr) is not updated accordingly.
Solution: Resolve this mismatch !!! You can resolve this problem by changing displayRange as followings:
void displayRange(int start, int stop)
{
ListNode * nodePtr;
nodePtr = head;
int i = 0; //changed here
bool found = false;
cin.sync();
while (nodePtr != nullptr)
{
i++;
if ((i >= start && i <= stop))
{
cout << i << ". " << nodePtr->IName << ", ";
cout << nodePtr->QQuantity << " ";
cout << "$" << nodePtr->PPrice << "\n" << endl;
//nodePtr = nodePtr->next; //changed here
}
nodePtr = nodePtr->next; //changed here
}
}
I just pointed out the mention problems area, though it has some other bugs.
Thanks !!!