3

enter image description hereI've just started learning the C++ programming language, using Xamarin Studio as my IDE on OSX 10.9.5 and following The C++ Language Tutorial by Juan Soulie'.

I'm following everything the guide says but whenever I compile this code:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! ";
    cout << "I'm a C++ program";
    return 0;
}

The terminal screen appears and displays this!

Hello World!  


press any key to continue... logout

[process completed]

Is this something that I'm doing wrong or is this an issue with my compiler? I also tried Xcode but got exactly the same results.

Sorry if this question sounds dumb!

Marc

Marc Freeman
  • 713
  • 2
  • 7
  • 30
  • 3
    Flush the stream with an `endl`. `cout << "I'm a C++ program" << endl;` – Luchian Grigore Apr 14 '15 at 16:00
  • 3
    @LuchianGrigore: It should be flushed automatically, by the destruction of the static `ios_base::Init` object at the end of the program. – Mike Seymour Apr 14 '15 at 16:03
  • 3
    possible duplicate of [std::cout won't print](http://stackoverflow.com/questions/14858262/stdcout-wont-print) – Eric Hughes Apr 14 '15 at 16:03
  • @Marc Freeman What options are you passing to the compiler? – Emil Laine Apr 14 '15 at 17:32
  • @zenith options? I've gone back to xCode and I'm just pressing Product>Perform Action>Compile "File-name.cpp". Is there anything I should be doing that I'm not? I'll add a screen shot to the initial question if I can. – Marc Freeman Apr 14 '15 at 18:16

2 Answers2

2

I can't reproduce it on my machine but I'm guessing you need to flush the buffer, with either cout << endl; or cout.flush();

dwcanillas
  • 3,562
  • 2
  • 24
  • 33
1

You need to declare the end of line when starting a new line.

Change your code to the following:

#include <iostream>
using namespace std;

    int main()
    {
        cout << "Hello World! "<< endl;
        cout << "I'm a C++ program"<<endl;
        return 0;
    }

Your terminal should look like this (this is on my machine): enter image description here

Javia1492
  • 862
  • 11
  • 28
  • 2
    The OP does not have or need a newline between the strings, and `'\n'` suffices for a newline if you don't need to guarantee the text is printed before the next guaranteed flushing point (like at the end of the program). – chris Apr 14 '15 at 16:08
  • @chris How do you know what the OP wants? If there is no endl anywhere at all he might have wanted it on 2 lines. This answer is just as valid as the previous one. – Philip Stuyck Apr 14 '15 at 16:15
  • Yes, if all he needed was a new line, he can use `\n`. I should have clarified that `endl` inserts a new-line character **and flushes the stream**. – Javia1492 Apr 14 '15 at 16:15
  • I've followed everyone's advice (I'm extremely grateful for how many people have come to help me) but now it's not even opening the terminal. I've tried using the 'std::endl;' and 'std::cout' as well as 'endl' and 'cout.flush'. Nothing seems to be working. – Marc Freeman Apr 14 '15 at 16:17
  • @PhilipStuyck, I'd say it's reasonable to assume the OP coded it the way he wants (especially considering there's a space at the end of the first string) until there's reason to suspect otherwise. The OP "might have wanted" a large number of things, and guessing at them all would make for an extremely off-topic answer. – chris Apr 14 '15 at 16:20
  • 1
    @MarcFreeman Try putting a `system("pause");` before `return 0;`. I just ran the code on my machine and it was fine. – Javia1492 Apr 14 '15 at 16:22
  • @chris If OP wants it all on one line, that's fine. If he wants two separate lines, that's fine. I dont see any point in discussing "how" he wants the output. I can be assumed he wants two line since he is using cout twice with two different strings when he could just combine it. He never specified how he wants it so it is left up to assumption due to the nature of the question/answer. `endl` happens to comply with both new line and stream flushing. If this is not a sufficient answer to solve his original problem of being able to display both strings, then correct me with a valid answer. – Javia1492 Apr 14 '15 at 16:27
  • @Javia1492 I've also tried this, everything I'm trying is failing. I'm gonna try a bit of system maintenance, reinstall my IDEs and try again tomorrow! – Marc Freeman Apr 14 '15 at 16:29