cin

This is a discussion on cin within the C++ Programming forums, part of the General Programming Boards category; stupid question if i wanted to cin a string but the string consisted of multiple words, how would i do ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    cin

    stupid question

    if i wanted to cin a string

    but the string consisted of multiple words, how would i do it

    for instance

    Code:
    int main ()
    {
    char f[100];
    cin >> f;
    cout << f;
    return 0;
    }
    wont do the job if i enter a string like Good morning everyone.
    because it takes the space as a delimiter.
    any ideas... i know this is a stupid question.. early morning mind isnt working

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    71
    ignore question

    i got it

    cin.getline (f,100);

    just in case anyone has the same question in the future.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    using member function getline for char* type strings.

    cin.getline(array,sizeof_array,delim_char='\n');
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Or, for reading a line of input into a string type:

    string text;
    getline(cin, text, '\n');

    Note that for both cin.getline() and getline(), you can read in multiple lines of text by changing the "delimiter" which is the 3rd parameter. If you use a '#' as the third parameter, input will continue until those functions find a '#' sign in the input, which you can insert after say the 5th line of input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 12:14 PM
  2. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 09:16 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 03:57 PM
  5. multiple instances of cin
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2004, 05:51 PM

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