Thread: program quits when using getline..

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    program quits when using getline..

    Code:
    /************************/
    /****   GetEntries   ****/
    /************************/
    void Phonebook::GetEntries(fstream &fFile)
    {
         int i=0;
         string test1;
         if(getline(fFile,test1,'|')==NULL)
         {
                        return;
         }
         else
         {
             while(getline(fFile,vEntries[i].name,'|') != NULL) //ERROR
             {
                       i++;
                       getline(fFile,vEntries[i].cnumber,'|');
                       getline(fFile,vEntries[i].hnumber,'|');
                       getline(fFile,vEntries[i].address);
             }
         }
    }
    The above code is suppose to take a .txt file and read in strings and set them to variables...Yes, for testing, I made sure there was a text file to open before actually trying it.

    It seems that when I get to the while loop with getline... this is when the program decides to quit and send a stupid windows error report. I know for a fact that it gets to the while loop because just to test it, I set up an if-conditional statement right before the while loop just to output a simple string "yep" and cin.get(); After pressing enter, the program crashes, obviously from the while loop? What could be wrong with it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is ventries an array of sufficient size?

    > i++
    Why are you incrementing the index between reading the name and reading the address.
    Won't that screw up the data?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    ventries is a vector... and i will move the increment down to the bottom of the data

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Why not just use a linked list?

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    I'm not very familiar with linked lists... and this program is suppose to get me more familiar with Vectors.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    3
    invalid floating point operation error ?
    overflow?
    be carefull this

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    invalid floating point operation error ?
    overflow?
    I am not familiar with these terms...could you describe?

    Maybe if I posted where the function is located in the code, it would be easier to understand whats going on.

    Code:
    /*********************/
    /****  MOpenFile  ****/
    /*********************/
    void MOpenFile()
    {
        Phonebook pbEntry;
         
        string filename;
        string directory = "Data/";
        string dirfile;
        
        CLEARSCREEN;
        
        cout<<"\t\t\tPlease enter the filename: ";
        cin>>filename;
        filename.append(".txt");
        
        dirfile=directory+filename;
        
        fstream File(dirfile.c_str(), ios::in);
        if(File.fail())
        {
             cout<<"\n\t\t\t   Error opening file!";
             return;
        }
        else if(File.good())
        {
            cout<<"\n\t\t\t\'"<<filename<<"\' was opened!!"<<endl<<endl;
            cin.ignore();
            cin.get();
            CLEARSCREEN;
        }
          
        int entrymenu;
        bool done=false;
        
        pbEntry.GetEntries(File);   // Right here <<<<<<<<<<<<<<
        
       while(done==false)
       {
        cout<<setfill('-')<<setw(80)<<"-";
        cout<<"\t\t\t\t    "<<filename<<endl;
        cout<<setfill('-')<<setw(80)<<"-";
        
        pbEntry.DisplayEntries();
        
        cout<<"\t\t\t\t-(1)Add Entry"<<endl;
        cout<<"\t\t\t\t-(2)Delete Entry"<<endl;
        cout<<"\t\t\t\t-(3)Edit Entry"<<endl;
        cout<<"\t\t\t\t-(4)Save and Quit"<<endl;
        cout<<"\t\t\t->: ";
        cin>>entrymenu;
        switch(entrymenu)
        {
                         case 1:
                              pbEntry.Add();
                              break;
                         case 2:
                              pbEntry.Delete();
                              break;
                         case 3:
                              pbEntry.Edit();
                              break;
                         case 4:
                              done=true;
                              File.close(); //Erase once WriteEntries is called
                              return;
                              break;
                         default:
                                cout<<"\t\t\tNot Valid!";
        }
       }
        
        //WriteEntries(File);
    }
    Last edited by gL_nEwB; 05-20-2006 at 11:36 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My guess is you're accessing elements of the vector which don't exist yet.

    Say
    Code:
    <some type, same a ventries> temp;
             while(getline(fFile,temp.name,'|') != NULL) //ERROR
             {
                       getline(fFile,temp.cnumber,'|');
                       getline(fFile,temp.hnumber,'|');
                       getline(fFile,temp.address);
                       vEntries.push_back( temp );
             }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't like this line
    Code:
    while(getline(fFile,vEntries[i].name,'|') != NULL) //ERROR
    It shouldn't be the cause of your problem but you actually compare the return of getline ( a reference to the input stream that evaluates to a boolean ) to a null pointer.
    I would prefer
    Code:
    while( getline(fFile,vEntries[i].name,'|') )
    Kurt

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    Salem, you were very correct! LOL, I treated the GetEntries as the DisplayEntries function, thinking that there were already entries in the vector... thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM