Thread: Seg Fault AFTER the program finishes..

  1. #1
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45

    Seg Fault AFTER the program finishes..

    Below is a method that properly deletes a Professor from a list of Professors.

    If this method is not called(as there are other options in the menu) then the program terminates just fine. However, after calling this method to delete a Professor, and subsequently terminating the program after seeing that the method did in fact perform delete correctly, a Seg Fault appears.

    Folks in #C++ on freenode were very helpful, but we did not completely get rid of the problem. It is believed the problem may lie within the for loop with profIterator. I do know that the program runs without a hitch if there exists only one element in the list. But not for more than one element, which is where the for loop comes into play. It was suggested I create an operator<< overload and output what profIterator is returning.. but for some reason, the standard style:

    Code:
    ostream& operator<<(ostream& os, const Professor& P) {
       os << P.getLastName() << ", " << P.getFirstName() << endl;
    }
    does not compile correctly. The errors fill a large screen with 'trait' stuff.

    Can you see the problem without having to implement an operator<< method? I'd be grateful if you could.

    Here's the method:
    Code:
    // delete a specific professor
    bool deleteProfessor()
    { 
      // local variable for readibility
      int size = profs.size(); 
    
      // handle empty list
      if ( size < 1 ) {
        cout << endl << "  The list contains " << profs.size() << " elements.\n" << endl;
    
        // handle non-empty list
      } else {
    
        // handle one element list, no need to iterate
        if ( size == 1 ) {
          cout << endl << "  Only one element in the list. Delete (y/n)? " << flush;
          char choice;                // for yes or no answer
          cin >> choice;              // get user's input
          cin.ignore( 100, '\n' );    // ignore all input to end of line
    
          if ( toupper( choice ) == 'Y' ) {
            // delete the only professor in the list
            profs.clear();
            cout << "\n  Professor succesfully deleted. 'P' to print.\n" << endl;
            return true; // return immediately instead of continuing
          } else {
            // notify user that no one was deleted
            cout << "\n  No element was deleted. List is intact.\n" << endl;
            return true; // return immediately instead of continuing
          }
      
          // handle larger than one element list
        } else {
          // present the list for viewing
          handlePrintList(); 
    
          cout << "\nWhich professor do you wish to delete (1-" << size
               << ", or 0 to quit)? " << flush;
          
          int choice;                 // for user input(which professor)
          char* option;               // for conversion of string to int
          cin >> option;              // get the user's input
          cin.ignore( 100, '\n' );    // ignore all input to end of line
          choice = atoi( option );    // convert to int for evaluation
    
          // quit the operation if user desires to do so
          if ( choice < 1 ) {
            cout << "\n  Quitting - returning to main menu.\n" << endl;
    
            // take the appropriate action as requested
          } else if ( choice >= 1 && choice <= size ) {
            // delete the chosen professor
            list<Professor>::iterator profIterator; // iterator for stepping through Professor list
            int k = 1;                     // counter used to number list items
    
            for ( profIterator = profs.begin();     // pointer to first element
                  k != choice;                      // stop point doesn't equal input
                  profIterator++, k++ )             // increment both iterator and counter
              ;   // <- purpose is to move iterator to desired location and stop/continue
              
            profs.erase( profIterator );      // erase professor at profIterator location
            // notify user of success
            cout << "\n  Professor succesfully deleted. 'P' to print.\n" << endl;
          } else {
            // error handling
            cout << "\n  Invalid input. Please try again.\n" << endl;
          }
        }
      }
      return true;
    }
    Thanks

    EDIT: blasted.. well if anyone needs this there ya go. the problem was solved ten minutes after I posted this. Here's the correct codeblock that was causing the problem:
    Code:
          int choice;                 // for user input(which professor)
          string option;              // user option for conversion of string to int
          cin >> option;              // get the user's input
          istringstream i(option);    // using stringstream 
          i >> choice;                // convert string to int
    Last edited by CaptainMorgan; 12-02-2006 at 05:17 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> char* option;
    In case anyone was wondering, the issue was that this variable was never initialized, so it pointed to invalid memory. Switching to string was a good choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. access violation segmentation fault raised in your program
    By dinamit875 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 11:44 AM
  2. seg fault with any function call
    By LegoMan in forum C Programming
    Replies: 5
    Last Post: 04-15-2009, 05:30 PM
  3. Seg fault
    By Fox101 in forum C Programming
    Replies: 18
    Last Post: 04-09-2008, 04:15 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. seg fault on unix platform
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-08-2001, 12:04 PM