cin.getline()

This is a discussion on cin.getline() within the C++ Programming forums, part of the General Programming Boards category; Hello again, I'm having some difficulties with cin.getline() now. It seems when you use this function to input a string ...

  1. #1
    Registered User Ifurita.'s Avatar
    Join Date
    May 2003
    Posts
    4

    cin.getline()

    Hello again, I'm having some difficulties with cin.getline() now. It seems when you use this function to input a string with spaces that you have to use it for every input value or your program will run through all the input fields and exit. I use char arrays since i still can't get string to work with my Dev C++. This is rather bothersome, especially in a loop where you need to input lots of different data. for example...

    Code:
    int available() {
         for (int i=0;i<=23;i++) {
             if (!player[i].active)
                return i;
    
         }
    }
    
    void input_player_data() {
         int i=available();
         cout << "Enter the group/player's reference name: ";
         cin.getline(player[i].name, 30, '\n');
         cout << "Enter number of players in the group: ";
         cin >> player[i].quantity;
         cout << "Enter the table they will be playing on: ";
         cin >> player[i].table;
         player[i].time_s = time(NULL);
         tm *tmstruct = localtime( &player[i].time_s );
         strftime( player[i].time_str, 30, "%I:%M %p", tmstruct ); // 12 hour time
         player[i].active = 1;
    }
    In this sample, the function available() searches for a player slot that is "open" it then stores that in the int i and begins to start the inputing of the data. If I run this by itself it works just fine, but if i try to loop it with a menu before it, asking you what job you'd like to preform, it skips the name field anf starts on the quantity field. I believe it has something to do with the terminating character '\n' and the fact that without cin.getline(), input fields don't ignore '\n' and carry it over.

    Thanks for your help ahead of time.
    P.S. The menu i was talking about earlier looks like this:
    Code:
                cout << "1 -- Clock in a player or player group.\n";
                cout << "2 -- List all current players/groups.\n";
                cout << "3 -- Clock out players/groups.\n";
                cout << "4 -- Divide a player group into a single player.\n";
                cout << "5 -- Move the a group/player to a different table.\n";
                cout << "6 -- Quick Figure a Time.\n";
                cout << endl;
                cout << "Enter the corresponding number to the action you wish to preform: ";
                cin.getline(options, 5, '\n');
                if (!strcmp(options,"1")){
                   input_player_data();}
                else if (!strcmp(options,"2")){
                   list_players();}
                else if (!strcmp(options,"3")){
                   clock_out_player();}
                else if (!strcmp(options,"4")){}
                   //input_player_data();
                else if (!strcmp(options,"5")){}
                   //input_player_data();
                else if (!strcmp(options,"6")){}
                   //input_player_data();
                else {
                   x = 0;
                }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Have a look at this http://www.comeaucomputing.com/techtalk/#flushinput.
    Thats very general,maybe a cin.get() will also do.

    And your function available is a return statement missing.
    Always compile with -Wall option on.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I believe it has something to do with the terminating character '\n' and the fact that without cin.getline(), input fields don't ignore '\n' and carry it over."

    You are correct. The >> operator stops reading when it encounters whitespace(spaces, tabs, newlines), AND very importantly it leaves the whitespace character in the stream. So, after you use the >> operator, the '\n' is left in the input stream, and since the default delimiter for getline() is '\n', getline() reads in just that one char and ends. getline(), unlike the >> operator, removes the '\n' from the stream. So, it appears like the getline() statement was skipped. The lesson is that any time you use the >> operator and then subsequently use getline(), you need to remove the '\n' from the input stream. One way to do that is to use the ignore() function:

    cin.ignore();

    removes one char from the stream.

  4. #4
    Registered User Ifurita.'s Avatar
    Join Date
    May 2003
    Posts
    4
    So, your saying after every >> I should ad a cin.ignore()? Thanks for the help, didn't know that existed, most appreciated.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "So, your saying after every >> I should ad a cin.ignore()?"

    Only if you need to subsequently use getline(). If you subsequently use the >> operator, it will skip leading whitespace.

    cin.ignore() actually has 3 forms:

    1) cin.ignore()
    removes one char.

    2) cin.ignore(3)
    removes the specified number of char's

    3) cin.ignore(50, '\n')
    removes the specified number of chars or until the specified delimiter is reached, whichever occurs first.
    Last edited by 7stud; 05-29-2003 at 01:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 11:01 AM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 08:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM

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