This tutorial for complete beginners.
Use a compiler like VC++ 6 or Bloodshed.
CODE
#include <iostream.h> //Includes iostream for I/O operations?!?!?!?!?!? LIEK
char f[50] = "Hello World!"; //Sets f to 50 character length and puts "Hello World!" in
int main() //Initialises main
{ //All code in Main goes after this
cout << f << "\n"; //Outputs whats in f then makes a new line ("\n")
return 0; //Ends the program
}//End of code for main
without speaking about the fact you need more point.
there is the exacly same thing but more powerful (and more c++
#include <iostream.h>
#include <string.h>
using namespace std;
int main()
{
string p = "hello world";
cout << p << endl;
return 0;
}
I thing it's better like that
Umm, if I had never seen C++ before I would just be lost from that. It's good to try to help people, but you can't really help beginners by showing them a 7 line code segment Hello World.
iostream.h? thats old
it should be #include<iostream>
If you really want to learn C++, use a good tutorial like the one from cplusplus.com: http://cplusplus.com/doc/tutorial/.
| elekis wrote: |
without speaking about the fact you need more point.
there is the exacly same thing but more powerful (and more c++
#include <iostream.h>
#include <string.h>
using namespace std;
int main()
{
string p = "hello world";
cout << p << endl;
return 0;
}
I thing it's better like that |
There are a couple of things wrong with that code, and a couple of things that could be done better. Here are the actual problems:
- The header <iostream.h> is not legal. It was never part of the standard, although it was common before the standard was finalized.
- The header <string.h> is not the header you think it is. That is the C header, with everything included into the global namespace. You want the C++ string header, which is <string>.
And here are some things that can be done better. When you're writing code to teach a complete beginner, it's always a really bad idea to start out by teaching them bad habits:
- It is a better idea to put the using directive in the smallest scope possible - in this case, at the top of main.
- It is a better idea to use the constructor idiom when constructing a variable. It is more clear that you are constructing something, and you're not relying on compiler optimizations to remove the unnecessary assignment.
- Is "p" really the best name for that variable? Why not something better, like "greeting"?
Here is the corrected code:
| Code: |
#include <iostream>
#include <string>
using namespace std; // <- You can use this here, but...
int main()
{
// using namespace std; // ... this would be better
string greeting = "Hello, world"; // <- Good...
// string greeting("Hello, world"); // ... better
cout << greeting <<endl; // Or '\n' instead of endl, if you prefer
return 0;
} |
Hello World's the starting program in every language but it's not helpful if you just give code like that. Comments help but you still need a lot of introduction of different parts of c++ code. And iostream.h is old. use #include<iostream>
I think writing short codes that too like Hello World are much obsolete now,
If you ppl can come out with serious project source codes, then post here else there is no fun in watching all those noobiee's coding right on the forum..
Well there is no online complier to check your mistakes in that code