How to read string with space (help please)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaft
    New Member
    • Nov 2006
    • 6

    How to read string with space (help please)

    hi
    i have the following code :

    string addrress;
    cin >> address;
    cout << address;

    if I enter any string with space in between , it will save the first word only.

    e.g. if i enter "this is a test"

    address = this

    how can I make address to have the rest of the string too. please help
  • shamik
    New Member
    • Dec 2006
    • 5

    #2
    use gets(address) instead of cin>>address

    and puts(address) instead of cout<<address

    Comment

    • shaft
      New Member
      • Nov 2006
      • 6

      #3
      thanks it works. but i had to declare address as Char not string. so how can i get the size or length of the Char. is like (string.size or string.length). advise me please.

      regard's

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by shaft
        string addrress;
        cin >> address;
        cout << address;
        If you want to keep address as a string, use the getline function:

        Code:
        getline(istreamVar, stringVar);
        where istreamVar would be cin, and stringVar is address. This will get every character in the input stream until a newline character is found. So, with your example, if you entered "this is a test", address would hold "this is a test".

        Comment

        Working...