Discussion Locked

Help with hello world C++ program please.!

0 helpful
15 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I put int his code...

int main()
{
cout << "Hello world"; end1;
cout << "I want to be a programmer"; end1;
cin.get()
}

The error is it needs a ";" after < in iostream whats up with that?

15 replies

by charliespencer

You may want to repost this as a Question instead of a Discussion. The Questions forum is a better place for technical topics, and has a very different audience.

by jaqui

your code is for c++ but isn't configured to use c++ in a gui, which is where it's best used.

the c version of it would be:

[pre]
#ifdef HAVE_CONFIG_H
#include
#endif

#include
#include

int main(int argc, char *argv[])
{
printf("Hello, world!\n");

return EXIT_SUCCESS;
}
[/pre]

a c++ version:
[pre]
#include "hello.h"

#include

#include
#include

hello::hello()
: KMainWindow( 0, "test" )
{
// set the shell's ui resource file
setXMLFile("testui.rc");

new QLabel( "Hello World", this, "hello label" );
}

hello::~hello()
{
}
return 0

[/pre]

by jaqui

the .h is not needed by C++ any more, it hasn't been for 4 years or so.
It works with it, but isn't needed.

by pringles86

I just copied and pasted it from an old assignment of mine... I didn't bother to take it out.

by jaqui

are hard to break.

We were taught the isiostream.h and they changed the spec on us.

I bet the next release of the spec kills the support for iostream.h and makes it only iostream.

The code I posted was the basic KDE hello World app generated by Kdevelop, it doesn't even include iostream, unless you dig into the other includes.

by xsubxwooferx

I put #include but I had to take out both end1 command cause it would keep giving errors.......how would you make 2 lines so when it shows up in DOS it is Hello WorldI want to be a programmer and it say ......Hello World
I want to be a programmer

by jaqui

[pre]
#include ;

int main()
{
cout << "Hello world"; endl;
cout << "I want to be a programmer"; endl;
cin.get()
}
[/pre]
The include needs the semicolon on the end, as with any directive.

endL not end1, though it is more common to use the newline symbol of /n;
so cout>>Hello World /n;

Then there is the fact of cout being a c command not a c++ command any more.

try stdout::Hello world /n;

you also seem to be missing the namespace declaration for the new spec of c and c++
std [ standard namespace ]

making me think at this ungodly hour of the day, I'm not responsible for typos in anything I'm posting :p

and, a great place for asking questions about c and c++ programming:

http://cboard.cprogramming.com/

by xsubxwooferx

Heres the new code I did
#include ;

int main()
{
cout << "Hello World"/n;
cout << "I want to be a programmer"/n;
cin.get();
}

and here are the errors...In function `int main()':
error: `cout' undeclared (first use this function)
error: (Each undeclared identifier is reported only once for each function it appears in.)
error: `n' undeclared (first use this function)
error: `cin' undeclared (first use this function)

Execution terminated

by jaqui

with no namespace declared as per the current specification there are no functions available.

this is where the std::cout form comes from.
std being the standard namespace

by jaqui

To avoid naming conflicts, the concept of namespaces was incorporated into C++. This means you can take classes, functions, and variables and put them in a namespace. There's a namespace named std. That namespace includes a bunch of basic things you use all the time, like cout and cin. To use them you would do something like this:

Code:

std::cout << "hello world" << std::endl;

Since typing std:: can be tiresome, you can tell the compiler to automatically check out certain namespaces or certain elements inside certain namespaces when it comes across an unknown element.

Code:

using namespace std;

...

cout << "hello world" << endl;

When cout and endl are reached by the compiler, it knows to check namespace std to see if cout and endl happen to be there. If it finds them there, it uses those particular elements.

Again, namespaces are there so you can avoid naming conflicts. You might not make a class with the name cout, given that you know one exists, but you might accidentally make one for something less well known and that might cause issues. Namespaces allows you to organize your code.

with everything posted erlier that give you the complete, correct code.

by trilokacc

I think u r code need modification
#include// #include added
using namespace std;//namespace added
int main()
{
cout << "Hello world"; endl;
cout << "I want to be a programmer"; end1;
cin.get();//semicolon added
return 0;//code added
}

by jmgarvin

#include

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

That should do it. If you want the C version:

#include

main()
{
printf("Hello World!\n");
exit 0;
}

Keep in mind this isn't correct coding, nor should it be used on a regular basis, but:
A) It's simple
B) It works

Don't pick up bad habits not, but be SURE you understand why your program won't compile.

This discussion is closed to new replies.