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

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 \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.

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?

commented: You used code tags on very first post...!!! Great!!! and ya welcome to the family... +5

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.

Hope that helps!

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


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.


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.


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.

Thanks a lot for the further explanation.

Its just a string concatination to another string.

I think that's what the example was trying to show. It was about the use of strings.

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?

From my experience, you should NEVER use a double as a loop counter. There are very odd comparison problems that I guess are floating point errors that really mess things up sometimes.

I would do something like this

for(unsigned int i = 0; i < 25; i++)
double d = static_cast<double>(i);
//now use d for whatever you wanted to use i for.

Yes, I was also thinking of static_cast , but I know it from another book. This is just the second chapter, and nothing is specified about... templates (?), other than the vector.

Here is another way to do it since the requirement is to use <vector> class.

#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

int main()
   vector<float> ay;
   for(float i = 0.0F; i < 1.0F; i += 0.1F)

   vector<float>::iterator it = ay.begin();
   for( ; it != ay.end(); it++)
       cout << left << setw(10) << setfill('0') << *it << "\n";

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.

Here is a vector tutorial that might help you. They are not all that difficult -- the code I posted is about as basic as one could get.

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.

Any help?