Мой односвязный список состоит из char *id; char firstname[15]; char lastname[15]; struct rec *next;
.
Итак, моя главная проблема, которая возникает в этом назначении, - удаление записи путем передачи идентификатора в функцию удаления. Он удалит идентификатор, но когда он попадет в строку, чтобы удалить имя или фамилию, он выдаст эту ошибку.
"ChristmasList.exe has triggered a breakpoint. occurred"
.
Моя вторая главная ошибка заключается в том, что мой strcmp
всегда сравнивает одну и ту же строку, поэтому она всегда возвращает 0, я также немного потрясен, как это сделать в любом случае, так что даже если он вернул -1 или 1. Я не не знаю, будет ли это правильно сортировать.
Спасибо всем, кто может предложить помощь, я застрял в этой проблеме в течение долгого времени и должен исправить ее, прежде чем мы получим следующую лабораторию, которую мы добавляем в этот код.
Main:
// ChristmasList.cpp : Adds, Deletes, and prints items in a structured list
// MyName
#include "stdafx.h"
#include "list.h"
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
rec myRec; // pointer to rec structure
int choice = 1; // initializing so choice is not 0 to start
int order; // ascending/descending input
char buf[500]; // buffer array
int result; //result of function calls
while (choice != 0)
{
cout << "Enter 1 to Add an Item.\n";
cout << "Enter 2 to delete a record.\n";
cout << "Enter 3 to Print a list of records.\n";
cout << "Enter 0 to exit the program\n";
cin >> choice; // User input for choice
switch (choice)
{
case 1: //Add
cout << "Enter your ID: "; //ask to enter ID
cin >> buf; //Input for ID into rec
myRec.id = buf;
cout << "Enter your first name: "; // Ask for first name
cin >> myRec.firstname; // Input first name into rec
cout << "Enter your last name: "; // Ask for last name
cin >> myRec.lastname; // Input last name into rec
if (AddItem(myRec)) // Call Add
{
cout << "Success\n";
}
else
{
cout << "\nFailed to add :(\n";
}
break;
case 2: //Delete
cout << "Enter the ID of the record you want to delete: "; // ask to enter ID that wants to be deleted
cin >> buf; // Input id wanting to be deleted
result = DeleteItem(buf); // Call delete
break;
case 3: //Print
cout << "Enter 0 for ascending order and 1 for descending order: "; // ask for ascending or descending
cin >> order; // User input for ascending or descending
PrintList(order); // Call Print
break;
case 0:
break;
}
}
return 0;
}
list.cpp:
#include "stdafx.h"
#include "list.h"
#include<iostream>
#include<cstring>
using namespace std;
rec *first = NULL;
rec *last = NULL;
int counter = 0;
rec *MyNewRec2[500];
int AddItem(rec r)
{
int comp;
int comp2;
rec *MyNewRec;
MyNewRec = new rec;
MyNewRec->id = new char[10];
strcpy_s(MyNewRec->id, strlen(r.id) + 1, r.id); // Copying ID
strcpy_s(MyNewRec->firstname, strlen(r.firstname) + 1, r.firstname);// Copying first name
strcpy_s(MyNewRec->lastname, strlen(r.lastname) + 1, r.lastname);// Copying last name
while (first != NULL)
{
if (*MyNewRec->id == *first->id)
{
cout << "Duplicate ID: " << MyNewRec->id << endl;
return 0;
}
else
{
MyNewRec2[counter] = MyNewRec;
last = MyNewRec2[counter];
first = first->next;
counter++;
return 1;
}
}
if (first == NULL) // If no items are in the list
{
MyNewRec->next = NULL;
first = MyNewRec;
last = MyNewRec;
MyNewRec2[counter] = MyNewRec;
counter++;
}
if (r.lastname >= "97")
{
char *temp;
temp = r.lastname - 32;
}
comp = strcmp(MyNewRec->lastname, (first->lastname - 1));
comp2 = strcmp(MyNewRec->lastname, (last->lastname - 1));
if (comp == -1 && comp <= 0) //Move first back in array
{
MyNewRec2[counter - 1] = NULL;
MyNewRec2[counter - 1] = MyNewRec;
MyNewRec->next = first;
first = MyNewRec;
return 1;
}
else if (comp2 == 1 && comp2 >= 0) //Move last back in array
{
MyNewRec->next = NULL;
last->next = last;
MyNewRec2[counter -1] = last;
last = MyNewRec;
return 1;
}
else
{
while (first->next != NULL)
{
comp = strcmp(MyNewRec->lastname, first->lastname);
if (comp == 1)
{
first = first->next;
return 1;
}
else if (comp == 1 || comp == 0)
{
MyNewRec2[counter - 1] = first;
MyNewRec->next = first->next;
first->next = MyNewRec;
return 1;
}
}
}
return 1;
}
int DeleteItem(char *delid)
{
rec *MyNewRec, *temp;
if (first == NULL)
{
return 0;
}
while (first != NULL)
{
if (*first->id == *delid)
{
MyNewRec = first->next;
//temp = MyNewRec2[counter - 1]; // Random try
//temp->next = MyNewRec; // Random try
delete[] first->id;
delete[] first->firstname;
delete[] first->lastname;
delete first;
counter--;
return 1;
}
else
{
first = first->next;
}
}
return 0;
}
void PrintList(int order)
{
rec *temp = new rec;
if (order == 0)
{
int i = 0;
temp = MyNewRec2[i];
while (temp != NULL)
{
cout.flush() << temp->id << " ";
cout.flush() << temp->firstname << " ";
cout.flush() << temp->lastname << " " << endl;
i++;
temp = MyNewRec2[i];
}
}
temp = MyNewRec2[counter - 1];
if (order == 1)
{
while (temp != NULL)
{
cout.flush() << temp->id << " ";
cout.flush() << temp->firstname << " ";
cout.flush() << temp->lastname << " " << endl;
counter--;
temp = MyNewRec2[counter - 1];
}
}
}
list.h:
struct rec
{
char *id;
char firstname[15];
char lastname[15];
struct rec *next;
};
int AddItem(rec r);
int DeleteItem(char *delid);
void PrintList(int order);