Thread: cin / scanf

  1. #1
    Registered User
    Join Date
    Sep 2001

    Question cin / scanf


    Can somebody please help me out?

    Consider the following snippet:

    cout << "Enter your name: ";
    cin >> n;

    cout << "Hello " << n << "!" << endl;

    If I input 'Joe Bloggs' it will only print out 'Hello Joe!'.

    When using cin or scanf to read a string, how do you make it read everything the user types in (including spaces)?

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    use cin.getline()
    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 bljonk's Avatar
    Join Date
    Oct 2001

    cin.getline>> name;

    will assign all the strings on the line till it finds a new line character.

    would be usefull if only two names, but u can add more to it.
    Ünicode¬>world = 10.0£

  4. #4
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    yeah, the cin >> syntax's fields are delimited by spaces or endlines.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. 'cin' and 'scanf()' issue
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2004, 10:53 AM
  4. Cin & Cout VS. Scanf & Printf
    By MatriXxX in forum C Programming
    Replies: 19
    Last Post: 08-08-2003, 11:47 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM