cin.getline()

This is a discussion on cin.getline() within the C++ Programming forums, part of the General Programming Boards category; i have written this code for a phone directory for entering data of 5 persons when i use "cin>>" then ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    cin.getline()

    i have written this code for a phone directory for entering data of 5 persons when i use "cin>>" then my program works but when i use cin.getline then my program does not works
    after one menu it does not repeat menu for input just display messages 4 times simultaneaously
    Code:
    # include <iostream>
    # include <conio.h>
    using namespace std;
    
    	struct directory
    	{
    		long int phone_number;
    		char name[50];
    	
    	};//end struct
    
    
    int main ()
    {
    	directory a[5];
    	for (int i=1;i<=5;i++)
    	{
    		cout<<"enter name of a person: "<<endl;
    		cin.getline(a[i].name,50);
    		cout<<"enter phone number of person: "<<endl;
    		cin>>a[i].phone_number;
    	
    	}//end for
    	
    
    	getch ();
    
    
    }//end main

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    cin skips initial white-space and newlines, reads some input, then stops at white-space and newlines (and leaves them in the input stream).

    getline() reads everything, and specifically stops at newlines.

    Mixing the two usually ends up in grief.

    Itīs generally a good idea to adopt a single, consistent approach to reading input.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    ....
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I generally agree with Salem, but unfortunately the more elegant solutions are all relatively complicated.

    A simple solution to this issue is to add cin.ignore(); on the line directly after reading in the phone number. This ignores the newline entered when the user types the phone number and hits <enter>. (You do not want to add ignore() after the call to getline, because getline ignores that newline automatically.)

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    can u give me some example how to "cin" ?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I'm not sure what you mean? Your code is using cin right now.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i mean to say i cant understand what salem is saying i cant abstract something from his words
    kindly salem give me some example

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    cin gives you multiple ways to do the same thing with its data. The data it actually has are usually things that you type. It's text.

    Salem is saying that it is OK to use this method to extract data:

    cin>>a[i].phone_number;

    You can do that with any type C++ has built in already and it will turn into what you want automagically. If this is the only way you used throughout the program, it would work. It's the same deal with getline(). If you relied on getline() it would work. While using getline() (in effect, reading strings) is elegant, Daved has also commented that it's complicated.

    What he means by this is that the strings you read need to be converted into ints or floats or whatever you have.

    How do you do that? Well it's a FAQ, and we all know that nobody reads those. Just like they hardly search for their question on the boards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  2. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  3. cin.getline()
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-29-2003, 02:14 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 06:53 PM

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