Thread: unhandled exception error

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    Question unhandled exception error

    Does anyone know what an unhandled exception error is? It also gives an address of 77f85c41.

    The program was given by my teacher. I was able to compile and link. As soon as I type the filename (to open fstream) I get a pop up message that tells me about the unhandled exception error and it closes the window. I made some changes from the orig. because it wouldn't compile with my compiler but I can't figure out how to get past this problem. The program is supposed to read in a text file ("phone.txt") and then place a name and phone # it in a bucket determined by the calc. Anyhow, here is the code.

    Code:
    #include<iostream>
    #include<string>
    #include<fstream.h>
    using namespace std;
    
    typedef struct phonerecord
    {
      string name;
      string number;
      phonerecord * next;
      
    };
    
    
    const int buckets = 11;
    
    int hashfunction(string);
    void display_buckets(phonerecord * [], int);
    
    
    int main()
    {
       ifstream instream;
       string Infilename;
       int j;
       int whichbucket;
       phonerecord * latest;
       string newname;
       string newnumber;
       
       phonerecord * newphonerecord;
      
       phonerecord * hashtable[buckets];
        
        
        for(j = 0; j < buckets; j++)
           hashtable[j]=0; //empty
           
        
      cout<<"Enter The Name Of The File With .txt Extension:"<<endl;
      cout<<"For This Program Enter phone.txt"<<endl;
    
       
       
       getline(cin, Infilename);
       
      instream.open(Infilename.data());  
     
    
    for(;;)
    	
    	 { 
    	  
    	  getline(instream, newname);//takes in first line of document.
          
    	  getline(instream, newnumber);
    	  
    
            if (instream.eof()) break;
    	   
         
         
    	  latest = newphonerecord;
    	  latest -> name = newname;
    	  latest -> number = newnumber;
    	  //put into bucket
          whichbucket = hashfunction(newname);
          latest -> next = hashtable[whichbucket];
          hashtable[whichbucket] = latest;
          
          
          }
        
      
       display_buckets(hashtable,buckets);
      
     }
     
     
     int hashfunction(string s)
    { 
       int one, two, three;
       
       
         one = s[0];
         two =  s[s.length()]-1;
         three = (two/2)%11;
       
        return three;
      
     }
     
     void display_buckets(phonerecord * hashtable[], int buckets)
     {
       int j;
       phonerecord * aux;
         for(j=0;j<buckets; j++)
          {
            aux = hashtable[j];
            while (aux !=0)
             { 
               cout<<aux -> name;
               aux = aux -> next;
             
             }
             
           }
           
       }
    Thanks...

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Re: unhandled exception error

    Originally posted by trends
    Code:
    for(;;)
    Thanks...
    I want to say you have a problem here, for loop needs parameters, i.e. counter, parameter, and increment.

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Re: Re: unhandled exception error

    Originally posted by alpha
    I want to say you have a problem here, for loop needs parameters, i.e. counter, parameter, and increment.
    That's not a problem. That's a perfectly legal for loop. Notice this line:

    Code:
     if (instream.eof()) break;
    He's simply reading each line of the file and when he encounters EOF he breaks out of the infinite loop.


    As to the original question: an unhandled exception means you're either trying to access a pointer that hasn't been initialized, trying to access one that has already been freed or trying to delete one that doesn't exist.
    Last edited by jdinger; 11-15-2002 at 06:36 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>phonerecord * latest;
    >>phonerecord * newphonerecord;
    >>latest = newphonerecord;
    >>latest -> name = newname;
    Looks to me like latest doesn't actually point anywhere particular when you try to dereference it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    oh okay, well im still learning c++, only been programming for less than a year and the main goal of the class im taking is to learn for the AP, so i still don't know much...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM