467,859 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,859 developers. It's quick & easy.

Access Violation Error

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:

Expand|Select|Wrap|Line Numbers
  1. void display(llist mylist)
  2. {
  3.     char name[25], address[80], telno[15], input[100];
  4.     int yearofbirth, choice, records;
  5.     yearofbirth = 0;
  6.     choice = -1;
  7.  
  8.     while(choice != 6)
  9.     {
  10.         cout << "*****************************************************\n";
  11.         cout << "         Choice 1:  Add a new record\n";
  12.         cout << "         Choice 2:  Modify a record\n";
  13.         cout << "         Choice 3:  Delete an existing record\n";
  14.         cout << "         Choice 4:  Display a record\n";
  15.         cout << "         Choice 5:  Display all records\n";
  16.         cout << "         Choice 6:  Exit Program\n";
  17.         cout << "*****************************************************\n";          
  18.         cout << "Please enter one of the above choices from 1 - 6:  ";
  19.         cin >> input;
  20.         choice = atoi(input);
  21.         cin.ignore(100, '\n');
  22.  
  23.         switch (choice)
  24.         {
  25.             case 1:
  26.                 //get user input for name of record
  27.                 cout << "\nEnter a name:  ";
  28.                 while(!getfield(name, '\n', 25))
  29.                 {
  30.                     cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
  31.                     cout << "Pleas re-enter:  ";
  32.                 };
  33.  
  34.                 //get user input for address of record
  35.                 cout << "\nEnter an address ending it with '@':  ";
  36.                 while(!getfield(address, '@', 80))
  37.                 {
  38.                     cout << "ERROR! Length of name entered exceeds character limit! (80 character)\n";
  39.                     cout << "Pleas re-enter:  ";
  40.                 }
  41.  
  42.                 //get user input for yearofbirth of record
  43.                 cout << "\nEnter the year of birth:  ";
  44.                 cin >> input;
  45.                 cin.ignore(100, '\n');
  46.                 while(!(yearofbirth = atoi(input)) || (numberofdigits(yearofbirth) != 4))
  47.                 {
  48.                     cout << "Invalid year entered, please re-enter:  ";
  49.                     cin >> input;
  50.                     cin.ignore(100, '\n');
  51.                 }
  52.  
  53.                 //get user input for telno of record
  54.                 cout << "\nPlease enter a telephone # with no special characters:  ";
  55.                 while(!getfield(telno, '\n', 15) || !checkTelno(telno))
  56.                     cout << "Invalid phone number, please re-enter:  ";
  57.  
  58.                 //adding the new record into the list and displaying the list
  59.                 records = mylist.addRecord(name, address, yearofbirth, telno);
  60.                 cout << "Record added sucessfully!\n";
  61.                 mylist.printAll();
  62.                 break;
  63.             case 2: 
  64.                 //get user input for name of record
  65.                 cout << "\nEnter a name:  ";
  66.                 while(getfield(name, '\n', 25) == 0)
  67.                 {
  68.                     cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
  69.                     cout << "Pleas re-enter:  ";
  70.                 };
  71.  
  72.                 //get user input for address of record
  73.                 cout << "\nEnter the new address ending it with '@':  ";
  74.                 while(getfield(address, '\n', 80) == 0)
  75.                 {
  76.                     cout << "ERROR! Length of name entered exceeds character limit! (80 character)\n";
  77.                     cout << "Pleas re-enter:  ";
  78.                 }  
  79.  
  80.                 //get user input for telno of record
  81.                 cout << "\nPlease enter the new telephone # with no special characters:  ";
  82.                 while(getfield(telno, '\n', 15) == 0 || !checkTelno(telno))
  83.                     cout << "Invalid phone number, please re-enter:  ";
  84.  
  85.                 //modify records (if exists) and display feed back to user
  86.                 records = mylist.modifyRecord(name, address, telno);
  87.                 if(records == -1)
  88.                     cout << "The list is empty, there were no records to modify\n";
  89.                 else
  90.                 {
  91.                     mylist.printAll();
  92.                     cout << "Records found and modified:  " << records << "\n";
  93.                 }
  94.                 break;
  95.             case 3:
  96.                 //get user input for name of record
  97.                 cout << "\nEnter the name of record you would like to delete:  ";
  98.                 while(getfield(name, '\n', 25) == 0)
  99.                 {
  100.                     cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
  101.                     cout << "Pleas re-enter:  ";
  102.                 };
  103.  
  104.                 //delete record requested and display feedback to user
  105.                 records = mylist.deleteRecord(name);
  106.                 if(records == -1)
  107.                     cout << "The list is empty, there were no records to delete\n";
  108.                 else
  109.                 {
  110.                     mylist.printAll();
  111.                     cout << "Records found and deleted:  " << records << "\n";
  112.                 }
  113.                 break;
  114.             case 4:
  115.                 //get user input for name of record
  116.                 cout << "\nEnter the name of record you would like to display:  ";
  117.                 while(getfield(name, '\n', 25) == 0)
  118.                 {
  119.                     cout << "ERROR! Length of name entered exceeds character limit! (25 character)\n";
  120.                     cout << "Pleas re-enter:  ";
  121.                 };
  122.  
  123.                 records = mylist.printRecord(name);
  124.                 if(records == -1)
  125.                     cout << "ERROR!!! The list is empty\n";
  126.                 else
  127.                     cout << "Records found:  " << records << "\n";
  128.  
  129.                 break;
  130.             case 5:   
  131.                 mylist.printAll();
  132.                 break;
  133.             case 6:
  134.                 mylist.~llist();
  135.  
  136.                 cout << "********************Exiting Program********************\n";
  137.                 break;
  138.             default:
  139.                 cout << "\n   ERROR! You have entered an incorrect choice     \n";
  140.         }
  141.     }
  142. }
  143.  
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.
Expand|Select|Wrap|Line Numbers
  1. void llist::printAll()
  2. {
  3.     record *begin = start;
  4.     int count = 0;
  5.  
  6.     while(begin != NULL)
  7.     {
  8.  
  9.         cout << "Name:  " << begin -> name << "\n";
  10.         cout << "Address:  " << begin -> address << "\n";
  11.         cout << "Year of Birth:  " << begin -> yearofbirth << "\n";
  12.         cout << "Telephone Number:  " << begin -> telno << "\n\n";
  13.         begin = begin -> next;
  14.     }
  15. }
Dec 6 '07 #1
  • viewed: 2018
Share:
2 Replies
weaknessforcats
Expert Mod 8TB
WHy is there all this code in a display function?? It looks like your main() hase wandered in here.

I see the display funciton argument is a llist and not an llist& or llist*. That menas a copy was made when the display was called. And that menas you may have copy constructor problems. (Actually, it means you shoud have used a pointer or referecne argument).

Next, the printall() uses a magic variable named start that drops in from nowhere. I suspect a global variable. Probably the value in the global has gotten screwed up. You see, there has to be an llist that was used to make the copy for the display function argunment. That means there are at least two llist variables using the same start. Bad news. That is a race condition.

Read the C/C++ Article http://www.thescripts.com/forum/thread736028.html.

Try to get your functions to do only one thing. I would start by renaming your display function to main() and go from there.
Dec 6 '07 #2
Thanks. The variable start is a private member of class llist that is defined in a header file. Unfortunately I cannot change the definition of the class. As for the copy constructor, you are right, I was actually making a copy. However, even as I consolidate the display() function with int main(), the problem still persist. I was able to figure out the problem was actually coming from my addRecord function.

Regards,
Jthep
Dec 6 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by Steven Reddie | last post: by
reply views Thread by Steven Reddie | last post: by
7 posts views Thread by Daniel | last post: by
reply views Thread by Microsoft News | last post: by
reply views Thread by Bruce Pataki | last post: by
1 post views Thread by =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post: by
reply views Thread by Paavo Helde | last post: by
2 posts views Thread by =?Utf-8?B?c29jYXRvYQ==?= | last post: by
39 posts views Thread by Martin | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.