Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (arwym):

Currently in the process of learning C++, I picked up a free ebook titled "How to Think Like a Computer Scientist, C++ Edition." In the book, the "Hello world" program is given as: #include // main: generate some simple output void main () { cout << "Hello, world." << endl; return 0 } The book was published during 1999, so I can assume that some things have changed. Now, I was under the impression that "void" does NOT return anything? Was it different in the past? Obviously, trying to run this code gives me an error, but I want to understand more about this.

OpenStudy (anonymous):

Firstly, with a modern C++ compiler, you shouldn't use 'void' with main. Instead, use int; must be a terrible book if it uses void main() everywhere O.o: int main() { // program return 0; } nowadays, you would only need to write #include <iostream> instead of the old-style: #include <iostream.h> also, you need to prefix both cout and endl by std:: , so the program looks like std::cout << "Hello, world." << std::endl; or you can add the line using namespace std; so you don't have to prefix cout and endl by std:: A working program looks like #include <iostream> using namespace std; int main() { cout << "Hello, world." << endl; return 0; }

OpenStudy (anonymous):

why is it all different from the book? About using int main() over void main(): every C++ program must pass a return code of type int back to the operating system, which is why main must always be declared int and should include a `return' statement somewhere in the main function. Actually, you can ignore having to include `return 0' since the compiler will automatically do it for you anyway, as long as you declare main as returning int (and not void). about prefixing everything with std::, or having to use `using namespace std;' C++ has something called namespaces, and if you wish to use anything that comes from the C++ standard library (things like cout and endl), you have to prefix them with std::, or have the line `using namespace std;' in order to let the compiler know that these objects are from the C++ standard library.

OpenStudy (arwym):

Wow... Well, so far the book uses void for main() in all the examples I've found. o.O Perhaps I should go with a more up-to-date book. At least I have the C++ pocket reference from O'Reilly to help, though.

OpenStudy (arwym):

Thanks for the clarification! :)

OpenStudy (anonymous):

I learned some rudimentary C++ through Bucky Robert's video tutorials: http://www.youtube.com/watch?v=tvC1WCdV1XU&list=PLC6E50B89DA30C77A&index=1&feature=plpp_video For textbooks, I would check out some of the texts listed here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

OpenStudy (arwym):

Excellent! I will check those out, indeed. :D

OpenStudy (jagatuba):

I love Bucky. He was a great help when learning Java.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!