I'm really sorry I have to ask this...
I'm having a hard time Converting from C to C++
I can't seem to get Hello World working.
1 #include <iostream.h>
2
3 int main()
4 {
7 cout << "\n\nHello World!\n";
8 return 0;
9 }
Printable View
I'm really sorry I have to ask this...
I'm having a hard time Converting from C to C++
I can't seem to get Hello World working.
1 #include <iostream.h>
2
3 int main()
4 {
7 cout << "\n\nHello World!\n";
8 return 0;
9 }
Your code should read:
Cheers,Code:#include <iostream>
using namespace std;
int main()
{
cout << "\n\nHello World!\n";
return 0;
}
cgkanchi
I just compiled it - and it's just fine. Can you tell me what errors you are getting?
Thanx for the help, i get this error on multiple computers and multiple versions of Windows...
Edit*
More Madness: I have no trouble compiling C code. Only get errors with C++ source.
Sorry, I don't think I'm familiar with your compiler. I'm using Microsoft Visual C++ 6.
Just so you can see the the results of your code I've attached a screen shot.
-Thanx any way gals/guys...
-I guess i'll just stick to C anyway!
-Peace*
tampabay420: If you use the code that cgkanchi posted, your program will work. You forgot the "using namespace std;" right after your includes. It's a commonly overlooked thing and is not required by all compilers.
Try it like this:
#include <iostream.h>
void main(void)
{
cout << "Hello, World!";
}
I don't think you need the using. . . line with your compiler.
The program that bludgeon posted will work, but I believe that is following the older standards. Let me explain exactly what the using namespace command does.
When you use the standard C++ functions, they are declared in the "std" scope. For example, if I did not put the "using namespace std;" I would have to say something like, "std::cout << "Hello World\n";" in order for it to work. That's just a little explanation.
If you wanna read more indepth about how namespaces work exactly, here's a link for you: http://www.cplusplus.com/doc/tutorial/tut5-2.html
Hope this helps.
I used the using namespace std;
-didn't work... but the link you sent was great. Thanx :-)
edit*
________________________________
#include <iostream>
using namespace std;
int main () {
cout << "Hello world in ANSI-C++\n";
return 0;
}
________________________________
#include <iostream>
int main () {
std::cout << "Hello world in ANSI-C++\n";
return 0;
}
________________________________
Both didn't work, same error... Look at the screen shot...
-Maybe it's my compiler...
This wont compile on Win2k, Win ME, Linux, FreeBSD- i keep getting the same error:
<source>
#include <iostream>
using namespace std;
int main () {
cout << "Hello world!\n";
return 0;
}
</source>
<error>
Undefined reference to "cout"
Undefined reference to "ostream::operator<<(char const *)"
HalCyOn, I was curious what "older standards" you were talking about in your post - respectfully, of course.Quote:
Originally posted here by HaLCy0n
The program that bludgeon posted will work, but I believe that is following the older standards. Let me explain exactly what the using namespace command does.
When you use the standard C++ functions, they are declared in the "std" scope. For example, if I did not put the "using namespace std;" I would have to say something like, "std::cout << "Hello World\n";" in order for it to work. That's just a little explanation.
If you wanna read more indepth about how namespaces work exactly, here's a link for you: http://www.cplusplus.com/doc/tutorial/tut5-2.html
Hope this helps.
My code:
#include <iostream.h>
void main()
{
cout << "Hello World!" << endl;
}
....compiles and executes in MS Visual C++ 6 and . NET
I've used "using namespace std;" - just never in a program this simple. Thanks!
You guys may feel this way, too...but I feel in programming, though there are many solutions to a probelm, there is usually one shining, elegant, beautiful, and simple solution. You know....complex in it's simplicity, tidy, ......oh, I could go on....but I won't. :-)
Anyway, I'm not refering to "Hello World" - just programming in general, and I wondered if any of you felt the same way.
As cgkanchi said, this is the correct way to write this program:
#include <iostream>
using namespace std;
int main()
{
cout << "\n\nHello World!\n";
return 0;
}
or you could have written:
#include <iostream>
int main()
{
std::cout << "\n\nHello World!\n";
return 0;
}
The cout statement belongs in the std namespace and can only be used within this namespace.
As for the include directive '#include <iostream>', this is the correct directive. Some implementations of the Standard Template Library require the directive to look like this '#include <iostream.h>. Notice that the latter directive is platform dependent, and not the original implementation as proposed by the inventors of C++. Still in many IDEs the first is used, for instance Borland C++ and MS VC++. In GCC the original implementation is used.
One suggestion in this thread does this mistake:
#include <iostream.h>
void main(void)
{
cout << "Hello, World!";
}
Using 'void main(void)' is very non-standard and not the correct way to decalre the main function. It will work however with many compilers, but the good ones will not let this pass. GCC returns this error: 'hello.cpp:6: main must return int'. Somehow void main has become default in Borland (I think).
BTW there is no reason not to use C functions in C++. The big difference between the languages lies in C++'s use of objects. Using the Standard Template Library is not a big issue. But once you've learned it it's very useful.
Wow, I overlooked this one...you are using "gcc". That's the C compiler. g++ is what you are going to want.
If I'm wrong, someone please correct me, but I'm thinking this is the issue we all overlooked here.
dont you have to use g++ for cpp. i thought gcc was just for c code! i could be wrong but it would explain why the compiler didn't understand cout
sorry HaLCy0n your post wasnt there when i looked at the gif, but i agree with you.
Yes, you have to use the g++ command for c++, as the gcc command for c. GCC is also the name of the compiler though (GNU Compiler Collection). Confusing! GCC contains both the gcc and the g++ command.
Yes gcc is for C and g++ is for CPP. Although i am using DJGPP (in this case the compiler will know what style to link via the file ext. i may be wrong- if so i can't find the C++ compiler...) I am using the RHIDE development tool that is packaged with DJGPP. The reason i wanted to use DJGPP is so that i could use it for windows apps (DJGPP is GNU)... I compiled it on my FreeBSD Box, no problems... For any further information concerning DJGPP please goto http://www.delorie.com/djgpp/ , it's free! They also have a really neat tool:: a free public compiler (granted it's only in C and only a few libraries are available- it's still pretty cool> check it out == http://www.delorie.com/djgpp/compile/ )
gcc compile the code I posted just fine, no problems at all, if you try to use cin>> though you'll get a bunch of errors.
I tried it on the gcc compiler under cygwin, and got the same
error when invoking the compiler as gcc.
When I used the g++ command, it compiled perfectly, so
you have to use the right command for c++ code. It doesn't
automatically tell from the cpp extension.
:cool:
there is no g++ command with the DJGPP package, is it called something else... the DJGPP page isn't too informative...
Maybe it doesn't have a c++ capability.Quote:
Originally posted here by tampabay420
there is no g++ command with the DJGPP package, is it called something else... the DJGPP page isn't too informative...
From what I understand, the same compiler does both c and c++,
but there is a different preprocessor or front end for c++,
hence the different command.
If you can't get it to work, maybe you'll have to get a different
compiler.
:cool:
I have no idea what's going on, but I just got it to work? I re-installed DJGPP and everything is A-OK. Thanx a lot gals/guys for all the help. I would never have known about the namespace and all of that. Thanx for helping me with programming problem!!!
:-)
Here's what you do. Try saving your file as .cc or .cpp it might work.
Cheers,
cgkanchi
mathgirl why u using that proprietary stuff? hehe... (j/k that's what I use too)