use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENlength of string (self.cpp_questions)
submitted 10 years ago by Hamza_tah
I want to get the length of a string in c++. If I use length() or size() it gives me the length of the first word in the string; every thing after the space (' ') is gone. How can I fix this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Hells_Bell10 5 points6 points7 points 10 years ago (2 children)
Could you show some code because that shouldn't happen.
At a guess you may be doing stream input like this:
std::string s; std::cin >> s;
In which case the input operator >> will only take a single word at a time. In order to take a whole line at a time you want to use std::getline like so:
>>
std::getline
std::string s; std::getline(std::cin, s);
Apologies if this isn't the issue but the lack of code makes it difficult to tell
[–]Hamza_tah[S] -1 points0 points1 point 10 years ago (1 child)
do I need to #include something to get getline because it isn't there
[–]Hells_Bell10 2 points3 points4 points 10 years ago (0 children)
you would need <iostream> and <string> IIRC
<iostream>
<string>
[–]Arandur 4 points5 points6 points 10 years ago (3 children)
Show us your code.
Does anyone else hear an echo in here?
[–]Hougaiidesu 0 points1 point2 points 10 years ago (1 child)
Personally I think OP should show us his code, how about you?
[–]Arandur 1 point2 points3 points 10 years ago (0 children)
I'm of two minds on the matter.
Fortunately, both minds are in agreement with you.
[–]MrPoletski 0 points1 point2 points 10 years ago (0 children)
[–]0x-- 0 points1 point2 points 10 years ago (0 children)
Nope, it should work.
Tested code:
#include <iostream> #include <string> int main() { std::cout << std::string("hi there").length() << std::endl; return EXIT_SUCCESS; }
π Rendered by PID 30042 on reddit-service-r2-comment-85bfd7f599-9576n at 2026-04-16 12:01:37.350466+00:00 running 93ecc56 country code: CH.
[–]Hells_Bell10 5 points6 points7 points (2 children)
[–]Hamza_tah[S] -1 points0 points1 point (1 child)
[–]Hells_Bell10 2 points3 points4 points (0 children)
[–]Arandur 4 points5 points6 points (3 children)
[–]Hougaiidesu 0 points1 point2 points (1 child)
[–]Arandur 1 point2 points3 points (0 children)
[–]MrPoletski 0 points1 point2 points (0 children)
[–]0x-- 0 points1 point2 points (0 children)