Cpp-Primer icon indicating copy to clipboard operation
Cpp-Primer copied to clipboard

ex3_5b Why check whether str is empty?

Open jokerkeny opened this issue 6 years ago • 1 comments

https://github.com/Mooophy/Cpp-Primer/blob/master/ch03/ex3_5b.cpp In line 13, for (string buff; cin >> buff; str += (str.empty() ? "" : " ") + buff); why should it check whether str is empty? Could it be empty?.

jokerkeny avatar Feb 21 '19 05:02 jokerkeny

str is empty at first. When you input some words, There should be a blank space between each two words, so it's neccessary check whether str is empty, If not ,insert " ".

razeimmortal avatar Feb 21 '19 05:02 razeimmortal