I'm trying to get this piece of code I converted from C to work in C++ but I'm getting an access violation error. Problem occurs at line 61. Someone can help me with this?
The function display(llist mylist) displays a list of choices for a record book:
The part where the error comes up is during case 1 within the switch statement (At line 61). When I call printAll() an access violation error is thrown. The function prints all the records just fine but when it reaches the end, the error is thrown.
The function display(llist mylist) displays a list of choices for a record book:
Code:
void display(llist mylist)
{
char name[25], address[80], telno[15], input[100];
int yearofbirth, choice, records;
yearofbirth = 0;
choice = -1;
while(choice != 6)
{
cout << "*****************************************************\n";
cout << " Choice 1: Add a new record\n";
cout << " Choice 2: Modify a record\n";
cout << " Choice 3: Delete an existing record\n";
cout << " Choice 4: Display a record\n";
cout << " Choice 5: Display all records\n";
cout << " Choice 6: Exit Program\n";
cout << "*****************************************************\n";
cout << "Please enter one of the above choices from 1 - 6: ";
cin >> input;
choice = atoi(input);
cin.ignore(100, '\n');
switch (choice)
{
case 1:
//get user input for name of record
cout << "\nEnter a name: ";
while(!getfield(name, '\n', 25))
{
cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
cout << "Pleas re-enter: ";
};
//get user input for address of record
cout << "\nEnter an address ending it with '@': ";
while(!getfield(address, '@', 80))
{
cout << "ERROR! Length of name entered exceeds character limit! (80 character)\n";
cout << "Pleas re-enter: ";
}
//get user input for yearofbirth of record
cout << "\nEnter the year of birth: ";
cin >> input;
cin.ignore(100, '\n');
while(!(yearofbirth = atoi(input)) || (numberofdigits(yearofbirth) != 4))
{
cout << "Invalid year entered, please re-enter: ";
cin >> input;
cin.ignore(100, '\n');
}
//get user input for telno of record
cout << "\nPlease enter a telephone # with no special characters: ";
while(!getfield(telno, '\n', 15) || !checkTelno(telno))
cout << "Invalid phone number, please re-enter: ";
//adding the new record into the list and displaying the list
records = mylist.addRecord(name, address, yearofbirth, telno);
cout << "Record added sucessfully!\n";
mylist.printAll();
break;
case 2:
//get user input for name of record
cout << "\nEnter a name: ";
while(getfield(name, '\n', 25) == 0)
{
cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
cout << "Pleas re-enter: ";
};
//get user input for address of record
cout << "\nEnter the new address ending it with '@': ";
while(getfield(address, '\n', 80) == 0)
{
cout << "ERROR! Length of name entered exceeds character limit! (80 character)\n";
cout << "Pleas re-enter: ";
}
//get user input for telno of record
cout << "\nPlease enter the new telephone # with no special characters: ";
while(getfield(telno, '\n', 15) == 0 || !checkTelno(telno))
cout << "Invalid phone number, please re-enter: ";
//modify records (if exists) and display feed back to user
records = mylist.modifyRecord(name, address, telno);
if(records == -1)
cout << "The list is empty, there were no records to modify\n";
else
{
mylist.printAll();
cout << "Records found and modified: " << records << "\n";
}
break;
case 3:
//get user input for name of record
cout << "\nEnter the name of record you would like to delete: ";
while(getfield(name, '\n', 25) == 0)
{
cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
cout << "Pleas re-enter: ";
};
//delete record requested and display feedback to user
records = mylist.deleteRecord(name);
if(records == -1)
cout << "The list is empty, there were no records to delete\n";
else
{
mylist.printAll();
cout << "Records found and deleted: " << records << "\n";
}
break;
case 4:
//get user input for name of record
cout << "\nEnter the name of record you would like to display: ";
while(getfield(name, '\n', 25) == 0)
{
cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
cout << "Pleas re-enter: ";
};
records = mylist.printRecord(name);
if(records == -1)
cout << "ERROR!!! The list is empty\n";
else
cout << "Records found: " << records << "\n";
break;
case 5:
mylist.printAll();
break;
case 6:
mylist.~llist();
cout << "********************Exiting Program********************\n";
break;
default:
cout << "\n ERROR! You have entered an incorrect choice \n";
}
}
}
Code:
void llist::printAll()
{
record *begin = start;
int count = 0;
while(begin != NULL)
{
cout << "Name: " << begin -> name << "\n";
cout << "Address: " << begin -> address << "\n";
cout << "Year of Birth: " << begin -> yearofbirth << "\n";
cout << "Telephone Number: " << begin -> telno << "\n\n";
begin = begin -> next;
}
}
Comment