Hi! I opened this thread because I've found out that, as I'm making my way through a textbook, I find many problems which I can't figure out for myself.
To start, Thinking in C++ has this buggy program right in its second chapter. The code looks OK to me, but the compiler/console wants to prove otherwise. The output, I say, should be "Hello, World! I am 8 Today!". Instead, I get "Hello, World! I aToday", which is... well... wrong :) I can't figure out where the problem comes from. I'm using vim and the GNU C++ Compiler in an OS X Terminal window. Any help would be appreciated :)
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1, s2;
string s3 = "Hello, World!";
string s4("I am");
s2 = "Today";
s1 = s3 + " " + s4;
s1 += 8;
cout << s1 + s2 + "!\n";
return 0;
}
I'd also like to know if return 0;
and \n
are good programming habits, and if Thinking in C++ isn't outdated. I can see everyone recommends it, but how do we go about the broken piece of code?
Later Edit: Well... apparently, replacing s1 += 8
with s1 += " 8 "
did the trick. But I'd like this thread to remain opened, as I'm sure to encounter other problems along the way. Also, the questions right after the code snippet stand.
Thanks in advance, and I hope I won't be a bother to anyone.
P.S.: Is there a way to apply icode to more than one line at a time?