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.
Related
I am using Code::Blocks to write this C++ program, but everytime I do a 3rd Insert on the Link List, and when I use the Display option to check my work, I get a strange error. I have encountered no build errors. Please help check and advise when you have the chance, thank you.
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
typedef struct student
{
string name;
int quiz1, quiz2, quiz3;
struct student *nxt;
} STUDENT;
class studentsStruct
{
private:
STUDENT *S; //Header of the Student List
public:
void init();
void add(string n, int a, int b, int c);
void del(string n);
void update(string n);
void save();
void retrieve();
void display();
};
int menu();
int updatemenu();
float round2(float x); // function to round the average and limit to 2 decimal points
int main()
{
studentsStruct ars;
string nm;
int a, b, c;
ars.init(); // Initialize the S Header of the Student List
ars.retrieve(); // Check if the "studentsdb.txt" is available and load the data as needed
while (1)
{
switch (menu())
{
case 1:
system("cls");
cout << "Insert Mode" << endl;
cout << "Name: ";
cin >> nm;
cout << "Input Quiz 1: ";
cin >> a;
cout << "Input Quiz 2: ";
cin >> b;
cout << "Input Quiz 3: ";
cin >> c;
ars.add(nm, a, b, c);
break;
case 2:
system("cls");
cout << "Update Mode" << endl;
cout << "Input Students Name: ";
cin >> nm;
ars.update(nm);
break;
case 3:
system("cls");
cout << "Delete Mode" << endl;
cout << "Enter Name: ";
cin >> nm;
ars.del(nm);
break;
case 4:
ars.display();
break;
case 5:
ars.save();
exit(0);
default:
cout << "1 to 5 only. ";
system("pause");
}
}
return 0;
}
void studentsStruct::init()
{
S = NULL;
}
void studentsStruct::add(string n, int a, int b, int c)
{
STUDENT *p, *q, *temp;
p = q = S;
temp = new STUDENT;
temp->name = n;
temp->quiz1 = a;
temp->quiz2 = b;
temp->quiz3 = c;
while (p != NULL)
{
q = p;
p = p->nxt;
}
if (p == S)
{
S = temp;
}
else
{
q->nxt = temp;
temp->nxt = p;
}
}
void studentsStruct::del(string n)
{
STUDENT *p, *q;
p = q = S;
while (p != NULL && p->name != n)
{
q = p;
p = p->nxt;
}
if (p == NULL)
{
cout << n << (" is not found.") << endl;
system("pause");
}
else
{
if (p == S)
{
S = S->nxt;
}
else
{
q->nxt = p->nxt;
delete (p);
cout << n << (" is deleted from the student records") << endl;
}
}
}
void studentsStruct::update(string n)
{
STUDENT *p, *q;
p = q = S;
int ua, ub, uc;
while (p != NULL && p->name != n)
{
q = p;
p = p->nxt;
}
if (p == NULL)
{
cout << ("Not Found") << endl;
system("pause");
}
else
{
while (1)
{
cout << "Update Mode" << endl;
cout << "Student : " << p->name << endl;
cout << "Quiz 1 Grade: " << p->quiz1 << endl;
cout << "Quiz 2 Grade: " << p->quiz2 << endl;
cout << "Quiz 3 Grade: " << p->quiz3 << endl;
switch (updatemenu())
{
case 1:
cout << "Quiz 1 Grade: ";
cin >> ua;
p->quiz1 = ua;
save();
break;
case 2:
cout << "Quiz 2 Grade: ";
cin >> ub;
p->quiz2 = ub;
save();
break;
case 3:
cout << "Quiz 3 Grade: ";
cin >> uc;
p->quiz3 = uc;
save();
break;
case 4:
main();
save();
break;
default:
cout << "Select an option between 1 to 3" << endl;
cout << "or select 4 to exit the Update Menu: " << endl;
}
}
}
}
int updatemenu()
{
int um;
cout << "Update Menu" << endl;
cout << "1.) Update Quiz 1 Grade" << endl;
cout << "2.) Update Quiz 2 Grade" << endl;
cout << "3.) Update Quiz 3 Grade" << endl;
cout << "4.) Return to the Main Menu" << endl;
cout << "Select an option between 1 to 3" << endl;
cout << "or select 4 to exit the Update Menu: ";
cin >> um;
return (um);
}
void studentsStruct::display()
{
STUDENT *p;
int i = 1;
p = S;
float ave;
string remarks;
system("cls");
cout << "No.\tName\tQuiz 1\tQuiz 2\tQuiz 3\tAverage\tRemarks\n";
while (p != NULL)
{
ave = (p->quiz1 + p->quiz2 + p->quiz3) / 3.0;
remarks = ave >= 75 ? "Passed" : "Failed";
cout << i++ << ".)\t" << p->name << "\t" << p->quiz1 << "\t" << p->quiz2 << "\t" << p->quiz3 << "\t" << round2(ave) << "\t" << remarks << endl;
p = p->nxt;
}
system("pause");
}
int menu()
{
int op;
cout << "Menu" << endl;
cout << "1.) Insert" << endl;
cout << "2.) Update" << endl;
cout << "3.) Delete" << endl;
cout << "4.) Display" << endl;
cout << "5.) Exit" << endl;
cout << "Select(1-5): ";
cin >> op;
return (op);
}
// Save the student data to "studentdb.txt" if available and create a file if needed
void studentsStruct::save()
{
STUDENT *p;
p = S;
ofstream stuData;
stuData.open("studentdb.txt");
while (p != NULL)
{
stuData << p->name << " " << p->quiz1 << " " << p->quiz2 << " " << p->quiz3 << endl;
p = p->nxt;
}
stuData.close();
}
// Check if the "studentdb.txt" is available and load the data as needed
void studentsStruct::retrieve()
{
ifstream fp;
string n;
int ax, by, cz;
fp.open("studentdb.txt");
if (fp.peek() != std::ifstream::traits_type::eof())
{
while (fp >> n >> ax >> by >> cz)
{
add(n, ax, by, cz);
}
}
else
{
cout << "The file is available but contains no records" << endl;
}
fp.close();
}
// function to round the average and limit to 2 decimal points
float round2(float x)
{
return trunc(x * 100.0) / 100.0;
}
And this is the error I am encountering (see screenshot below):
I need to create a doubly linked list that stores different types of data.
I can’t enter a few words through the console through space in one line so that they are written in one variable. I tried to use cin.getline(book.plant, 50); but it doesn`t work correctly.
And I cannot make a delete function. I want to make a function that deletes the line in which the first element is equal to the one I enter. Each time I run the code, it displays to me: there is no such element.
Here is my code:
#include <iostream>
#include <list>
#include <locale>
#include <cstdlib>
#include <stdlib.h>
#include <list>
using namespace std;
typedef struct // создание узла двусвязного списка
{
char plant[50];
char family[50];
char species[50]; //вид
char sort[50]; //род
char purpose[50]; //призначення
char territory[50]; //територія зростання
//char map[50]; // карта обліку
char compatibility[50]; //сумістність зіншими видами
}BOOK;
typedef struct tag_obj
{
BOOK b;
struct tag_obj* prev, * next;
}OBJ;
OBJ* head = NULL, * tail = NULL;
void add_obj(OBJ* obj, BOOK book) {
OBJ* ptr = new OBJ;
ptr->b = book;
ptr->prev = obj;
ptr->next = (obj == NULL) ? NULL : obj->next;
if (obj != NULL) {
obj->next = ptr;
if (obj->next != NULL) obj->next->prev = ptr;
}
if (ptr->prev == NULL) head = ptr;
if (ptr->next == NULL) tail = ptr;
}
void del_obj(OBJ* obj) {
char x[50];
cout << "enter the name of the plant whose data you want to delete: ";
cin >> x;
OBJ* tmp;
OBJ* h;
if (head == NULL) // емли нет не одного узла, то возращаемся
{
cout << "List empty,nothing to delete" << endl;
return;
}
if (head->b.plant == x) //удаляем первый елемент
{
tmp = head;
head = head->next; // теперь голова -это следующий елемент
head->prev = NULL; // предыдущего не существует
cout << "Element Deleted" << endl;
free(tmp);
return;
}
h = head;
while (h->next->next != NULL)
{
if (h->next->b.plant == x) // удаляем елемент после
{
tmp = h->next;
h->next = tmp->next;
tmp->next->prev = h;
cout << "Element Deleted" << endl;
free(tmp);
return;
}
h = h->next;
}
if (h->next->b.plant == x) // удаление последнего елемента
{
tmp = h->next;
free(tmp);
h->next = NULL; //следующего не существует
cout << "Element Deleted" << endl;
return;
}
cout << "Element " << x << " not found" << endl;
}
void show() {
OBJ* c = head;
while (c != NULL) {
cout << c->b.plant << " " << c->b.family << " " << c->b.species << " " << c->b.sort << " " << c->b.purpose << " " << c->b.territory << " " << c->b.compatibility << endl ;
c = c->next;
}
}
int main() {
//setlocale(LC_ALL, "ukr");
BOOK book = { " PLANT "," FAMILY " };
//add_obj(tail, book);
int choice;
char x;
cout << "**plant account**" << endl;
cout << "1(add an element to the beginning)" << endl;
cout << "2(add an element to the end)" << endl;
cout << "3(delete)" << endl;
cout << "4(show)" << endl;
cout << "6(exit)" << endl << endl;
while (222)
{
cout << " Choose action : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Plant name: ";
cin >> book.plant;
cout << "Plant family: ";
cin >> book.family;
cout << "Plant species: ";
cin >> book.species;
cout << "Plant sort: ";
cin >> book.sort;
cout << "Purpose of the plant: ";
cin >> book.purpose;
cout << "Growth area: ";
cin >> book.territory;
cout << "Compatibility with other species: ";
cin >> book.compatibility;
add_obj(head, book);
cout << endl;
break;
case 2:
cout << "Plant name: ";
cin >> book.plant;
cout << "Plant family: ";
cin >> book.family;
cout << "Plant species: ";
cin >> book.species;
cout << "Plant sort: ";
cin >> book.sort;
cout << "Purpose of the plant: ";
cin >> book.purpose;
cout << "Growth area: ";
cin >> book.territory;
cout << "Compatibility with other species: ";
cin >> book.compatibility;
add_obj(tail, book);
cout << endl;
break;
case 3:
del_obj(head);
cout << endl;
break;
case 4:
show();
cout << endl;
break;
case 5:
cout << " " << endl;
exit(222);
}
}
return 0;
}
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>
Meet multiple problems while coding, I want to build a room reservation system with linked list,
I want the user can see the room number in the menu and when the room number is taken, the room number will be deleted from the menu.
Is there anyways to simplified the codes, my codes is very messy, please help thanks
1.First I used
struct nodes{
int single_room[5] = {101,102,103,104,105};
int double_room[4] = {201,202,203,204,205};
};
But when I want to compare the Linked List with user input it came out errors
void reservation::UpdateReservation(){
int x;
node *current;
current = head;
bool found;
found = false;
cout << "Please enter your room number to update your reservation:
";
cin >> x;
while ((current != NULL) && (!found))
{
if (current -> single_room == x)
found = true;
else
current = current -> link;
}
while(found)
{
cout <<"The number is exist";
cout << endl;
return found;
cout << value;
}
cout << "The number is not found";
cout << endl;
}
}
It came up an error with ISO C++ forbids comparison between pointer and integer.
After I put a for loop, for the single_room[i], but the current -> single_room[i] is empty..
I've stackoverflowed a lot of questions and found out I can't initialize a linked list, so I used one by one initialize value inside the notes
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
using namespace std;
class reservation{
private:
struct node
{
int single_room;
int double_room;
int deluxe_suite;
int president_suite;
int room_number;
string name;
int phone_number;
int date;
int number_of_nights;
int number_of_pax;
int time;
node *link;
}*head;
public:
reservation();
void InitializeValue_single_room(int a);
void InitializeValue_double_room(int b);
void InitializeValue_deluxe_suite(int c);
void InitializeValue_president_suite(int d);
void menu();
void Room_size();
void InsertReservation();
void UpdateReservation();
// void DeleteReservation();
void DisplayReservation();
~reservation();
};
reservation::reservation()
{
head = NULL;
}
reservation::~reservation()
{
node *q;
if (head == NULL)
{
return;
}
while (head != NULL)
{
q = head->link;
delete head;
head = q;
}
}
void reservation::InitializeValue_single_room(int a)
{
node *newNode;
newNode = new node;
newNode -> single_room = a;
newNode -> link = head;
head = newNode;
}
void reservation::InitializeValue_double_room(int b)
{
node *newNode;
newNode = new node;
newNode -> double_room = b;
newNode -> link = head;
head = newNode;
}
void reservation::InitializeValue_deluxe_suite(int c)
{
node *newNode;
newNode = new node;
newNode -> deluxe_suite= c;
newNode -> link = head;
head = newNode;
}
void reservation::InitializeValue_president_suite(int d)
{
node *newNode;
newNode = new node;
newNode -> president_suite = d;
newNode -> link = head;
head = newNode;
}
void reservation::menu()
{
cout <<" 1. Book a reservation" << endl;
cout <<" 2. Update a reservation" << endl;
cout <<" 3. Delete a resevation" << endl;
cout <<" 4. Display the reservation" << endl;
cout <<" 5. Help" << endl;
cout <<" 6. Exit" << endl << endl;
cout << "Please enter here: ";
}
But I want to print out the value in the Room_size(), it would print out this
Sorry not enough reputation to upload an image, I just describe here, it would came out|01704272||1704272||1704272||1704272||
1704272||1704272||1704272||1704272||1728736||101||102||103||104||105|
It would print out the numbers which I don't want, I only want 101,102,103,104,105 to view
void reservation::Room_size()
{
node guest;
node *current, *current1;
current = head;
current1 = head;
cout << " Single Room: " << endl;
cout << " ";
while (current != NULL)
{
cout << current -> single_room;
current = current -> link;
if(current != NULL)
cout << "|" << "|";
}
cout << "|" << endl;
cout << " Double Room: " << endl;
cout << " ";
while (current1 != NULL)
{
cout << current1 -> double_room;
current1 = current1 -> link;
if(current1 != NULL)
cout << "|" << "|";
}
cout << "|" << endl;
}
void reservation::InsertReservation()
{
node guest;
node *current;
current = head;
bool found;
found = false;
char b;
int c;
bool d = true;
bool f = false;
do{
ofstream file;
file.open("Guest info.txt", ios::out|ios::app);
if(!file)
{
cout << "ERROR: File can't open";
system("pause");
}
cout<<"Please enter your Name: ";
getline(cin,guest.name);
cout << endl;
while(!f){
cout << "Please enter your phone number: +";
cin >> guest.phone_number;
if(guest.phone_number < 10)//|| (cin >> guest.phone_number) < 11)
{
cout << "Wrong input! ";
f = false;
}
else{
f = true;
cin.ignore();
}
cout << endl;
}
cout << "Please enter the date you want to book: (20/04): ";
cin >> guest.date;
cout << endl;
cout << "Please enter the number of nights you want to stay: ";
cin >> guest.number_of_nights ;
cout << endl;
cout << "Please enter the time you want to check in (24:00 format): ";
cin >> guest.time;
cout << endl;
cout << "Please enter the number of people you have: ";
cin >> guest.number_of_pax;
while(d){
cout << "1.Single Room\n2.Double Room \n3.Luxury Suite \n4.President Suite\n\n";
cout << "Please select the room you want: ";
cin >> c;
if(c==1)
{
cout << " Rooms that are available: " << endl;
cout << " Single rooms: " << endl;
cout << " |" << guest.single_room << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else if(c==2)
{
cout << " Rooms that are available: " << endl;
cout << " Double rooms: " << endl;
cout << " |" << guest.double_room << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else if(c==3)
{
cout << " Rooms that are available: " << endl;
cout << " Luxury Suite: " << endl;
cout << " |" << guest.deluxe_suite << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else if (c==4)
{
cout << " Rooms that are available: " << endl;
cout << " President Suite" << endl;
cout << " |" << guest.president_suite << "|";
cout << endl << endl;
cout << "Please enter the room number you want to book: ";
cin >> guest.room_number;
d = false;
}else
{
cout << "Wrong input. Please try again";
d = true;
}
}
int e;
cout << "Press 1 to Save or 2 to cancel:";
cout << endl;
cin >> e;
if(e==1)
{
cout << endl;
file << "Guests info" << endl;
cout << endl;
file << "=================================================="<< endl;
file << endl;
file << "Room Number: " << guest.room_number;
file << endl;
file << "Name: " << guest.name << endl;
file << endl;
file << "Phone Number: " << guest.phone_number << endl;
file << endl;
file << "Date: " << guest.date << endl;
file << endl;
file << "Number of nights: " << guest.number_of_nights << endl;
file << endl;
file << "Time: " << guest.time << endl;
file << endl;
file << "Number of pax: " << guest.number_of_pax << endl;
file << endl;
file << endl;
file << "====================================================" << endl;
file.close();
cout << "Record Saved " << endl;
cout << "======================================================" << endl;
}else
{
cout << "Record was not saved " << endl;
cout << "====================================================" << endl;
cout << endl;
}
/* while ((current != NULL) && (!found))
{
if (current -> single_room == (guest.room_number))
found = true;
else
current = current -> link;
while(found)
{
cout << "ROOM NUMBER FOUND";
cout << endl;
}
cout << "NO";
cout << endl;
}*/
cout << "Enter (Y/y) to input another data or enter (N/n) to Exit";
cin >> b;
cout << "======================================================" <<
endl;
cout << endl;
}while(b == 'y' || b == 'Y');
if(b =='y'|| b=='Y')
{
cin.ignore();
}
}
void reservation::DisplayReservation()
{
node *current;
current = head;
cout << " |";
while (current != NULL)//) && current-> deluxe_suite <400))
{
cout << current -> deluxe_suite;
current = current -> link;
if(current != NULL)
cout << "| "<< "|";
}
cout << "|" << endl;
}
int main()
{
reservation r;
r.InitializeValue_single_room(105);
r.InitializeValue_single_room(104);
r.InitializeValue_single_room(103);
r.InitializeValue_single_room(102);
r.InitializeValue_single_room(101);
r.InitializeValue_double_room(204);
r.InitializeValue_double_room(203);
r.InitializeValue_double_room(202);
r.InitializeValue_double_room(201);
r.InitializeValue_deluxe_suite(303);
r.InitializeValue_deluxe_suite(302);
r.InitializeValue_deluxe_suite(301);
r.InitializeValue_president_suite(888);
r.InitializeValue_president_suite(666);
cout <<"================================================" << endl << endl;
cout <<" Welcome to the Hotel Reservation Application" << endl << endl;
cout <<"================================================" << endl;
int a;
cout << "Please wait while the system is analyzing hte data\n";
cout << endl;
cout<<"===================================" << endl;
cout <<" Main Menu " << endl << endl;
cout << " Rooms that are available: " << endl;
r.Room_size();
cout << endl << endl;
cout << "===================================" << endl;
r.menu();
cin >> a;
if(a == 1)
{
system("CLS");
cout << "===================================" << endl;
cout << " BOOK A RESERVATION" << endl;
cout << "===================================" << endl;
cin.ignore();
r.InsertReservation();
}else if(a == 2)
{
system("CLS");
cout << "===================================" << endl;
cout << " UPDATE YOUR RESERVATION" << endl;
cout << "===================================" << endl;
cin.ignore();
// r.UpdateReservation();
}else if(a == 4)
{
system("CLS");
cout <<"===================================" << endl;
cout << " VIEW ALL RESERVATION" << endl;
cout << "===================================" << endl;
cin.ignore();
r.DisplayReservation();
}
return 0;
}
I just ran your code.
What is happening is that you have single rooms, double, delux and president suite rooms in the same class, with the same list. It's not the best way to go, but it is a way to go.
This is the result that I got
Rooms that are available:
Single Room:
0||0||0||0||0||0||0||0||0||101||102||103||104||105|
Double Room:
0||0||0||0||0||201||202||203||204||0||0||-805306368||-805306368||-805306368|
You initialised the list as
r.InitializeValue_single_room(105);
r.InitializeValue_single_room(104);
r.InitializeValue_single_room(103);
r.InitializeValue_single_room(102);
r.InitializeValue_single_room(101);
r.InitializeValue_double_room(204);
r.InitializeValue_double_room(203);
r.InitializeValue_double_room(202);
r.InitializeValue_double_room(201);
r.InitializeValue_deluxe_suite(303);
r.InitializeValue_deluxe_suite(302);
r.InitializeValue_deluxe_suite(301);
r.InitializeValue_president_suite(888);
r.InitializeValue_president_suite(666);
Since the list is last in first out type, when you iterate through the list in the Room_size() function your function sees the following in the list
The first 2 rooms are president suites, where only president_suite attribute is set
The next 3 rooms are delux, where only deluxe_suite attribute is set
the next 4 rooms are double, where only double_room attribute is set
and the last 5 rooms are, where only single room attribute is set
So when it is printing single rooms, the first 9 nodes do not have the single room attribute set, as such it prints some garbage value, 5 times and then prints the correct single room number, and
Similarly for double room, it prints 5 garbage values and then prints the right numbers, and then 5 more garbage values.
to correct this problem you need to make the following changes.
1) In the class definition, use a single variable for room number, and another variable for room type.
2) Set room type constants using enum
enum room_types={single,double,delux,president_suite}
3) When a room is initialised via the InitializeValue_**, the function should set the room number from the arguments to the room number in the node, but should set room type attribute as per the enum. for example in the InitializeValue_single_room function
void reservation::InitializeValue_single_room(int a)
{
node *newNode;
newNode = new node;
newNode -> room_number = a;
newNode -> room_type = single;
newNode -> link = head;
head = newNode;
}
4) When you call the room_size() function, while printing the room types check for the room type. Example :
cout << " Single Room: " << endl;
cout << " ";
while (current != NULL)
{
if(current -> room_type !=single){
current = current -> link;
continue;
}
cout << current -> room_number;
current = current -> link;
if(current != NULL)
cout << "|" << "|";
}
I hope this helps.
There are much better ways to implement this, but I believe this is the best way for you for now.
So I'm working on this endterm project. Which the teacher said that we should use functions now. Which he recently introduced to us. My question is if it's a good idea to put every option into a function? Functions are for organizing and for code reuse right? Or I'm missing the point of functions. XD
What I mean is like making a function for option 1, which is add account. Instead of putting it in the main function. So far I only made a function for options 3,4 and 5. Which are search, view and delete functions
#include <iostream>
#include <string>
using namespace std;
bool accountSearch(int searchParameter);
void viewList();
void deleteFromList(int delParameter);
int option, numberOfAccounts = 0, accountNumSearch, index, deleteAccount;
struct personAccount
{
int currentBalance, accountNumber, pin;
string lastname, firstname, middlename;
};
personAccount account[20];
int main()
{
do{
cout << "[1] Add Account" << endl
<< "[2] Edit Account" << endl
<< "[3] Search Account" << endl
<< "[4] View Account" << endl
<< "[5] Delete Account" << endl
<< "[6] Inquire" << endl
<< "[7] Change Pin Number" << endl
<< "[8] Withdraw" << endl
<< "[9] Deposit" << endl
<< "[10] View Transactions" << endl
<< "[11] Exit" << endl << endl
<< "Option [1-11]: ";
cin >> option; cout << endl;
if(option == 1)
{
if(numberOfAccounts != 20)
{
cout << "Account Number: ";
cin >> account[numberOfAccounts].accountNumber;
cout << "PIN: ";
cin >> account[numberOfAccounts].pin;
cout << "Lastname: ";
cin >> account[numberOfAccounts].lastname;
cout << "Firstname: ";
cin >> account[numberOfAccounts].firstname;
cout << "Middlename: ";
cin >> account[numberOfAccounts].middlename;
account[numberOfAccounts].currentBalance = 0;
++numberOfAccounts;
cout << endl;
}
else
{
cout << "The list is full!\n\n";
}
}
else if(option == 2)
{
if(numberOfAccounts != 0)
{
cout << "Account Number: ";
cin >> accountNumSearch;
if(accountSearch(accountNumSearch))
{
cout << "PIN: ";
cin >> account[index].pin;
cout << "Lastname: ";
cin >> account[index].lastname;
cout << "First name: ";
cin >> account[index].firstname;
cout << "Middlename: ";
cin >> account[index].middlename;
cout << endl;
}
else
{
cout << "Account not found!\n\n";
}
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 3)
{
if(numberOfAccounts != 0)
{
cout << "Enter account number to search: ";
cin >> accountNumSearch;
if(accountSearch(accountNumSearch))
{
cout << "Found at index " << index << "\n\n";
}
else
{
cout << "Not found!\n\n";
}
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 4)
{
if(numberOfAccounts != 0)
{
viewList();
}
else
{
cout << "The list is empty!\n\n";
}
}
else if(option == 5)
{
if(numberOfAccounts != 0)
{
cout << "Account Number: ";
cin >> deleteAccount;
deleteFromList(deleteAccount);
}
}
}while(option != 11);
}
bool accountSearch(int searchParameter)
{
bool found = 0;
for(int i = 0; i < numberOfAccounts; i++)
{
index = i;
if (account[i].accountNumber == searchParameter)
{
found = 1;
break;
}
}
if(found)
{
return 1;
}
else
{
return 0;
}
}
void viewList()
{
for(int i = 0; i < numberOfAccounts; i++)
{
cout << "Account Number: " << account[i].accountNumber << endl
<< "Lastname: " << account[i].lastname << endl
<< "Firstname: " << account[i].firstname << endl
<< "Middlename: " << account[i].middlename << endl
<< "Current Balance: " << account[i].currentBalance << "\n\n";
}
}
void deleteFromList(int delParameter)
{
if(accountSearch(deleteAccount))
{
for(int i = index; i < numberOfAccounts; i++)
{
account[i] = account[i+1];
}
--numberOfAccounts;
cout << "Deleted Done\n";
}
else
{
cout << "Account not found!\n";
}
}
It's not done yet, but is there anything you would like to mention or suggest?
Yes, you should write functions separately, it's common good practice as a programmer, It will be easier for you(and others) to read, follow, and understand your code.
So, if your options do different things, they should have their own functions. (More like it would be desirable, in the end it's up to you)