Reputation: 11
It is necessary that the record I need is deleted and edited (second, fifth, etc.). But it happens to only with the last record added. How to fix it?
#include <iostream>
#include <string>
using namespace std;
struct DATE
{
int number;
int month;
int year;
};
struct informations
{
char code[10];
char number_car[10];
char mark[10];
DATE release;
DATE registration;
informations* next;
} typedef Informations;
Informations* top = nullptr;
void push()
{
Informations* ptr = new Informations;
cin.get();
cout << "Enter the code owner: ";
cin.getline(ptr->code, 10);
cout << "Enter the number of the car: ";
cin.getline(ptr->number_car, 10);
cout << "Enter the mark of the car: ";
cin.getline(ptr->mark, 10);
cout << "Enter the date of release: " << endl;
do {
cout << "number: ";
cin >> ptr->release.number;
} while ((ptr->release.number > 31) || (ptr->release.number <= 0));
do {
cout << "month: ";
cin >> ptr->release.month;
} while ((ptr->release.month > 12) || (ptr->release.month <= 0));
do {
cout << "year: ";
cin >> ptr->release.year;
} while ((ptr->release.year > 2019) || (ptr->release.year <= 1980));
cout << "Enter the date of registration: " << endl;
do {
cout << "number: ";
cin >> ptr->registration.number;
} while ((ptr->registration.number > 31) || (ptr->registration.number <= 0));
do {
cout << "month: ";
cin >> ptr->registration.month;
} while ((ptr->registration.month > 12) || (ptr->registration.month <= 0));
do {
cout << "year: ";
cin >> ptr->registration.year;
} while ((ptr->registration.year > 2019) || (ptr->registration.year <= 1980));
cout << endl;
ptr->next = top;
top = ptr;
}
void change()
{
cin.get();
cout << "Enter the code owner: ";
cin.getline(top->code, 10);
cout << "Enter the number of the car: ";
cin.getline(top->number_car, 10);
cout << "Enter the mark of the car: ";
cin.getline(top->mark, 10);
cout << "Enter the date of release:" << endl;
do {
cout << "number: ";
cin >> top->release.number;
} while ((top->release.number > 31) || (top->release.number <= 0));
do {
cout << "month: ";
cin >> top->release.month;
} while ((top->release.month > 12) || (top->release.month <= 0));
do {
cout << "year: ";
cin >> top->release.year;
} while ((top->release.year > 2019) || (top->release.year <= 1980));
cout << "Enter the date of registration: " << endl;
do {
cout << "number: ";
cin >> top->registration.number;
} while ((top->registration.number > 31) || (top->registration.number <= 0));
do {
cout << "month: ";
cin >> top->registration.month;
} while ((top->registration.month > 12) || (top->registration.month <= 0));
do {
cout << "year: ";
cin >> top->registration.year;
} while ((top->registration.year > 2019) || (top->registration.year <= 1980));
cout << endl;
}
void pop()
{
auto bad = top;
top = top->next;
delete bad;
}
void show_stack()
{
int i = 0;
cout << endl;
Informations* ptr = top;
while (ptr != NULL)
{
i++;
cout << " " << i << endl;
cout << "The code owner: " << ptr->code << endl;
cout << "The number of the car: " << ptr->number_car << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << "The date of release:\nnumber:\t\t\t\t" << ptr->release.number << endl;
cout << "month:\t\t\t\t" << ptr->release.month << endl;
cout << "year:\t\t\t\t" << ptr->release.year << endl;
cout << endl;
cout << "The date of registration:\nnumber:\t\t\t\t" << ptr->registration.number << endl;
cout << "month:\t\t\t\t" << ptr->registration.month << endl;
cout << "year:\t\t\t\t" << ptr->registration.year << endl;
cout << endl;
ptr = ptr->next;
}
}
void November_and_December()
{
Informations* ptr = top;
while (ptr != NULL)
{
if ((ptr->registration.month == 11) || (ptr->registration.month == 12))
{
if (ptr->registration.year == 2018)
{
cout << "The number of the car: " << ptr->number_car << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next;
}
}
else cout << endl;
}
}
void Average_age()
{
Informations* ptr = top;
int i = 0;
int averageyear = 0;
while (ptr != NULL)
{
i++;
averageyear += ptr->release.year;
ptr = ptr->next;
}
double average = averageyear / i;
cout << average << endl;
}
void February_and_March()
{
int vozrast;
Informations* ptr = top;
while (ptr != NULL)
{
if (((ptr->registration.month == 2) || (ptr->registration.month == 3)) && (ptr->registration.year == 2019))
{
vozrast = (ptr->registration.year) - (ptr->release.year);
if ((ptr->registration.month) < (ptr->release.month))
{
vozrast = vozrast - 1;
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next;
}
else if ((ptr->registration.month) == (ptr->release.month))
{
if ((ptr->registration.number) < (ptr->release.number))
{
vozrast = vozrast - 1;
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next;
}
else
{
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next;
}
}
}
else {
cout << "Age of car: " << vozrast << endl;
cout << "The mark of the car: " << ptr->mark << endl;
cout << endl;
ptr = ptr->next;
}
}
}
int main()
{
int l;
do
{
cout << "--------------------------------------";
cout << endl;
cout << " 1)Enter " << endl << " 2)Change " << endl << " 3)Delete " << endl << " 4)Show " << endl << " 5)November and December " << endl << " 6)Average age" << endl << " 7)February and March" << endl << " 8)Exit " << endl;
cin >> l;
switch (l)
{
case 1:push(); break;
case 2: {
if (top != NULL) change(); break;
}
case 3:
{
if (top != NULL) pop(); break;
}
case 4: show_stack(); break;
case 5:
{
if (top != NULL) November_and_December(); break;
}
case 6: if (top != NULL) Average_age(); break;
case 7:
{
if (top != NULL) February_and_March(); break;
}
default:break;
}
} while (l != 8);
}
Upvotes: 1
Views: 119
Reputation: 84551
Since your collection of data is actually a linked-list which is being treated as a stack by only operating on the top
(head) node, you can simply provide the code
and number
to identify a unique car in the list and delete that node from your list. Since both code
and number
members are held as type char *
, you can use strcmp
to compare the values. To locate and delete an individual node from the list you can use:
void del_car (const char *code, const char *number)
{
Informations **ppn = &top, /* pointer to pointer to top node */
*pn = top; /* pointer to top node */
while (pn) { /* iterate over each car */
/* compare with code and number to delete */
if (strcmp(pn->code, code) == 0 && strcmp(pn->number, number) == 0) {
*ppn = pn->next; /* set node at current address to next */
delete pn; /* delete current node */
return;
}
ppn = &pn->next; /* advances address of current to next */
pn = pn->next; /* advance current to next */
}
fputs ("car not found.\n", stderr); /* warn if no match found */
}
However, the use of new/delete
and basic types of char *
instead of using the current STL std::vector
and std::string
, is about a decade or more out of date. (there is nothing wrong with knowing how to use new/delete
and fundamental types -- there is a lot of legacy code out there, but for new code, you are much better off letting the containers manage the memory for you)
Give it a try and let me know if you have further questions. See Linus on Understand Pointers for understanding why it is advantageous to iterate using both a pointer to top
(a pointer-to-pointer) and the pointer top
to delete the node in the list.
Upvotes: 3