Basic doubt

This is a discussion on Basic doubt within the C++ Programming forums, part of the General Programming Boards category; hello shouldn't the code below return 11 for "hello there", it returns me the size of the first word i.e ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    30

    Exclamation Basic doubt

    hello shouldn't the code below return 11 for "hello there", it returns me the size of the first word i.e 5 for hello

    Code:
    #include<iostream>
    #include <string>
    using namespace std;
    
    int main(){
    	string a;
    	cin>>a;
    	cout<<a.size();
    }
    well if i take the input in the code itself as string a = "hello there". i get 11.

    i suspect its some thing related to input stream buffer or something. I am naive at this.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    cin's >> operator will stop at whitespace. If you want to read a whole string, do this:
    Code:
    #include <iostream>
    
    int main()
    {
        std::string s;
        std::getline(std::cin, s); 
        std::cout << s << std::endl;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    thanks bithub

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic doubt in C structure
    By cbalu in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2009, 12:03 PM
  2. function calling: basic doubt
    By doubty in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 03:31 AM
  3. basic doubt
    By sincere_cute in forum C Programming
    Replies: 2
    Last Post: 06-20-2007, 04:33 AM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 12:35 AM

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