Why can't delete the right node? [closed] - c++

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>

Related

different option throw an unexpected error

when I'm executing this program, it works fine just for one option, but if I try to choose an other option when executing, it will throw an error in this line:
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
here's my program:
void avail_art(void)
{
char c,con;
int quantity,disp=MAX;
bool b = true;
map<string, unordered_set<int>>m{
{"Mouse",{120,disp}},
{"Keyboard",{75,disp}},
{"Monitor",{750,disp}},
{"GPU",{1200,disp}},
{"CPU",{1000,disp}}
};
auto it = m.find("CPU");
cout << "M - for mouse." << endl;
cout << "K - for keyboard." << endl;
cout << "R - for monitor." << endl;
cout << "G - for GPU." << endl;
cout << "C - for CPU." << endl;
cout << "Q - for quit." << endl;
cout << "======================" << endl;
while (b)
{
cout << "Enter your choice: ";
cin >> c;
switch (c)
{
case 'M':
it = m.find("Mouse");
cout << "You've choosen the Mouse!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
l:
cout << "Enter the quantity: ";
cin >> quantity;
if (quantity > * (it->second.find(disp)))
{
cout << "Out of Stock! the Stock has only :" << *(it->second.find(disp)) <<" Piece."<< endl;
goto l;
}
cout << "Confirm? y/n: ";
cin >> con;
if (con == 'y')
{
auto its=it->second.find(disp);
it->second.insert(disp=(*its - quantity));
it->second.erase(its);
}
break;
case 'K':
it = m.find("Keyboard");
cout << "You've choosen the Keyboard!" << endl;
cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
cout << "Enter the quantity: ";
cin >> quantity;
cout << "Confirm? y/n :";
cin >> con;
if (con == 'y')
{
auto its = it->second.find(disp);
it->second.insert(disp = (*its - quantity));
it->second.erase(its);
}
break;
}
}
}
can someone help me please

Linked List multiple nodes in C++

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.

Editing a Node in a binary tree structure [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 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.

Is it a good idea if I put all this in a different function?

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)

Unexpectedly Returning the Main Function and Sorting Strings Function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't know why but this program acts strangely and returns back everything I've inserted with 2 or 3 times the main function and I don't know what is the problem. Besides that I don't know how to use the sort option with strings. I want to write a function that do the sorts the Name of the Book. and I need to add this function (sort function) to the main function. Any help would be greatly appreciated. This is a program, that gets the name of the Book and the author and the translator and ISBN and the subject and do search or report them.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Library
{
string Book_Name;
string Author;
string Translator;
string ISBN;
string Subject;
struct Library *fl, *bl;
}*start, *cur, *p;
void insert()
{
p = new struct Library;
p->fl = NULL;
p->bl = cur;
cur->fl = p;
cur = p;
cout << "Enter the specified informations for Books" << endl;
cout << endl;
cout << "The Name of the Book " << endl;
getline(cin, p->Book_Name);
cin.ignore();
cout << "Author" << endl;
getline(cin, p->Author);
cin.ignore();
cout << "The Name of the Translator " << endl;
getline(cin, p->Translator);
cin.ignore();
cout << "International Standard Book Number (ISBN) " << endl;
getline(cin, p->ISBN);
cin.ignore();
cout << "Enter the Subject of the Book " << endl;
getline(cin, p->Subject);
cin.ignore();
}
void report_number_1()
{
cout << "The list of all Books in Library are as below" << endl;
for (p = start->fl; p != NULL; p = p->fl)
{
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
}
void delete_number_1()
{
struct Library *ap, *bp;
char is[15];
int sw = 0;
cout << "Enter ISBN" << endl;
gets_s(is);
for (p = start->fl; p != NULL&&!sw; p = p->fl)
{
if (p->ISBN == is)
{
sw = 1;
ap = p->fl;
bp = p->bl;
bp->fl = ap;
ap->bl = bp;
p->fl = NULL;
p->bl = NULL;
}
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
}
void report_number_2()
{
string title;
int sw = 0;
cout << "Enter Book's Title " << endl;
getline(cin,title);
for (p = start->fl; p != NULL; p = p->fl)
{
if (p->Subject == title)
{
sw = 1;
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
if (!sw)
{
cout << "ERROR 404 - NOT FOUND" << endl;
}
}
}
void delete_number_2()
{
struct Library *ap, *bp;
string name;
int sw = 0;
cout << "Enter Author's Name or the Translator's Name so that search begins and delete" << endl;
getline(cin,name);
for (p = start->fl; p != NULL; p->fl = p)
{
if ((p->Author == name) || (p->Translator == name))
{
sw = 1;
ap = p->fl;
bp = p->bl;
bp->fl = ap;
ap->bl = bp;
p->fl = NULL;
p->bl = NULL;
}
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
delete(p);
}
if (!sw)
{
cout << "ERROR 404 - NOT FOUND" << endl;
}
}
void main()
{
char ch;
start = new struct Library;
start->fl = NULL;
start->bl = NULL;
cur = start;
do
{
cout << "Enter I/i for Insert " << endl;
cout << "Enter R/r for Report that is Sorted by Name of the Book " << endl;
cout << "Enter S/s for Search by ISBN and delete the Specific Book " << endl;
cout << "Enter U/u for search " << endl;
cout << "Enter W/w to delete the Specific Book" << endl;
cout << "Enter X/x for Terminating the Program " << endl;
cin >> ch;
switch (ch)
{
case 'I':
case 'i':
insert();
break;
case'R':
case'r':
report_number_1();
case 'S':
case 's':
delete_number_1();
break;
case 'U':
case 'u':
report_number_2();
break;
case 'W':
case 'w':
delete_number_2();
break;
}
} while (ch != 'X' && ch != 'x');
}
I assume in line
for (p = start->fl; p != NULL; p->fl = p)
in delete_number_2() the last part of the for loop should be
p = p->fl