getline help!

This is a discussion on getline help! within the C++ Programming forums, part of the General Programming Boards category; I have this function for my address book program but the first getline isn't working, somebody please tell me why! ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    37

    getline help!

    I have this function for my address book program but the first getline isn't working, somebody please tell me why!

    PHP Code:
    void ChangeEntry (sturec &bookfstream myfileint &recordnum)

        
    int whichnum;
        
    char temp1[50], temp2[50];
        
        
    cout << "\nRecord number to change? ";
        
    cin >> whichnum;

        if (
    whichnum recordnum)
            
    cout << "\nI'm sorry, there are not that many records.  \nPlease select a record"<<
            
    " between 0 and "<<recordnum<<endl<<endl;
        
                    else
        {
            
    myfile.seekg(whichnum*98);
            
    myfile.read(temp115);

            
    cout <<"Last name: "<<temp1;
            
    cout <<"\nLast name? \n";
            
    cin.getline (temp215);
            
    cout << temp2;
            if (
    strcmp(temp2"")!=0)
                
    strcpy (book.lastnametemp2);
            
    myfile.read(temp115);
            
    cout <<"First name: "<<temp1<<endl;
            
    cout <<"First name? ";
            
    cin.getline (temp215);
            if (
    strcmp(temp2"")!=0)
                
    strcpy (book.firstnametemp2);    

        }



  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    because there is still a newline char in the input buffer following the line:

    cin >> whichnum;

    to fix it add this after the above line:

    cin.ignore();

    As you get more sophisticated with your programming skills read about istream methods (>> and getline in particular) in your favorite textbook (as there are some picky ooni details that can trip you up if you don't know how these methods work), or do a search on this board using getline as the searchword.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 05:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21