473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Access Violation Error

34
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
2 2289
weaknessforcats
9,208 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
jthep
34
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

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

Similar topics

15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
7
by: Daniel | last post by:
I want to write a method to remove the last node of the linked list. But the error "Access Violation" exists and the error point to this method. What does it means by Access Violation and how can...
0
by: Microsoft News | last post by:
I'm getting the following error when I shut down my C# .NET v1.1 application: 0xC0000005: Access violation reading location 0x73bc0000 This error didn't occur until I added a...
0
by: Bruce Pataki | last post by:
I am creating an MFC application with activeX document server support. The application runs perfectly fine when i run as a standalone application. But when i run the application in Internet...
1
by: =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post by:
In my project i have a component named PTS Engine which is developed in VC++ ..Net 2003. And it is migrated to VC++.NET 2005 version. Initially this migration process has coded and tested in VC++...
0
by: Paavo Helde | last post by:
Shawn <sfncook@gmail.comwrote in news:6410e273-318e-44a0-9735-b922366ca1ab@w1g2000prd.googlegroups.com: I bet you have some multithreading access error. Ensure that your threads do not read or...
2
by: =?Utf-8?B?c29jYXRvYQ==?= | last post by:
Hi, I have a DLL in VC6, when a specific function is called it will spawns a few threads and then return. The threads stay running and inside one of these threads an event is created using the...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.