I am to create user menu for users to choose Stack and Queue. When I compile this code there's no error but when I run the program, it stops and i cant cin the data
#include <iostream>
#include <string>
usingnamespace std;
struct book {
string name;
int year;
book *next;
};
book *frontptr;
book *rearptr;
book *topptr;
void createEmptyQueue();
void createEmptyStack();
void pop();
void push();
void displayStack();
void displayQueue();
void searchStack();
void searchQueue();
void removeAll();
void queueMenu();
void stackMenu();
void enqueue();
void dequeue();
void removeAllNodes();
void createEmptyStack(){
topptr = NULL;
}
int main() {
char choice;
cout<<"Choose A=Queue and B=Stack"<<endl;
cin >> choice;
if (choice=='A')
queueMenu();
elseif (choice=='B')
stackMenu();
else
cout<<"Please choose between A and B";
return 0;
}
void queueMenu(){
cout<<"\t\t\tQueue Menu\t\t\t"<<endl;
enqueue();
enqueue();
enqueue();
cout<<"The list of book is :"<<endl;
displayQueue();
dequeue();
cout<<"After deleted the first data the list of book is:"<<endl;
displayQueue();
searchQueue();
removeAllNodes();
}
void stackMenu(){
cout<<"\t\t\tStack Menu\t\t\t"<<endl;
push();
push();
push();
cout<<"The list of book is :"<<endl;
displayStack();
pop();
searchStack();
cout<<"After deleted the first data the list of book is:"<<endl;
displayStack();
removeAll();
}
void push(){
book *newptr;
string title;
newptr=new book;
cout<<"Please enter the book name"<<endl;
cin.ignore();
getline(cin,title);
newptr->name=title;
cout<<"Please enter the year"<<endl;
cin>>newptr->year;
newptr->next=NULL;
if(topptr==NULL)
topptr=newptr;
else{
newptr->next=topptr;
topptr=newptr;
}
}
void pop(){
book *newptr;
if(topptr==NULL)
cout<<"The list is empty";
else{
newptr=topptr;
topptr=topptr->next;
delete newptr;
}
}
void displayStack(){
book *curptr;
curptr=topptr;
int num=1;
cout<<"\t\t\tNo."<<"\t\t\tTitle"<<"\t\t\tYear"<<endl;
while(curptr!=NULL){
cout<<"\n\t\t\t"<<num<<"\t\t\t"<<curptr->name<<"\t\t\t"<<curptr->year<<endl;
curptr=curptr->next;
num++;
}
}
void displayQueue(){
book *curptr;
curptr=frontptr;
int num=1;
cout<<"\t\t\tNo."<<"\t\t\tTitle"<<"\t\t\tYear"<<endl;
while(curptr!=NULL){
cout<<"\n\t\t\t"<<num<<"\t\t\t"<<curptr->name<<"\t\t\t"<<curptr->year<<endl;
curptr=curptr->next;
num++;
}
}
void searchQueue(){
book *curptr;
curptr=frontptr;
string data;
cout<<"Which book do you want to search for?";
cin.ignore();
getline(cin,data);
while(curptr!=NULL && curptr->name != data){
curptr=curptr->next;
}
if(curptr->next == NULL)
cout<<"The book is not found"<<endl;
else
cout<<"The book is in the list"<<endl;
}
void searchStack(){
book *curptr;
curptr=topptr;
string data;
cout<<"Which book do you want to search for?";
cin.ignore();
getline(cin,data);
while(curptr!=NULL && curptr->name != data){
curptr=curptr->next;
}
if(curptr->next == NULL)
cout<<"The book is not found"<<endl;
else
cout<<"The book is in the list"<<endl;
}
void removeAll(){
book *curptr=topptr;
while(curptr!=NULL){
pop();
}
}
void removeAllNodes(){
book *curptr=frontptr;
while(curptr!=NULL){
dequeue();
}
}
void createEmptyQueue(){
frontptr=NULL;
rearptr=NULL;
}
void enqueue(){
book *newptr;
string title;
int produced;
newptr = new book;
cout<<"Enter the book title:"<<endl;
cin.ignore();
getline(cin,title);
cout<<"Enter the year of the book published"<<endl;
cin>>produced;
newptr->name = title;
newptr->year = produced;
if(rearptr==NULL && frontptr==NULL){
frontptr=newptr;
rearptr=newptr;
}
else{
rearptr->next=newptr;
rearptr=newptr;
}
}
void dequeue(){
book *newptr;
if(frontptr==NULL)
cout<<"Sorry,the list is empty"<<endl;
else{
newptr=frontptr;
frontptr=frontptr->next;
delete newptr;
}
}
Please use code tags. Edit your post, highlight the code and click the "<>" button to the right of the edit window.
When I run the program I get:
Choose A=Queue and B=Stack
A
Queue Menu
Enter the book title:
Silent
Enter the year of the book published
1983
Enter the book title:
Bobby
Enter the year of the book published
1969
Enter the book title:
The world and You
Enter the year of the book published
2533
The list of book is :
No. Title Year
1 Silent 1983
2 Bobby 1969
3 The world and You 2533
Segmentation fault (core dumped)
removeAll() contains an infinite loop because curptr never changes inside the loop.
removeAllNodes() has the same problem.
enqueue() leaves newptr->next uninitialized when inserting into an empty list.
searchQueue() will mistakenly say it hasn't found the book when the book found is the last one in the list.
searchStack() has the same bug as searchQueue
Fix these bugs and then consider restructuring the code to make Stack and Queue classes. Also notice the duplicate code to prompt for a book in enqueue and push(). It would be a lot cleaner to break enqueue() into 2 functions: one that prompts for a book and another that inserts a book into the queue, then you'd call them like this: