Thread: cin a whole line ?

  1. #1
    Unregistered
    Guest

    cin a whole line ?

    How do i cin a whole line ?

    For example, if I type both firstname and lastname,
    the following code doesn't work.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    main()
    {
    	char name[20];
    
    	cout << "Enter Name : ";
    	cin >> name;
    
    	cout << "Hello " << name;
    
    	getchar();
    }
    http://mahurshi.tripod.com/mainframes.htm

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    use cin.getline()

  3. #3
    Unregistered
    Guest
    You can also declare "name" as a string variable, string variables can include whitespaces.


    O.B.T.W....Don't forget to include the string.h header file.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Yeah - your problem's stemming from the fact that cin stops reading in for each variable at whitespace.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    Your code should be the following:
    #include <iostream.h>
    #include <stdio.h>

    main()
    {
    char name[20];

    cout << "Enter Name : ";
    cin.getline( name, 20 ); //there is another argument which is the terminating character usually '\n'

    cout << "Hello " << name;

    getchar();
    }
    Regards,
    Ammar...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  2. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM