对于这个程序,我使用单个链表让人们输入他们的动物信息。我在浏览链表时遇到麻烦,我可以查找任何动物名称并将其所有者的姓氏打印出来。当我输入信息并尝试搜索动物名称时,它会正确打印动物名称,但它会打印出我输入的最后一个所有者的姓氏,而不是与该特定动物相对应的所有者的姓氏。
我如何修复我的搜索功能,这是我到目前为止所拥有的......
我的头文件(Vet.h)
#include <iostream>
using namespace std;
int const MAX_NUMBER_OF_PATIENTS = 100;
int const TABLE_SIZE = 31;
enum Pet_Type
{
Dog,
Cat,
Bird,
Reptile,
Other
};
class LIST
{
private:
struct Node
{
char owners_last_name[30];
char pet_name[20];
char animals_color[12];
char d_o_b[11];
Pet_Type pet_type;
Node *next;
}; Node *head;
public:
LIST()
{
head = NULL;
}
Pet_Type getPetType();
void appendNode();
void Menu();
void Read(Node *);
void Search();
void Display();
void Clear();
void Quit();
};
我的cpp文件(Veterinary.cpp)
#include "Vet.h"
#include <iostream>
int main()
{
LIST P;
char choice = ' ';
int terminate = 0;
int counter = 0;
string name = "NULL";
while( counter != 100 )
{
P.Menu();
cin >> choice;
switch ( choice )
{
case 'E' : case 'e':
if( counter <= MAX_NUMBER_OF_PATIENTS )
{
P.appendNode();
counter++;
break;
}
else
{
cout << "Your database is full\n";
break;
}
case 'S' : case 's':
P.Search();
break;
case 'D' : case 'd':
P.Display();
break;
case 'C' : case 'c':
P.Clear();
break;
case 'Q' : case 'q':
cout << endl;
cout << "Number of entries in your database: " << counter << endl << endl;
P.Quit();
return 0;
break;
}
}
return 0;
}
void LIST::appendNode()
{
Node *newNode, *nodeptr;
newNode = new Node;
Read( newNode );
newNode->next = NULL;
if( !head )
{
head = newNode;
}
else
{
nodeptr = head;
while( nodeptr->next )
{
nodeptr = nodeptr->next;
}
nodeptr->next = newNode;
}
}
void LIST::Read( LIST::Node *newNode )
{
cout << "\nPlease enter your pets information.\n";
cout << "Your pets name: ";
cin >> newNode->pet_name;
cout << "Your last name: ";
cin >> newNode->owners_last_name;
newNode->pet_type = LIST::getPetType();
cout << "Your animals date of birth (MM/DD/YYYY): ";
cin >> newNode->d_o_b;
cout << "Your animals color of animal: ";
cin >> newNode->animals_color;
cout << endl;
}
void LIST::Display()
{
Node *nodeptr;
nodeptr = head;
while( nodeptr )
{
cout << endl;
cout << "Pets name: " << nodeptr->pet_name;
cout << endl;
cout << "Owners last name: " << nodeptr->owners_last_name << endl;
switch ( nodeptr->pet_type )
{
case 0:
cout << "Animal type: Dog\n";
break;
case 1:
cout << "Animal type: Cat\n";
break;
case 2:
cout << "Animal type: Bird\n";
break;
case 3:
cout << "Animal type: Reptile\n";
break;
default:
cout << "Animal type: Other\n";
break;
}
cout << "Pets date of birth: " << nodeptr->d_o_b;
cout << endl;
cout << "Pets color: " << nodeptr->animals_color;
cout << endl << endl;
nodeptr = nodeptr->next;
}
}
void LIST::Menu()
{
cout << "(E)nter data to database\n";
cout << "(S)earch the database\n";
cout << "(D)isplay data in database\n";
cout << "(C)lear data in database\n";
cout << "(Q)uit the program\n\n";
cout << "What would you like to do? \n";
cout << "Enter a letter: ";
}
Pet_Type LIST::getPetType()
{
int animal_choice;
cout << "What type of animal do you have?\n";
cout << "1) Dog\n";
cout << "2) Cat\n";
cout << "3) Bird\n";
cout << "4) Reptile\n";
cout << "5) Other\n";
cout << "Select a number: ";
cin >> animal_choice;
switch ( animal_choice )
{
case 1:
return Dog;
case 2:
return Cat;
case 3:
return Bird;
case 4:
return Reptile;
default:
return Other;
}
}
void LIST::Clear()
{
cout << "\nDatabase cleared! \n\n";
head = NULL;
}
void LIST::Search()
{
Node *nodeptr;
nodeptr = head;
cout << "Enter the name of the pet you want to search: ";
cin >> nodeptr->pet_name;
cout << endl;
cout << "Pets name: " << nodeptr->pet_name;
cout << endl;
cout << "Owners last name: " << nodeptr->owners_last_name << endl;
}
void LIST::Quit()
{
system( "pause" );
}