I'm still new to programming. I have made a book record system and I just wanted some coding review if there is any mistakes I should fix or any improvements that are needed. Here is the code I made
#include <iostream>
#define MAX 5
using namespace std;
struct record
{
int id;//stores id
float price;//store price
int qty;//stores quantity
record* next;//reference to the next node
};
record* head;//create empty record
record* tail;//the end of the record
void push(record *& head, record *&tail, int id, float price, int qty)
{
if (head == NULL)
{
record* r = new record;
r->id = id;
r->price = price;
r->qty = qty;
r->next = NULL;//end of the list
head = r;
tail = r;
}
else if (head != NULL && (MAX - 1))
{
record* r = new record;
r->id = id;
r->price = price;
r->qty = qty;
r->next = head;
head = r;
}
}
int pop(record *&head, record *& tail)
{
if (head == NULL)
{
cout << "No record in memory" << endl;
}
else if (head == tail)
{
cout << "The record "<<"ID: " << head->id << "\nPrice: " << head->price << "\nQuantity: " << head->qty << "\n" << "was deleted" << endl; //CORRECTION HERE
}
else
{
record* delptr = new record;
delptr = head;
head = head->next;
cout << "The record " << delptr->id << ", " << delptr->price << ", " << delptr->qty << " was deleted" << endl; //CORRECTION HERE
delete delptr;
}
return 0;
}
void display(record *&head)
{
record* temp = new record; //CORRECTION HERE
temp = head;
if (temp == NULL)
{
cout << "No record in memory" << endl;
}
else
{
cout << "Record : " << endl;
while (temp != NULL)
{
cout <<"\nID: "<< temp->id << "\nPrice: " << temp->price << "\nQuantity: " << temp->qty <<"\n"<< endl; //CORRECTION HERE
temp = temp->next;
}
}
}
int LinearSearch(record *&head) {
int key;
record *aux = head;
cout << "Enter ID to search";
cin >> key;
while (aux != NULL) {
if (aux->id == key)
{
cout << "I found it\n";
cout << "\nID: " << aux->id << "\nPrice: " << aux->price << "\nQuantity: " << aux->qty << "\n" << endl;
}
else{
cout << "ID not found";
}
aux = aux->next;
}
return NULL;
}
int update(record *& head, record *&tail, int id, float price, int qty) {
int key;
record *aux = head;
cout << "Enter ID to search";
cin >> key;
while (aux != NULL) {
if (aux->id == key)
{
cout << "I found it\n";
cout << "\nID: " << aux->id << "\nPrice: " << aux->price << "\nQuantity: " << aux->qty << "\n" << endl;
int id, qty;
float price;
cout << "Update your data please";
cout << "Enter ID:";
cin >> aux->id;
cout << "\nEnter Price: ";
cin >> aux->price;
cout << "\nEnter Quantity: ";
cin >> aux->qty;
}
else {
cout << "ID not found";
}
aux = aux->next;
}
return NULL;
}
char menu()
{
char choice;
cout << "\t::MENU::\n" << endl;
cout << "1. Add new record\n" << endl;
cout << "2. Delete record\n" << endl;
cout << "3. Show record\n" << endl;
cout << "4. Search record\n" << endl;
cout << "5. Update record\n" << endl;
cout << "6. Quit\n" << endl;
cout << "-----------------------\n" << endl;
cout << "\nEnter selection : " << endl;
cin >> choice;
return choice;
}
int main()
{
record* head;
record* tail;
head = NULL;
tail = NULL;
char choice;
do
{
cout << "---------------------- - \n" << endl;
choice = menu();
switch (choice) { //CORRECTION HERE
case '1':
int id, qty;
float price;
cout << "Enter ID:";
cin >> id; // Please correct yourself here, what is r here, r is not declared anywhere
cout << "\nEnter Price: ";
cin >> price;
cout << "\nEnter Quantity: ";
cin >> qty;
push(head, tail, id, price, qty);
break;
case '2':
pop(head, tail);
break;
case'3':
display(head);
break;
case'4':
LinearSearch(head);
case '5':
update(head, tail, id, price, qty);
default:
cout << "Quiting...\n";
return 0;
}
} while (choice != '6');
return 0;
}