-4

This shows no output:

int x = 10;
cout<<"Hello C++ " + x ;

But this does show output:

int x = 10;
cout<<"Hello C++ ";
cout<<x<<endl;

What is the problem? They seem exactly the same.

BTW I'm Using Visual C++ 6.0 On Visual Studio 2010 .

wally
  • 10,717
  • 5
  • 39
  • 72
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Dec 28 '16 at 14:39
  • The first version also does not flush the stream and there's no newline... so how can they be the same? – LogicStuff Dec 28 '16 at 14:40
  • 3
    Why Are You Talking Like This? – Lightness Races in Orbit Dec 28 '16 at 14:40
  • 2
    _@Mario_ `"Hello C++ " + x` does pointer arithmethic. It yields the address of the literal + 10. – πάντα ῥεῖ Dec 28 '16 at 14:42
  • 1
    To everyone voting to close because of "typographical error", what it the case here is different, and the OP is just used to languages where strings can be concatenated with numbers by default with `+` operator? – SingerOfTheFall Dec 28 '16 at 14:42
  • 3
    @SingerOfTheFall: The close reason is also used in practice to indicate that the question is so basic and so lacking in fundamental beginner research that it seems unlikely to be of substantive, on-topic use to future readers. It doesn't really matter what the OP is "used to". This is clearly not an actual typographical error, but it is almost as inconsequential to the wider programming community. Although a canonical dupe would have been better. As it happens, I didn't close vote at all, but instead answered. – Lightness Races in Orbit Dec 28 '16 at 14:45
  • Absolutely agree with @Lightness. – πάντα ῥεῖ Dec 28 '16 at 14:46

5 Answers5

5

Because that's not how to use streams, or strings.

The + operator is for:

  1. adding two numbers together, or
  2. concatenating two std::strings together.

Here you have a string literal and a number. The + operator is not compatible with those.

(What you actually end up doing is "adding" 10 to the pointer representing the string literal; as it happens, because your string is ten characters long, that leaves the pointer exactly where your string literal's NULL terminator is, so it is like trying to print "".)

The correct approach, as you have yourself found via the second example, is to use the stream's << operator again.

cout << "Hello C++ " << x;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

How do you append an int to a string in C++?

I suggest you look closely at the above post to tackle your problem.

In C or C++, concatenation is done best using string streams. But to specifically answer your question:

This C++ Code Shows No Output:

int x = 10;
cout<<"Hello C++ " + x ; //uses "+" as you are trying to concatenate an "int" to some output to the stream

But This One which Shows An Output:

int x = 10;
cout<<"Hello C++ "; //output directly to stream
cout<<x<<endl; //same here, no concatenation

What's the problem then, are they exactly the same ? The problem is with "+" concatenation to the stream and NO, they aren't the same! :)

Community
  • 1
  • 1
1
    "Hello C++ " + x ;

makes no sense. You add 10 to the address of the string "Hello C++" and then std::cout should do an output of this what is on address "Hello C++" + 10. So this makes simply no sense. std::cout is part of the iostream.

0x0C4
  • 205
  • 1
  • 11
0

Try using instead of: cout<<"Hello C++ " + x ;

cout<<"Hello C++ " << x ;
DasOhmoff San
  • 865
  • 6
  • 19
  • Not a bad first answer. I have upvoted it, but in future you may wish to consider adding some _explanation_ to your answers so that the OP knows _why_ your solution is the right one. – Lightness Races in Orbit Dec 28 '16 at 14:46
-2
int x = 10;
cout << "Hello world!" << ++x;
return 0; 

If you want to increment a variable, you should use ++x. If you want to decrement it, use --x.

And, if you want to show a variable next to another one, or next to another string, use << between them.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055