Thread: Basic doubt

  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,268
    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, 11:03 AM
  2. function calling: basic doubt
    By doubty in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 02:31 AM
  3. basic doubt
    By sincere_cute in forum C Programming
    Replies: 2
    Last Post: 06-20-2007, 03:33 AM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 11:35 PM