The difficulties I have in learning C++

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 :)

I'd also like to know if return 0; and \nare 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.

icode is "inline code", so if you have more than 1 line, it is not INline anymore haha. return 0; tells the OS that the program finished properly - i.e. without errors. \n outputs a new line without flushing the buffer. There are zillions of threads around here discussing how/when to flush buffers - but what I've gathered/summarized is that you should use \n when you need it to be fast and don't care if you see the new line right away, and endl when you need to make sure the new line appears right away.

As to you main problem - you are correct assigning 8 to a string isn't going to do much good - but casting it as a char with quotes did the trick.

Little corrections to both the posts given above with some information:

@23.12.2012:

Here s1+=8; doesn't really help because ASCII value of 8 is '\b' character the backspace character.And by executing the above statement with earlier string s1 which was "Hello World! I am" we get "Hello World! I am\b" as a result of which we get just "Hello World! I aToday!" in the output.

@daviddoria:

Casting 8 to a char or to say (char)8 would result in the same situation as stated above as the compiler tries to find the character with ascii value 8 which results in backspace again.

@Both:

Treating 8 itself as a character rather than ASCII is done by enclosing it with single quotes as '8'.Using this the output of "Hello World! I am 8 Today!" can be got by the following code :

s1 = s3 + " " + s4 + " ";
s1 += '8';
cout << s1 + " " + s2 + "!\n";

The thing which the OP has done above by the statement s1 += " 8 "; is concatinating s1 with the string " 8 " i,e <space>8<space>.Here the string " 8 " is used directly and it has no relation with respect to casting of 8 to character or anything.Its just a string concatination to another string.

Well... I'm back with a question. One of the exercises requires me to fill a vector with 25 floating-point values. The code I use is for(double i = 0.0; i < 25.0; i++) . I'd say this is not the right way to do it, because the output is the same as if I were using int instead of double . So... how can I improve?

Well... thanks a lot for the help, but these are too advanced, and I think I'll do the exercise using integers. The main points were the basic usage of vector and getting your foot wet with data types.

Well... no, it's not, actually. I repeat, the book simply wanted to get my foot wet with the vector, it even states that more about it is written in the second volume of the book, so I can't understand anything from what you've written in your code.

>Also, shouldn't you have used cstdio instead of stdio.h ? I read somewhere that the .h at the end becomes a c at the beginning of the name in C++.
Yes, what you mean is that you've read about new-style headers.
In fact you may even just leave that line out, it doesn't make any sense in that code, and I even can't find a reason on why it slipped in there :)

Hmm... I've got a new problem. I'm using Leopard with GNU C++. I can't find any document on the compiler's page regarding the use of your own libraries. It's just that an exercise requires the creation of my own set of functions, and I'd like to know where to place them in order to make them usable.