1

I recently decided try learn a bit of programming, so I wrote my " hello world" and it did not work. This is my code

#include <iostream>
using namespace std;
int main()
{
cout << "hello world!";
return0;
}

I have also tried to reinstall g++ and using #include <iostream.h>.

All these exclamation marks say basicly the same: unable to resolve identifier (cout etc.). The one at the top says: Cannot find include file iostream

MalteK
  • 424
  • 2
  • 7
  • 19
  • Please tell us which are the exact errors. And return0 is not a keyword. Have you installed the development libraries? sudo apt-get build-essential – Rmano Mar 28 '14 at 15:38
  • @Rmano oops, yeah it is actually return(0), but did you mean sudo apt-get install build-essential? – MalteK Mar 28 '14 at 15:46
  • No, it's return 0 (yours works, as return ((((0)))) works, but return is a statement, not a function). – Rmano Mar 28 '14 at 15:48

1 Answers1

1
  1. you need the development headers.

    sudo apt-get install build-essential

  2. you have a typo

    return 0

    (note the space)

  3. Now it compiles, but you missed the newline after the output string...

    cout << "Hello world!" << endl;

    or

    cout << "Hello world!\n";

    otherwise your output will be messed up with the next shell prompt:

Result of your program without the new line:

[:~/tmp] 1 % g++ lilla.cc
[:~/tmp] % ./a.out 
hello world!%  

(Note the % ...)

For the future, if you have some header file missing, normally the problem is that you do not have the corresponding -dev package. To try to find it, it is a good thing to install apt-file and search for it (this is an unrelated example, because iostream.h has too much hits):

apt-file search missing_header_file.h

...and then wade through the result and see if a relevant package carries it.

Rmano
  • 31,947
  • could you explain the same for a noob again:)? what do you mean with new line after output string. In the book I use to learn c++ it is just like up above ( except from the typo:)) and what what difference does it make, when it is a statement not a function? oh and sudo apt-get build-essential does not work for me, just sudo apt-get install build-essential works. – MalteK Mar 28 '14 at 15:57
  • Yes, the missing install was my typo. If you want a carriage return (a new line) after your "Hello world!", you have to add it... but this has nothing to do with Ubuntu, is C++ 101. I can just advise you to read a good book, or a good programming class (you probably will appreciate a generic one, before jumping to a specific language). – Rmano Mar 28 '14 at 16:01
  • but why does it matter, I mean when I want my cursor to stay in the first line, why can I just skip the "endl./\n" part and go right for it? btw i will upvote when I understood it, not earlier:-) – MalteK Mar 28 '14 at 16:08
  • @ubuntu.stuff, yes, if you do not want the newline, don't put it. But normally you want it. If your shell prompt is programmed for example to clean the current line, you will see no output. YMMV. And by the way, this is my free time; I teach for my job and normally get paid. I understand the program now compile, so this answer is correct. Do whatever you want with the votes. Goodbye. – Rmano Mar 28 '14 at 16:10
  • but you just said that a % would appear if I don't do it. I know I am a noob by the way, I just started reading the book, so please have some patience;-) and what is abou the function/statement thing? – MalteK Mar 28 '14 at 16:13
  • @ubuntu.stuff, these are basics of programming. No way I can teach you programming 101 here. Let me google this for you... http://www.cplusplus.com/doc/tutorial/functions/ – Rmano Mar 28 '14 at 16:17
  • ok than don't answer, I was just wondering... anyway thx for the help. – MalteK Mar 28 '14 at 16:23