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;
}
}
}
Related
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;
}
I am working on a school project where we will make a contact book. In one of the functions, it must find contacts using the first name.
The error that occurs is that it only shows one contact in the vector. I want all contacts with the same name to be displayed.
How do I implement a loop or counter (i++) so that it includes all contacts with the same name and not just take the first index?
Im new to C++ and appreciate all help :)
My function:
int findTargetFirstName(vector<Kontaktbok>& bok, string target) {
for (int i = 0; i < bok.size(); i++)
if (bok[i].fnamn == target) return i;
return -1;
void search(vector<Kontaktbok>& bok) {
if (bok.size() > 0) {
string firstname;
cout << "First name of the contact: ";
getline(cin, firstname);
int pos = findTargetFirstName(bok, firstname);
cout << "Firstname, Lastname, Address, Pnummer, E-post, Telefonnummer" << endl;
if (pos>=0) {
cout << left;
cout << setw(10) << bok[pos].fnamn << left
<< setw(15) << bok[pos].enamn << left
<< setw(20) << bok[pos].adress << left
<< setw(15) << bok[pos].pnummer <<left
<< setw(25) << bok[pos].epost <<left
<< setw(15) << bok[pos].telnummer << left << endl;
}
cout << "********************************************************************************" << endl;
}
}
One way is adding a parameter to determine which element to start from.
int findTargetFirstName(vector<Kontaktbok>& bok, string target, int start = 0) {
for (int i = start; i < bok.size(); i++)
if (bok[i].fnamn == target) return i;
return -1;
}
void search(vector<Kontaktbok>& bok) {
if(bok.size() > 0) {
string firstname;
cout << "First name of the contact: ";
getline(cin, firstname);
int pos = findTargetFirstName(bok, firstname);
cout << "Firstname, Lastname, Address, Pnummer, E-post, Telefonnummer" << endl;
while (pos>=0){ // change if to while
cout << left;
cout << setw(10) << bok[pos].fnamn << left
<< setw(15) << bok[pos].enamn << left
<< setw(20) << bok[pos].adress << left
<< setw(15) << bok[pos].pnummer <<left
<< setw(25) << bok[pos].epost <<left
<< setw(15) << bok[pos].telnummer << left << endl;
pos = findTargetFirstName(bok, firstname, pos + 1); // search for next contact
}
cout << "********************************************************************************" << endl;
}
}
Another way is having the function findTargetFirstName return a vector of contacts that meets the condition.
vector<int> findTargetFirstName(vector<Kontaktbok>& bok, string target) {
vector<int> ret;
for (int i = start; i < bok.size(); i++)
if (bok[i].fnamn == target) ret.push_back(i);
return ret;
}
void search(vector<Kontaktbok>& bok) {
if(bok.size() > 0) {
string firstname;
cout << "First name of the contact: ";
getline(cin, firstname);
vector<int> poses = findTargetFirstName(bok, firstname); // change type and variable
cout << "Firstname, Lastname, Address, Pnummer, E-post, Telefonnummer" << endl;
for (int pos : poses){ // change if to range-based for
cout << left;
cout << setw(10) << bok[pos].fnamn << left
<< setw(15) << bok[pos].enamn << left
<< setw(20) << bok[pos].adress << left
<< setw(15) << bok[pos].pnummer <<left
<< setw(25) << bok[pos].epost <<left
<< setw(15) << bok[pos].telnummer << left << endl;
}
cout << "********************************************************************************" << 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>
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.
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.