2

Previously, I have been interested in learning C++ so I decided to go for "InfiniteSkills" training video (http://www.infiniteskills.com/training/learning-c-plus-plus.html) The instructor start by teaching "Hello World" as a basic as always.

Here is the code:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!";
    return 0;
}

but after I build it using CodeBlocks it won't compile I have also tried using Sublime text too, but the result seems to be the same Any suggestion?

Image:

Boann
  • 48,794
  • 16
  • 117
  • 146
kybookie
  • 41
  • 1
  • 9

5 Answers5

1

you should add a newline character to the end of the line you want to print. Probably you are not seeing your output because it is still in the buffer. As @Quirliom noted: It may not be the stdio buffer but Sublime buffering until new lines...

cout << "Hello, World!\n";

or

cout << "Hello, World!" << endl;
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • You may want to justify this answer due to the fact that as @FUZxxl says, stdio buffers are implicitly flushed on exit. – Sinkingpoint Jan 05 '15 at 11:30
  • I included your comment that indeed it might be because it is a different buffer... – Chris Maes Jan 05 '15 at 11:33
  • 2
    @ChrisMaes Please note that `endl` not only adds a newline character, but also flushes the buffer immediately. This can slow you programs. Hint: by default write `'\n'` for newline characters, and `endl` only when needed. – Didier Trosset Jan 05 '15 at 11:49
0

As per the comments, you are unable to see the output. Try this:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!";
    cin.get(); // This waits for you to input something and allows you to see the input.
    return 0;
}
thepace
  • 2,221
  • 1
  • 13
  • 21
0

I don't know the real solution for this problem. But my guess is because of the complier. I have test with CodeBlocks and Sublime Text 3 on mac both won't print "Hello World" for me. So I decided to test with another which is "Xcode" and it works! I don't know what the real problem is but if anyone have any problem like me you may want to try using another complier :)

Thank you everyone for your suggestion and happy coding!!!!

enter image description here

kybookie
  • 41
  • 1
  • 9
  • Check the compiler settings of CodeBlocks and SublimeText and let us know so that the actual problem can be solved without evading it :) CodeBlock :http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/ SublimeText: http://stackoverflow.com/questions/14621562/how-do-i-compile-and-run-a-c-program-in-sublime-text-2 . Do revert back. – thepace Jan 05 '15 at 12:16
  • 2
    The IDE (i.e CodeBlocks, Sublime, XCode) is not a compiler, it's just a text editor with extra functionality. Those IDEs use Clang or GCC-G++ as compilers. – Fernando Jan 05 '15 at 20:02
0

You should both add a newline command in your print function and some sort of pause.

#include <iostream>
using namespace std;
int main(){
  cout << "Hello World!\n" //calls for a newline
  cin.get(); //pauses until a key is pressed
  return 0;
}

Try this and see if it works

Bodi Osman
  • 117
  • 1
  • 7
0

I had this problem too but I was able to fix it by reinstalling the C++ plugin for VS Code. I think the iostream wasn't actually there originally.

Ikari
  • 1
  • 3